r/javascript • • 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

72 comments sorted by

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.

2

u/BeamAlchemist 14d ago

xD maybe the answer was never moving everything to one side but getting better at combining both

6

u/foxsimile 15d ago

Let’s stick it in the middle.

6

u/rudeluv 15d ago

Middle out?

7

u/foxsimile 15d ago

I’m a midstack developer

1

u/Subway909 12d ago

This guy fucks

2

u/Reashu 13d ago

I guess that's edge computing

2

u/EscherSketcher 12d ago

Dark reference 🤔

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

2

u/BeamAlchemist 14d ago

...and somehow every cycle comes with a completely new framework xD

1

u/Renan4Santos 12d ago

Programmers are just a bunch of sheep

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.

8

u/Discordbothost 15d ago

this is the version of the argument i agree with. the test i use: if two different clients (web + mobile, or web + a cron job) would need the same rule, it belongs on the server. stuff like "is this order refundable" or "what's the discounted total" ends up duplicated and slightly different in every client otherwise. what stays in the browser is the stuff that's only about the screen, open/closed panels, optimistic updates, form state before submit

1

u/BeamAlchemist 14d ago

That's a great way to draw the boundary. If multiple clients need the same rule, it probably shouldn't be duplicated in each one. UI-specific state is a different story that is what I mean above too.

4

u/bartread 14d ago

> "is this order refundable"

> "what's the discounted total"

Also great examples of things you absolutely cannot trust if calculated on the client-side anyway. Whether or not an order is eligible for refund is 100% a server-side concern otherwise "anyone"* could simply set whatever value they wanted on the client.

*I obviously realise this is beyond the capabilities of the vast majority of real users but the risk is nevertheless real.

1

u/Discordbothost 14d ago

yep, and it doesn't even take devtools skills, just replaying a request with a different body. the client can display the total, the server is the one that gets to decide it

2

u/BeamAlchemist 14d ago

Exactly. Especially when the frontend spends half its time transforming API responses into something usable. I'd rather have the server return data that's already shaped for the UI and it is not just about GUI so it might be that CLI needs the same data as well or vice versa eventually server should satisfy all its clients.

5

u/njmh 14d ago

Ideally that’s what BFFs are for. They belong to the frontend that consumes them so they can handle shaping the data exactly how the frontend needs it.

-1

u/WasteAmbassador47 14d ago

Why?

I am of the opposite opinion for these reasons:
- simpler backend causes less bugs and has more stable APIs.
- cheaper compute since running logic on client is free
- it is faster to re-deploy the frontend than backend.
- making changes on backend is riskier because it is “closer to data”, so it is a slower process.
- moving more UI logic to backend means that in practice UI engineers have to wrangle two codebases at the same time or even two different languages and build/deployment processes.
- certain things in the UI take several requests to accomplish, on the frontend you are in full control of how to make those requests, eg can show some UI when not all the data has arrived.

4

u/Discordbothost 14d ago

most of those are fair for presentation logic, but the catch is anything that decides what's allowed: prices, discounts, permissions, validation. that has to run on the server no matter what because the client can be edited in devtools. so if you also put it in the frontend you now have the same rule in two places, and they drift. that's usually where the bugs come from, not from the backend being bigger.

filtering, grouping, formatting, optimistic updates, showing partial data while requests land, all fine on the client imo

1

u/EvilPencil 14d ago

This. Frontend has validation to catch the easy stuff and keep the users on the happy path, but for complex logic that depends on the state of other parts in the system, the backend is always authoritative.

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.

1

u/BeamAlchemist 14d ago

This is pretty much the approach I like. Server-driven by default, JS where it makes sense. LiveView hooks are great for that, although integrating complex JS widgets can still require quite a bit of glue code.

1

u/cuelark 9d ago

Exactly—the server-driven default keeps ordinary flows simple. For rich widgets, putting a narrow boundary around their events and state can contain the glue without moving the whole page to the client.

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.

1

u/BeamAlchemist 14d ago

That's the duplication I'm trying to avoid. You still want client-side feedback for a good UX, but maintaining the same business rules in two places can become painful. The backend has to remain authoritative anyway.

1

u/PeterJoAl 14d ago

If you use a JS-backend like Node, you can reuse a lot of the validation libraries you write for your business logic.

Another fairly good option is to use a generator to take your rules and make client- and server-side libraries. More annoying to setup, but then rule changes magically appear in both with no effort.

The backend has to remain authoritative anyway.

I wish this were true, but I see a LOT of modern projects that have fallen into the classic trap of trusting the clients WAY too much.

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.

1

u/BeamAlchemist 14d ago

Fair points, but what happens when you're dealing with huge datasets? Imagine complex filtering, sorting, and transformations. Do we really want to push all that processing onto every user's device?

