Also, is it really easier to everything on the server? I don't think it is so by definition. You could argue that a stateless REST API with client-side state management is less complex than doing everything server-side. It's a much more scalable solution and creating interactive user interfaces is very easy.
Ofcourse it could be that interactive user interfaces is not something you are aiming for. That's fine. But do understand that most applications are actually usually very interactive.
Almost all applications that are written in the world today are glorified forms.
That's reality because 95% of apps are written for the business world. Most don't really do much apart from take some form data, apply business logic to it and show the results/send some messages.
Even most consumer apps like Facebook/Twitter/TikTok/Pintrest/etc. are basically glorified forms.
You can do save/load state all in nanoseconds, any competent programmer can easily aim for 50ms page loads, well under human detection.
This is incredibly basic stuff and how it was done for decades before SPAs.
Writing a stateful front-end that just asks for what it needs from a well-built API when it needs it is enormously simpler than trying to store all state in some server session and having to work around your own framework whenever you need a dynamic dropdown list.
We all seem to have collectively forgotten how awful web frameworks used to be, and how little interactivity the user actually expected. Things are way better now.
It's also possible to store ViewState "on the server side" instead of on the client side. So instead of sending megabytes up and down with every request, the client just gets a unique identifier. The downside is increased memory usage, but that's often better, especially for internal enterprise apps.
Yes, you would need multiple pages. But that is how most form-based SPAs work today anyway, and avoiding a page reload isn't always preferrable to just reloading if the SPA version requires heavy amounts of JS and the good-old-static version loads in the blink of an eye.
Yeah, but our users like interactive forms. When a widget is changed, they like feedback instantly.
Just about any shop that reloads the entire page when you change a single filter gets annoying pretty fast.
Imagine an online computer store: tick the box for "16GB RAM", wait for page to reload, tick "32GB RAM", wait for page reload, tick "512GB SSD", wait for page reload, tick "1TB", wait for page reload.
Now you may argue you still have to wait if it is an SPA, but then you're only waiting for the results. You can quickly tick all four boxes in an SPA and each one may cause a background request, but that doesn't matter because you don't have to wait for each request to complete to make your next selection.
Tick all the boxes and then click "Apply filter".
>
> Tick all the boxes and then click "Apply filter".
Many places do that, but it's not as user-friendly as not having to click "apply". If there's a large number of filters (Buying a computer on Amazon, for example), the "apply" button is off-screen most of the time - scroll to either the last filter or the first one to even know that there is an "apply" button.
Also, some fields do not work too well by having a separate "apply" filter step. You drop-down the manufacturer selection, choose "Lenovo", click apply and now there is a new drop-down for "Model".
The better experience is one where both those drop-downs exist, and second one is populated only after a choice is made on the first one. In all the pages I've seen that have drop-down #2 dependent on drop-down #1, those that got a JSON list via AJAX were much more pleasant to use than those that reloaded the entire screen just to add 5 items to the drop-down #2.
As the creator of the software, it's more important to have happy users than have an easier maintenance burden.
Doesn't that still reload the entire page when a checkbox is clicked?
Example:
I have an application where a form input expects an URL. As soon as any URL is entered, I need to:
1) Provide feedback on wether that's a valid URL or not, before the user submits the form
2) Show a suggestion for a page title, which is usually the <head> title extracted from that URL. If the user clicks on the suggestion, that should become the value of the next input in the form.
Other than this, this is a pretty basic form. But how do you give a decent experience without JavaScript?
Right, a bit of jQuery would do it in this case... but then more and more similar small tweaks are necessary, and before you realise you're swimming in spaghetti.
I can't make that logical jump. If you can't keep that tiny bit of code organized, why do you want to pile on the entirety of react and why would that be more organized?
Maybe you're saying it's a snowball of these things. I've seen that happen, but it didn't have to.
But, the worst part in my experience, is that without one of these "opinionated" frameworks such as React or Vue, each new developer that joins the team (after others leave) have a total different idea of how to organize the code, or they have a hard time understanding the "custom" organization and ideology behind the structure you've had to invent to keep in maintainable.
Again, I'm not a fan at all of frontend heavy frameworks, but they do solve a real problem, specially in large apps and large teams or teams with a lot of turnover.
I'd say it is the exact situation in the backend regarding using Flask vs Django, Sinatra vs Rails, etc... having a set of imposed opinions and structure have long term benefits as soon as you're not the only one working on the codebase.
maybe 50ms is under the "conscious detection" threshold (debateable, IMO...) but it will definitely feels "laggy"
In my experience with hiring, the only reason it has become easier to move a lot of logic and state to the frontend is because frontend developers are plentiful and easier to hire. I'm not dissing them: it's very easy to find great frontend devs that know their tools inside-out and also know the fundamentals of software engineering.
On the other hand, good backend developers are becoming rarer and rarer, and most of the new breed struggles to do virtually anything beyond a cookie-cutter REST API. Very few are knowledgeable about databases, for example. Very few know about deployment. Some of them aren't even familiar with HTTP as a protocol. Any backend dev that I interview that is as knowledgeable about their niche as the frontend devs I interview often gets two or three other offers and we have to raise offers very very often.
I'm pretty sure there's lots of great backend engineers who could do SSR like we did back in 1990-2010 with one hand behind their backs. But there just isn't enough of those. Most of the ones you see will have no idea how to deal with it.
On the other hand... maybe not, for more conservative industries? You don't need a full-blown frontend team to be very productive. A great frontend engineer (easy to find) and a cookie-cutter backend code-monkey (also easy to find) working together and getting paid 1x each are probably as productive as a great backend engineer you're thinking of paying 2x.
But the thing is that the 2x will bring some extra experience, less bugs... but that's not what companies are after.
So yep you have a point.
The solution in each of these cases is the same, though. To simplify the state.
In general, I think a lot of the pain in web development comes from attempting to have shared mutable state between backend and frontend. Shared mutable states are almost always messy. You can get around that by pushing it all to the frontend, or pushing it all to the backend. It's when it's straddling both you're in for a headache.
The issue I have is we had a great idea to refresh parts of a page with AJAX and then we went overboard and tried to move the entire application to complex frameworks which add more problems then they solve. We became so scared of screen rendering we avoided at all costs. This is bad UX as users actually get good confirmation that something has happened when there is a screen refresh.
It has also been forgotten that people used to complain about 100ms of latency. Since then, modern users (especially mobile) have been trained to accept horrible latencies that would have gotten you excoriated back in 2000.
100ms of latency is MUCH more difficult to deal with server side than 2-3 seconds of latency. 2-3 seconds is an eternity with modern clouds and modern network connections.
OTOH, for stuff like Facebook they absolutely prefer the interactive interface.
In fact, I don't know anyone that doesn't.
No, rendering html server-side is not nice and never was. Why on earth you will send markup template for every list item over and over again instead of sending just data and only one code-template (to render data on a client) ? Suppose you need to show to user a list of books on your web-page. Server side rendering means you need to send to user a markup consists of html elements for every book
<div> <div class="book"> <div class="book-title">Book 1</div> <div class="book-author">Author 1</div> <div class="book-description">..description..</div> </div> <div class="book"> <div class="book-title">Book 2</div> <div class="book-author">Author 1</div> <div class="book-description">..description..</div> </div> <div class="book"> <div class="book-title">Book 2</div> <div class="book-author">Author 2</div> <div class="book-description">..description..</div> </div> </div> ...
Don't you see something wrong with this? Why you need to duplicate over and over again html template for every list item resulting of significant increase of size of page to download if you can send only one template-component like this
<div> <div class="book"> <div class="book-title">{book.title}</div> <div class="book-author">{book.author}</div> <div class="book-description">{book.description}</div> </div> </div>
and a data in a more compact way
[{title: "Book 1", author: "Author 1", description: "..."}, {title: "Book 1", author: "Author 1", description: "..."}, ...]
or more compact like this
[["Book 1", "Author 1", "..."],["Book 1", "Author 1", "..."], ...]
or even in more compact binary-encoded format resulting in more than magnitude size compaction comparing to over-duplication html-approach. More size means more traffic for users to download over network and users which use data-roaming will "thank" you for your hundreds of kilobytes instead of dozens
This is why server-side rendering approach was flawed from the beginning (not because of page reloading but because of data duplication and traffic consumption)
- to turn the JSON into HTML, you not only need the template you mentioned, you also need code to execute that template (potentially lots of it); preact is 10KB ungzipped (other frameworks, like React/Angular/Ember, are way larger), so your HTML solution is already 10,000 characters ahead (by comparison, the markup you demoed is ~90 bytes per book, so you’re ahead until at least ~100 books on the page, without considering the framework, your code, and the following points)
- HTML compresses amazingly well because it’s repetitive so the overhead is less than you think
- HTML stream renders, so you can start rendering the first book immediately; JSON streaming is technically possible, but not built-in, and quite difficult to implement (and it requires even more JS)
- the same KB of JS is way more expensive than it would be as HTML (or images) because parsing, compiling, and executing is slow
- latency is often a larger problem than bandwidth, especially on mobile, so saving bandwidth is less important than (a) streaming and (b) client-side blocking time
- a large number of users visit one page and bounce, for typical websites, so the promised savings once the SPA is live doesn’t materialize
- the naive way to implement a SPA is _full_ of footguns; first of all, you’d naively load the template for all the pages, making the overhead problem even worse, naively you wouldn’t render anything until the JS has arrived, delaying the TTFMP enormously, naively you’d mess up routing, etc.
- it’s relatively trivial to preload/precache HTML pages, including in a service worker to support offline, making performance on par or better than a SPA
I happily work on a huge SPA daily, for context.
- about SPA - in my comment I said no word about SPA, it's a more complex layer on top. I only arguing about template-data separation approach to decrease data duplication and traffic consumption. And no, you don't need to rewrite your app as SPA and no react/preact/javascript frameworks required. You can use your server-side rendering and just change format of what you are sending to user. Instead of sending index.html with repetitive html markup for every list item you can basically send script-tag where you loop over data and build html-markup directly on a client
<doctype html>
<html>
<body>
....
<div id="books"></div>
<script>
const data = [{title: "...", author: "...", description: ""}, {...}]
document.querySelector("#books").innerHtml = data.map(item=>`
<div class="book">
<div class="book-title">${item.title}</div>
<div class="book-author">${item.author}</div>
<div class="book-description">${item.description}</div>
</div>
`).join(``);
</script>
</body>
</html>Yes, it lacks http streaming but - less repetitive html markup -> smaller size -> less time to download -> http streaming becoming less useful
And again - you can use your favorite http compression (gzip, brotli, etc) on top of this template-data separation approach to compress even more
https://pastebin.com/8CM1eUKQ the HTML version https://pastebin.com/ujrai58n the templated version
Even I was surprised by the result. The original size is what you’d expect: 5.3K for the full HTML version, 2.4K for the JS template version. Once Brotlied (default compression) however, the situation changes completely: 127 bytes (!) for the pure HTML version and 197 bytes for the template version. This is even true for Gzip, 213 vs 284 bytes.
I don’t know if Brotli (and Gzip) are somehow optimized for HTML, but yeah it really doesn’t make sense to use templates here.
I see what you're saying, but I think server-side rendering is simpler, and the concern about repeated HTML tags in the content is likely much easier to address by using gzip over the wire.
When doing frontend rendering you usually work against a REST(ful) API to fetch data, the idea is to reuse endpoints for multiple frontend components, because of this generalization you usually end up sending more data than necessary, not only between the database and the backend, but also between the backend and the frontend.
And not only that, you start doing JOINs over HTTP and JavaScript.
With server side rendering I can write an exact SQL query to render that component.
Also, in your example, don't forget the code required to render that data, which is often a second fetch and is, you guessed it, utf-8 / ascii/ unicode. Now we've double startup costs or forced http/2.
There's some context with these opinions. I concur that rendering server-side is often much, much simpler, but acknowledge that might be because that was what came first; and I acknowledge that some of the hate against client-side is b/c modern web often feels bloated and JS takes the blame.
You have a point, but not everything needs to be black and white. We had this nifty technology called AJAX even before SPAs were a thing. Now I understand that SPAs are built on AJAX calls too, but it is also possible to render (most of the) HTML on the server side and only use AJAX using off-the-shelf components such as Datatables when you absolutely need that functionality.
Also I know there’s the promise of sharing code between the front and backend but the reality is that you’re probably more likely to reimplement one or the other and then have to keep them in sync.
The overhead for doing it just the once on the backend and sending compressed HTML over the wire is pretty low for a small team in comparison.
but yeah, gzip or the more recent ones such as brotli have been taking care of those things for 25 years....
so HTML server-side has always been nice.
Especially semantic markup cough cough
When it's needed, fine.
But there are so many SPAs that just don't need to be. Rendering HTML server-side is just nice. It's simple and easy to work with. Weird states are unusual. The user gets a really normalized, predictable experience. And done well/quickly, there's little actual difference.
Maybe I'm just showing my age. This falls really close to my "I miss the old web" type sentiments. The last thing I built, and the next thing I will build, are purposely doing full page renders for any actions (no jQuery or React/SPA stuff). I don't think I'm going to have any javascript at all. It's really fun and lean. I don't know, it's just got me going lately--reminds me of when I was starting out.
My last project is 300 lines of python, 270 lines of php, html/css of course, postgresql, and cron. Some scrapers run, ingest data, and the php sorts and displays it.