r/javascript • u/BeamAlchemist • 15d ago
AskJS [AskJS] Are we putting too much application logic in the frontend?
I've been exploring Phoenix LiveView lately, and it got me thinking.
We've spent years moving more state and logic into the browser. But what if we kept most application state on the server and used JavaScript mainly for rich interactions?
I'm not talking about replacing JS. Libraries like Monaco, AG Grid, and Chart.js are great and I'd absolutely want to keep using them.
Curious what frontend devs think. Would you consider a server-driven approach if you could still use your favorite JS libraries, or is there too much downside?
8
Upvotes
57
u/sozesghost 15d ago
Time is a closed circle. State and rendering has been moving between front and backend for many cycles now. The cycle must not be broken.
18
u/El_Serpiente_Roja 15d ago
Remember the architect in the matrix and how he was like "we go back and forth on this shit all the time" ...yea, that's us
11
u/njmh 15d ago
My personal opinion is that any business logic that acts on data from the server should live on the server. So many apps I’ve had to work on have been some form of API request followed by a heap of client side massaging of data and conditional logic etc before the using it to render the UI. As much of that should be done on the server as possible and passed to the UI as is ready to render.
5
u/cuelark 15d ago
i'd say yes for anything that's mostly forms, dashboards and crud, since server driven (liveview, htmx, hotwire) means way less duplicated state and no api layer to keep in sync. the catch is latency, every interaction is a round trip, so anything that needs to feel instant like drag and drop or live editing still wants client state. what worked for me was server rendered by default and a small js island only where it has to feel snappy.
4
u/PeterJoAl 15d ago
It's fine to have application logic in the frontend, but you then also need to have that identical logic in the backend to stop people from sending you bad data. Good testing will spot when this is not done right, but people tend to not do end-to-end tests these days.
Personally, I place all UX stuff in the frontend for speed and double check any application logic in the backend when the API is called. The means having to handle at the UX level that a call failed due to a clash in logic without resulting in an inconsistent UX state. It's mildly annoying but a better experience for the user.
7
u/darcygravan 15d ago
biggest issue latency.
And liveviews approch creates sticky sessions on server which is harder to do horizontal scaling compared to state less rest api.
And beam vm was specifically made to deal with handling real time work other runtimes like node,python isnt really suitable.
also most importantly u cant do pwa or give clean dx on Bad internet With server holding states.
and another main killer would be server cost u r taking computations from clients device whihc is free to ur server which is paid.
3
u/leeharrison1984 15d ago
Read up on Sessions, that's essentially what you are talking about.
2
u/hicksyfern 11d ago
Fwiw we had a Phoenix live view app at my old job for the “home” view of what was basically “figma for maps”.
We switched from live view to react and graphql (served over Phoenix) and it was way faster, fewer bugs, and easier to develop.
All the Phoenix developers used to rave about how amazing live view was but in reality it was a really poor experience for us and our users.
9
u/Reashu 13d ago
Client-side computation is free, low-latency, available offline, easy to reverse-engineer, and can never be trusted. I don't put work on the server when I can, I do it when I must.
2
u/Tittytickler 15d ago
This has got to be satire 🤣
4
u/ashmortar 15d ago
https://htmx.org/essays/hateoas/
1
u/evangelism2 15d ago
Whether you keep your state in the backend or the frontend, Chart.js and others are irrelevant. It's just a way to display that state. It's up to you to determine were it exists
1
u/agenaille1 15d ago
Good user interface development will always be a dumb user interface that just paints what the backend logic ultimately says it should. Logic should not exist on the front end, as much as possible.
1
u/TheWebGuy95 15d ago
Honestly for most business apps, yeah. If it's forms, tables and dashboards, the database is the source of truth anyway, so keep the logic there.
Where it breaks down is anything that needs to feel instant, like drag and drop or offline stuff. Rough test I use: if the user can wait 100ms for it, server. If not, browser.
Also anything trust-sensitive (limits, permissions) should stay on the server, since the client can lie.
6
u/Ahmed_Ayachi 13d ago
If i see someone ask such question, is either i misunderstood him or he doesn't understand what the front client actually is. The real app, what matters, is the backend, the server, the frontend is just a fancy easy way for untechnical people to interact with that server so they don't end up sending their own requests with postman. There is no shared state, the state is in the server, the server tells the client the who, the what, the when and the how. The state you use in the frontend is just a way to manage the UI. When you delete an item you know which data object you should get rid of, when you add one, you know which data object corresponds to that item. If you need to know if an order is refundable or not that is something the server must tell you, hey take this order object and order.isRefundable true.
1
u/Sensitive_Mirror_472 15d ago
yes, logic belongs in the database
0
u/ardicilliq 15d ago
Can you define what you mean by application state? It can mean a lot of things, what is the scope you were thinking About?
Personally, when I hear handling various application state per user on the server side, I think about what does it mean in terms of performance and scalability, front end moving away from server rendered webpages to web apps allows for a lot of flexibility, and reduces server overhead, server computing is expensive.
the faster/leaner the server side work is, the more clients you will be able to serve simultaneously.
Data and internet bandwidth also costs the users their data plans, so the smaller/packes responses save users data.
Computing work done on the clients side is “free” computing power in that sense, so any complex data chewing and manipulation for pure presentation is better off done on the client side instead of clogging the server with actions that are just better delegated on the client.
Also, take into consideration that personal devices are very capable nowadays, and I don’t know about you but most of the VPS machines that I run do not have the same processing power of a random iPhone, or Android, so I’d argue the stronger device should do the extra work and leave the server to do what it does best.
I am not saying business logic heavy lifting should be done on the client, but the server doesn’t need to know a lot about the application state per user, it just needs to perform the needed CRUD operations securely and efficiently.
There still is value in server rendered webpages, for secure pages for example, where you’d rather have the server handle the sensitive data so it’s not setting there on a private machine, that is among other things what makes tools like next.js so powerful, when you can decide what runs where…
I ended up writing quite the scroll here… welp.. these were my two cents on the matter
1
u/my5cent 14d ago
Getting closer to renting everything... you don't like installing things?
1
u/LowLifeArcade 14d ago
The answer is simply user experience. What you're describing is basically MPA. What the web was before js frameworks started showing better UX with less interaction pausing. Most non-js devs usually jocky for this because it seems like the most by the book way of doing things. But we do things in a competitive world and if we don't compete we lose, that simple.
1
u/Used-Donkey-1014 14d ago
Old school server-side is still a very sensible architecture. All the arguments against it are just theoretically incremental improvements in speed, with a shit ton of added complexity. I honestly think "modern JavaScript", mainly SSRing SPAs, was when webdev jumped the shark. It was the influx of noobs getting dazzled by pseudosophistication. For sure none of it was necessary.
1
u/pyeri 14d ago
There are many reasons why a "fat" or front-end heavy client is preferable these days:
1
u/GoldfishPi 13d ago
ice cold take, it depends. Sometimes it makes sense to do something on the front end, sometimes is makes sense to do things on the back end. Do whatever is easiest for the application you're currently working on.
1
u/xfilesfan69 12d ago
What state has moved from severs into the browser?
Browsers have become more capable and so more stateful applications have been written for them. However that doesn’t mean state has moved from one end to the other.
State that should persist across sessions for a long time should live on the server. Applications state that shouldn’t live longer than the lifetime of a browser session should implemented in the browser.
Maybe I’m missing something, though.
1
u/podgorniy 12d ago
Architecture of corporate applications is often defined by the shape of the organization: number of team, their composition, hierachy and dependencies. The FE-heavy development facilitated independent work with fast FE exerimentation (amazon, booking) and independent BE developmet (release of APIs which aren't used yet for example, waiting for the FE to get released and then switched on). Same goes about microservices story.
There is nothing special in either approaches (heavy FE/heavy BE). Do whatever you see fits your case. Today LLM agents removed these limiting factors but the culture articles, people with beliefs relevant to that the era are still circulating.
Today is such time when new ideas, new approaches could sprout into something what will become next jquery/react. And these ideas are in the future not in the past.
1
u/CritcoThinker 11d ago
Both are still valid and depends on the app. Let powerful clients distribute load you'd otherwise pay for on the server. Or pay extra for server CPU to do computations on server and minimize things that could affect UX. Just depends.
1
u/scottix 11d ago
I was actually playing with this idea for a different reason. I wanted SSR to be faster and actually be smaller than if you were to write raw js files yourself. I would say MarkoJS is the closest I found commercially but was trying to use TSX/JSX.
1
u/MrLewArcher 10d ago
Probably
1
u/Kolt56 10d ago
I like to parse my file uploads to JSON so I don’t have to deal with file handling in the backend. Vs opening a can of worms.
1
u/yabai90 9d ago
That depends whether you need offline support ot not. It's more complicated than just "are we..?". Also a web app doesn't necessarily need a backend.
0
u/savage_spectrum 15d ago
Latency kills the vibe. Your UI will feel dead compared to local state. Also your servers become sticky nightmares that refuse to scale horizontally without a massive ops tax.
0
u/Far-Consideration-39 14d ago
Too little, not too much. Sure, the backend should confirm, but the UX can be so much better if you can remove the network roundtrip whenever possible.
1
u/[deleted] 15d ago
[removed] — view removed comment