Client-side computation isn't exactly free either. It costs CPU, memory, and battery, especially on lower-end devices.

Phoenix LiveView handles server-driven updates and concurrent connections really well, too. Of course, latency, offline support, and server costs are valid concerns. That's why I prefer a hybrid approach: keep heavy data processing and business logic on the server, and let JS handle interactions that need to feel instant.

1

u/WesAlvaro Front-End Engineer 14d ago

As with practically everything in this field, there is no single best answer for every scenario.

3

u/leeharrison1984 15d ago

Read up on Sessions, that's essentially what you are talking about.

1

u/BeamAlchemist 14d ago

There's definitely some overlap, but I can say that LiveView takes it further. It maintains a stateful process over a persistent connection and pushes UI updates, rather than just storing session data between independent requests. As far as I know sessions do not provide suhc advantages but will still keep in mind thanks!

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.

2

u/hicksyfern 11d ago

GraphQL gave us a good way to tend towards adding logic on the server instead of the client. With REST the tendency is to just build entity-fetching endpoints but with GraphQL it leads you towards building whatever API you need for a given feature - causing more logic on the server and less on the client.

1

u/BeamAlchemist 10d ago

That actually sounds like a really practical architecture. You guys seem to have found the right balance!

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 🤣

1

u/BeamAlchemist 14d ago

no no do not take it like that :), genuinely curious. With tools like LiveView and HTMX gaining traction, I think it's worth revisiting where application logic belongs.

2

u/Tittytickler 14d ago

Yea I see where you're coming from after reading the other comments lol. I was laughing because I've been doing this for 9 years and like every 2 years the sentinent flips and we go from server heavy to frontend heavy back to server heavy, etc.

Ultimately I think what should be done is whatever is best for your use case, and its generally a mix of both in my experience. Some things have no option but to run on the frontend, and some things should never be computed on the frontend. ¯_(ツ)_/¯

1

u/BeamAlchemist 14d ago

Exactly I also think that hybrid approach is more sustainable to go with as it is kinda vicious circle as you said xD

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/BeamAlchemist 14d ago

Well just imagine an AG Grid table with 5 million rows. Loading, sorting, and filtering all that data in the browser could freeze the UI or exhaust memory.

With LiveView, you can keep the data on the server and send only the visible rows to AG Grid. JS handles the interaction, while the server handles the heavy lifting. That's where the architecture really matters.

1

u/evangelism2 14d ago

I'm well aware of that. But whether you load all 5 million rows or you only load 100,000 of them, you're still going to use AG Grid for both.

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/BeamAlchemist 14d ago

I mostly agree, although I'd still want the UI to be smart about interactions. Things like animations, drag and drop, and editors should absolutely run locally. Business decisions are where I'd prefer the server to take control.

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.

1

u/BeamAlchemist 14d ago

That's a useful rule of thumb. I'd just add that even with LiveView, interactions like drag and drop can stay entirely in JS. Server-driven doesn't have to mean server-dependent for every tiny UI interaction.

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.

2

u/Friction_693 12d ago

Best explanation

1

u/Sensitive_Mirror_472 15d ago

yes, logic belongs in the database

1

u/BeamAlchemist 14d ago

Until the stored procedures start asking for their own frontend framework xd

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/BeamAlchemist 14d ago

by application state I mostly mean business rules, permissions, workflows, and data transformations, not UI details like open menus or animations. I agree that moving every computation to the server would be an absolute mistake that is kind of using resources of the server for clients unnecessarily. The interesting part is finding the right boundary.

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:

  • Cost efficiency. Static hosting costs less, it's nearly free these days. But putting all logic and app assets in frontend JS, you ensure minimal running cost for your stack.
  • Browser the de-facto app. Chrome, Edge, Firefox and Safari have become the de-facto apps on most desktops and smartphones, it makes sense to use their built-in features rather than bring our own.
  • Rise of the machines. It's not just the browser's ubiquity but the machines they run on have become more than capable over the years. Even in the developing country where I live, 8-16 GM RAM has become quite common among smartphone users 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.

1

u/BeamAlchemist 14d ago

Latency is a fair concern, but not every interaction needs a server round trip and trust me LiveView can still run JS locally for instant interactions I ahve experienced what you mean and it still depends on how you handle things in JS again so at client/browser side and the BEAM handles lots of concurrent connections pretty well. Stateful connections do add scaling tradeoffs, though.

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

1

u/BeamAlchemist 14d ago

Agreed, the question is really which logic belongs where. Of course I wouldn't move animations or immediate interactions to the server as well but having the server own business state while JS handles presentation feels like a good balance to me.

1

u/Vizeroth1 14d ago

I like it when I can still do everything with JavaScript disabled. I have no problem with JavaScript making the user experience better, but it shouldn’t be necessary.