Disclaimer The views here are very much my own and not the opinions of LogRocket.
A further disclaimer is that I have spent the last ten years working on pretty much nothing else but single-page applications in their many guises.
A possible definition of a single page application is:
A single-page application is a web application that requires only a single page load in a web browser.
My definition of a single page application is any application that relies solely on client-side rendering (CSR).
The growing thirst for highly interactive user interfaces (UI) resulted in more and more JavaScript code pushed to the browser. Javascript MV* frameworks grew out of the sprawling, messy codebases to bring order out of chaos.
Backbone.js was the first JavaScript MV* framework that opened the flood gates of hell to severe amounts of JavaScript being both shipped to the browser and parsed by the browser. This lead to the JavaScript running in the browser rendering dynamic HTML from the JSON responses of REST API calls and not the server. The infamous loading spinner that is so prevalent now emerged from the primeval swamp to take its place on the historical timeline of web development.
Following along after Backbone.js came the new kids on the block EmberJS, AngularJS and the current hotness React. Today it is probably more common to be using a JavaScript MV* framework than not as we want our web applications to behave just like their desktop counterparts.
I am not going to list the usual list of complaints about the SPA (single page application) that include things like SEO, performance problems, and code complexity. I do believe there are viable solutions for these problems, such as serving different content for web crawlers and code splitting for performance issues.
Building the web that works for everyone
My main problem with single-page applications is that they generally do not start life using progressive enhancement.
Progressive enhancement used to be a du jour concept, but the rise of the SPA has stalled it in its tracks as developers would rather deal with the new and shiny world that only the modern browsers allow. What about users in developing countries on slow networks or users of certain assistive technologies? We’ve turned a blind eye to ensure our CVs stay relevant.
If you create a new SPA using the CLI tooling from React, Angular, or Ember or whatever is du jour, then you are starting with the assumption that you are dealing with a Utopian world. The code is expecting to be running on a modern browser operating on a fast network with all the bells and whistles.
A broad definition of progressive enhancement is:
Progressive enhancement is a strategy for web design that emphasises core web page content first. This strategy then progressively adds more nuanced and technically rigorous layers of presentation and features on top of the content as the end-users browser/internet connection allow. — Wikipedia
What this means is that we start with the lowest denominator and add in enhancements such as JavaScript and we don’t start with the premise that a service worker is going to act as a proxy and cache content for repeat visits.
If we want to target a broader net of browsers and devices, then we need to ensure that the first time we visit a site, then the first page request is server-rendered preferably from an isomorphic web application.
If we take this approach, then our websites can work with JavaScript disabled, which is the holy grail of progressive enhancement.
We should also be using technologies associated with progressive web applications (PWA), more on this later.
I am going to use React as the example framework to outline the differences between the two types of rendering.
The main difference is that for server-side rendering (SSR) your server’s response to the browser is the HTML of your page that is ready to be rendered, while for client-side rendering (CSR) the browser gets a pretty empty document with links to your JavaScript and CSS.
In both cases, React needs to be downloaded and go through the same process of building a virtual DOM and attaching events to make the page interactive — but for SSR, the user can start viewing the page while all of that is happening. For the CSR world, you need to wait for all of the above to happen and then have the virtual DOM moved to the browser DOM for the page to be viewable.
The performance benefits of server-side rendering have been exaggerated and spun into a misrepresentation of the truth like a politician would use when uncovered.
A PWA is a web app that uses modern web capabilities to deliver an app-like experience to users. The previous definition is a very wishy-washy explanation, but I think for any application to be qualified as a PWA, then it must fulfill the following three criteria:
For some reason, many think progressive web applications (PWA) are single-page applications (SPA), as they often use the app shell model promoted by Google.
The app’s shell is in the context of the app shell model is the minimal HTML, CSS, and JavaScript that is required to power the user interface of a progressive web app and is one of the components that ensures reliably good performance.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="theme-color" content="#000000"> <link rel="shortcut icon" href="/favicon.ico"> <title>My PWA</title> </head> <body> <div id="root"></div> </body> </html>
The first load should be rapid and immediately cached. Cached means that the shell files are loaded once over the network and then saved to the local device. Every subsequent time that the user opens the app, the shell files are loaded from the local device’s cache, which results in blazing-fast startup times.
If you create a new application with create-react-app then the workbox npm package, which is a collection of libraries for progressive web applications, is also installed. The workbox generated index.html is a bare-bones HTML file which has JavaScript script tags and CSS link tags added by webpack at build time.
This approach relies on aggressively caching the shell (using a service worker to get the application running. Next, the dynamic content loads for each page using JavaScript. An app shell model results in blazing fast repeat visits and native-like interactions.
The code generated by create-react-app
is client rendered only. No server generates a full HTML request for the first load. We are expecting the code running on a modern browser with modern features. There is no thought for progressive enhancement in this world.
There are definite advantages to both approaches, so the optimal approach is to use the best of both worlds.
If you make proper use of server-side rendering, then the server should initially respond to any navigation requests that are received with a complete HTML document, with content specific to the requested URL and not a bare-bones app shell.
Browsers that don’t support service workers can continue to send navigation requests to the server, and the server can continue to respond to them with full HTML documents.
Below is a render function that I use to server render React components. I am using loadable-components ChunkExtractor
to load only enough JavaScript and CSS for that specific URL using code splitting.
export async function render({ req, res }: RendererOptions): Promise<void> { const extractor = new ChunkExtractor({ entrypoints: ['client'], statsFile, }); const context: StaticRouterContext = {}; const html = renderToString( extractor.collectChunks( <StaticRouter location={req.url} context={context}> <Routes /> </StaticRouter>, ), ); res.status(HttpStatusCode.Ok).send(` <!doctype html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta httpEquiv="X-UA-Compatible" content="IE=edge" /> <meta charSet="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> ${extractor.getStyleTags()} </head> <body> <div id="root">${html}</div> ${extractor.getScriptTags()} </body> </html> `); }
On the first load, a full HTML document is rendered that will still work if JavaScript is disabled.
Once the first load finishes, the react-router’s browser router takes over control of the navigation and, effectively, triggers the client-side rendering.
import React from 'react'; import { Routes } from '../../routes'; import { BrowserRouter } from 'react-router-dom'; export const App: React.FC = () => ( <BrowserRouter> <Routes /> </BrowserRouter> );
The hybrid strategy used by this approach to load the content doesn’t depend on a service worker, so even browsers that don’t support service workers can benefit from the implementation.
For browsers that do support service workers, we can still take advantage of the app shell model. Whenever a user triggers navigation inside the application, the service worker intercepts the request on the fetch event and adds the response to the cache. The next time navigation to that same URL is triggered, the service worker can load the content from the cache and delivers it instantly, without going to the network.
The service worker returns the same app shell HTML document for all navigation requests.
To make the app shell work, we need to get the service worker to cache a generic app shell HTML file. We can configure a special path like /app-shell
on the server to return a skeleton HTML file, and let the service worker fetch it during the installation of the service worker.
I use webpack and the workbox-webpack-plugin to generate the service worker config file.
Below is a scaled-down version of a service worker template file.
self.__precacheManifest = [].concat(self.__precacheManifest || []); // active new service worker as long as it's installed workbox.clientsClaim(); workbox.skipWaiting(); // suppress warnings if revision is not provided workbox.precaching.suppressWarnings(); // precahce and route asserts built by webpack workbox.precaching.precacheAndRoute(self.__precacheManifest, {}); // return app shell for all navigation requests workbox.routing.registerNavigationRoute('/app-shell');
In the above code, the self.__precacheManifest
variable stores all URLs that need to be pre-cached.
The call to workbox.precaching.precacheAndRoute()
tells the service worker to fetch and cache all these URLs in its install process and use the cached version to serve all future matched requests.
The workbox.routing.registerNavigationRoute('/app-shell');
instructs the service worker that whenever there’s a navigation request for a new URL, instead of returning the HTML for that URL, return a previously cached shell HTML file instead.
All we need is a route in our express application to return the app shell skeleton:
app.use('/app-shell', (req, res) => { res.status(HttpStatusCode.Ok).send(` <!doctype html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta httpEquiv="X-UA-Compatible" content="IE=edge" /> <meta charSet="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <!-- css link tags --> </head> <body> <div id="root"></div> <!-- js script tags --> </body> </html> `); });
I am amazed that this pattern is not more widespread. I think it is groundbreaking.
The single-page application made progressive enhancement take a back seat. The JAMstack and other similar frameworks have turned a blind eye to progressive enhancement and this to me is a backward step. We treat older devices as backwards compatibility. The web is often touted as for everyone but not in this world.
Progressive web applications following the app-shell model are blazing fast, but only if you are on a browser that supports service workers. Using a hybrid of rendering a full HTML document from an isomorphic JavaScript application and then letting the service worker kick in is where we should be heading. We are not in Utopia just yet, but we can breathe some life into the ailing progressive enhancement movement.
Install LogRocket via npm or script tag. LogRocket.init()
must be called client-side, not
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Hey there, want to help make our blog better?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowLearn how to implement one-way and two-way data binding in Vue.js, using v-model and advanced techniques like defineModel for better apps.
Compare Prisma and Drizzle ORMs to learn their differences, strengths, and weaknesses for data access and migrations.
It’s easy for devs to default to JavaScript to fix every problem. Let’s use the RoLP to find simpler alternatives with HTML and CSS.
Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.
8 Replies to "The single-page application must die"
You didn’t address the real issue. We need a common, accepted standard for distributed applications that is not built on HTML, css and js. Web assembly might open the door for that but we need an entire framework for building GUI apps. Everything we have now for HTML has been bolted onto something that was intended for a different use. We need a foundation built from the ground up for distributed apps that is supported by all operating systems and platforms but binary based.
Something else you left out is server workload vs client workload. With spas, you are leveraging millions of machines to do processing. With server rendering, you are putting more stress there. It makes sense to leverage a users processing power with client side code.
I disagree that it makes sense to leverage a user’s processor with client-side code.
As the site owner, you can scale the server processing easily. It can be scaled up and down rapidly in modern hosting environments. It can account for surges, low traffic, and ensure the same rendered code is delivered to all users (barring network issues). It is part of the cost of doing business.
You cannot, however, scale the end user’s processing power. If on an old phone, with a low battery, on dodgy cell service, and so on, you are leveraging millions of sub-par processors to create millions of sub-par experiences. You are essentially ceding the experience (and potentially reviews, word-of-mouth) to every device that does not match your developer’s device specs.
IOW, put the burden on the server where you can.
If a MPA were desirable in and of itself — and not merely a compromise made so that programs could be distributed over the web — then why did no one ever come up with a MPA for traditional PC applications and old-fashioned two-tier client-server applications?
They did, tabs and windows often serve to create multiple views focused on some need.
If an SPA can deliver a better user experience then it is the right choice. You simply cannot make some of the apps and interactions in a traditional MPA an example being Discord or YouTube Music. The user experience whilst using these products brings delight to those using them. Are they right all the time? No, but like anything they should be used in moderation and where they can excel at what they do best.
for logically: clear business division with view
for project management: fe-developer, be-developer
for scalability: I can serve a SPA or an android device with the same BE
for performance: rendering on the server means potentially thousands of pages on a single processor
by aptitude: code executed where it appears
for optimization: the data that transit on the internet is ONLY data
code cleanliness, scalability, speed, accuracy…
I would say that not using a SPA is just nonsense instead
Single-Page Applications (SPAs) are revolutionizing the field of web design and development.