The Only Way to Eat an Elephant - Code Splitting With Server Side Rendering


Code splitting is a way to optimize the performance of our app by shipping less code to the end users. The most common form of code splitting is to use dynamic imports after a user interaction. In our case, we’re going to talk about conditionally loading only the parts of the application that we need during server side rendering. We’re going to discuss the problems and the solutions, so you could try it out in your own project.

9 min
17 Jun, 2022


Sign in or register to post your comment.

AI Generated Video Summary

The Talk discusses the challenges of handling DOM manipulation with JavaScript and the performance issues that arise from using SPAs. It explores the solution of server-side rendering (SSR) to address SEO concerns but notes that bundle size remains an issue. Code splitting is introduced as a way to load content dynamically, but it presents challenges for SSR. The speaker suggests a solution using vanilla.js that solves the problem without scalability issues.

1. SSR Code Splitting and Vanilla.js

Short description:

Hi, my name is Lena, and today I'm here to tell you about the only way to eat an elephant. It all started at a time when there was no React. HTML pages were served as strings, but JavaScript made it tedious to handle DOM manipulation. JavaScript heroes introduced SPAs, but the increase in JavaScript resulted in worse performance. To solve the SEO problem, SSR was brought back, but bundle size remained an issue. Code splitting was introduced to load content dynamically, but it posed challenges for server-side rendering. The vanilla.js solution solves the problem but doesn't scale.

Hi, my name is Lena, and today I'm here to tell you about the only way to eat an elephant. You can probably remember it's bit by bit, and I have one disclaimer to make. No, I do remember it's a React conference, but I will be telling my brief story of SSR code splitting and why you need to use them both using vanilla JS mostly.

So it all started at the time when there was no React. Someone does remember these times and someone can't even believe that there was such time when there was no React. But that time all HTML pages were served as strings. You wouldn't believe that. But the whole content was a string served from servers, and it looked something like this. One wrote for a whole HTML content and there were many of them, as much as many rows you have, and actually everybody was happy that time. It was pretty straightforward actually, until there, they were not. And you can probably guess the reason. It was because of JavaScript.

So, it became too tedious to handle all the JavaScript, all DOM manipulation with jQuery, you know, with rendering tools like EGS, handlebars, and many, many more in every server language, and also the server roundtrips made people suffer, and I can hardly tell you who suffered more, users or developers, with all their JavaScript, jQuery, and handlebars. So, JavaScript heroes came, and they decided to have SPAs, single-page applications, and and they were imagining rendering content, all the content, on the client-side and they decided that the browser can handle navigation without going back to the server and they think, they thought it was cool, and also they decided they can do client-side routing, not because it is easy, but because it is hard. But they can. And the HTML because became very small and shallow and nothing actually was rendered on server anymore. Everything was rendered in client-side with one of JavaScript frameworks.

And there became more JavaScript, more tools to build it in one bundle, and more JavaScript problems to solve, more JavaScript conference to talk about these problems and new monsters were born. Actually, more JavaScript resulted in bigger bundle sizes, which resulted in worse performance, first rendering, and everything else, and actually users started to suffer and also business started to suffer because SEO was a problem. Crawlers can't read JavaScript, they need strings. So JavaScript heroes came back again, and they decided to have SSR back, but this time was there the same JavaScript they wrote, because it was so beautiful to throw it away and they decided that the solution is to render this JavaScript they have to a string on server and to have JavaScript function returning a string. So that was a good solution for SSR and it solved SEO problem. But it didn't solve bundle size problem, right? So they were using both the same JavaScript on client side and server side. So JavaScript heroes decided so that they need to come up with an idea of code splitting. And they did. They did these dynamic imports. Here you can see that there is one problem with this code. Not because it's not in React, but because it's asynchronous. And it does solve the problem of a big JavaScript bundle. You can see that now the content is not in one bundle, but loads dynamically. But dynamic imports is for client-side and they can't be rendered on server-side because server-side needs to do it synchronously. So when your content depends on the route, meaning you need this content right away for client to see it, you can have code splitting both on server and client-side. The only problem you need to solve is to decide on the environment you're in and to load it synchronously when you are not in client-side environment and load it synchronously when you are. So this is vanilla.js solution for this problem and as much as I like vanilla.js, we all know it is It doesn't scale. It solves the problem, but it doesn't scale.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Too much JavaScript is getting you down? New frameworks promising no JavaScript look interesting, but you have an existing React application to maintain. What if Qwik React is your answer for faster applications startup and better user experience? Qwik React allows you to easily turn your React application into a collection of islands, which can be SSRed and delayed hydrated, and in some instances, hydration skipped altogether. And all of this in an incremental way without a rewrite.
React Summit 2023React Summit 2023
24 min
React Concurrency, Explained
React 18! Concurrent features! You might’ve already tried the new APIs like useTransition, or you might’ve just heard of them. But do you know how React 18 achieves the performance wins it brings with itself? In this talk, let’s peek under the hood of React 18’s performance features: - How React 18 lowers the time your page stays frozen (aka TBT) - What exactly happens in the main thread when you run useTransition() - What’s the catch with the improvements (there’s no free cake!), and why Vue.js and Preact straight refused to ship anything similar
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Our understanding of performance
user-experience has heavily evolved over the years. Web Developer Tooling needs to similarly evolve to make sure it is user-centric, actionable and contextual where modern experiences are concerned. In this talk, Addy will walk you through Chrome and others have been thinking about this problem and what updates they've been making to performance tools to lower the friction for building great experiences on the web.

Workshops on related topic

React Summit 2023React Summit 2023
171 min
React Performance Debugging Masterclass
Workshop Free
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
JSNation 2023JSNation 2023
174 min
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
Workshop Free
This SvelteKit workshop explores the integration of 3rd party services, such as Storyblok, in a SvelteKit project. Participants will learn how to create a SvelteKit project, leverage Svelte components, and connect to external APIs. The workshop covers important concepts including SSR, CSR, static site generation, and deploying the application using adapters. By the end of the workshop, attendees will have a solid understanding of building SvelteKit applications with API integrations and be prepared for deployment.
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Workshop Free
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Workshop Free
- Introduction
- Prerequisites for the workshop
- Fetching strategies: fundamentals
- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)
- Test your build and serve it on Vercel
- Future: Server components VS Client components
- Workshop easter egg (unrelated to the topic, calling out accessibility)
- Wrapping up
Vue.js London 2023Vue.js London 2023
49 min
Maximize App Performance by Optimizing Web Fonts
Workshop Free
You've just landed on a web page and you try to click a certain element, but just before you do, an ad loads on top of it and you end up clicking that thing instead.
That…that’s a layout shift. Everyone, developers and users alike, know that layout shifts are bad. And the later they happen, the more disruptive they are to users. In this workshop we're going to look into how web fonts cause layout shifts and explore a few strategies of loading web fonts without causing big layout shifts.
Table of Contents:
What’s CLS and how it’s calculated?
How fonts can cause CLS?
Font loading strategies for minimizing CLS
Recap and conclusion
React Summit 2022React Summit 2022
50 min
High-performance Next.js
Next.js is a compelling framework that makes many tasks effortless by providing many out-of-the-box solutions. But as soon as our app needs to scale, it is essential to maintain high performance without compromising maintenance and server costs. In this workshop, we will see how to analyze Next.js performances, resources usage, how to scale it, and how to make the right decisions while writing the application architecture.