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

Rate this content
Bookmark

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.

This talk has been presented at React Summit 2022, check out the latest edition of this React Conference.

FAQ

SSR, or Server-Side Rendering, is a technique in web development where JavaScript content is rendered into HTML on the server before being sent to the client. This improves SEO as crawlers can read the HTML as plain strings and enhances initial page load times.

Code splitting is crucial in modern web development because it allows developers to divide JavaScript bundles into smaller chunks. This results in faster initial page loads as only the necessary code is loaded dynamically based on the user's actions or route changes.

Before SPAs (Single-Page Applications), developers struggled with managing extensive JavaScript and DOM manipulations which made the applications tedious to handle. Frequent server roundtrips for navigation and rendering also degraded the user experience and performance.

Dynamic imports allow web applications to load JavaScript modules asynchronously as needed rather than loading a large bundle all at once. This reduces the amount of code processed and rendered initially, thereby improving the application's load time and runtime performance.

In React applications, solutions like Loadable Components are used to integrate SSR with code splitting. Loadable Components help in rendering content both on the server and client-side dynamically, deciding the environment automatically to optimize performance and resource utilization.

SSR plays a vital role in improving SEO for JavaScript-heavy sites by rendering JavaScript into HTML on the server-side before it reaches the client. This makes the content crawlable by search engines which often struggle to process JavaScript, thus enhancing SEO performance.

Yes, SSR and code splitting can be used together to optimize both the rendering performance and the SEO of a web application. SSR ensures quick first paint and SEO friendliness, while code splitting reduces the payload and speeds up the interaction readiness of the application.

Lena Zhukova
Lena Zhukova
9 min
17 Jun, 2022

Comments

Sign in or register to post your comment.
Video Summary and Transcription
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

A Guide to React Rendering Behavior
React Advanced 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
Optimizing HTML5 Games: 10 Years of Learnings
JS GameDev Summit 2022JS GameDev Summit 2022
33 min
Optimizing HTML5 Games: 10 Years of Learnings
Top Content
Watch video: Optimizing HTML5 Games: 10 Years of Learnings
PlayCanvas is an open-source game engine used by game developers worldwide. Optimization is crucial for HTML5 games, focusing on load times and frame rate. Texture and mesh optimization can significantly reduce download sizes. GLTF and GLB formats offer smaller file sizes and faster parsing times. Compressing game resources and using efficient file formats can improve load times. Framerate optimization and resolution scaling are important for better performance. Managing draw calls and using batching techniques can optimize performance. Browser DevTools, such as Chrome and Firefox, are useful for debugging and profiling. Detecting device performance and optimizing based on specific devices can improve game performance. Apple is making progress with WebGPU implementation. HTML5 games can be shipped to the App Store using Cordova.
How React Compiler Performs on Real Code
React Advanced 2024React Advanced 2024
31 min
How React Compiler Performs on Real Code
Top Content
I'm Nadia, a developer experienced in performance, re-renders, and React. The React team released the React compiler, which eliminates the need for memoization. The compiler optimizes code by automatically memoizing components, props, and hook dependencies. It shows promise in managing changing references and improving performance. Real app testing and synthetic examples have been used to evaluate its effectiveness. The impact on initial load performance is minimal, but further investigation is needed for interactions performance. The React query library simplifies data fetching and caching. The compiler has limitations and may not catch every re-render, especially with external libraries. Enabling the compiler can improve performance but manual memorization is still necessary for optimal results. There are risks of overreliance and messy code, but the compiler can be used file by file or folder by folder with thorough testing. Practice makes incredible cats. Thank you, Nadia!

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
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 🤐)
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
JSNation 2023JSNation 2023
174 min
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
Top Content
Featured WorkshopFree
Alba Silvente Fuentes
Roberto Butti
2 authors
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.
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
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.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- 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
React Performance Debugging
React Advanced 2023React Advanced 2023
148 min
React Performance Debugging
Workshop
Ivan Akulov
Ivan Akulov
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 🤐)
High-performance Next.js
React Summit 2022React Summit 2022
50 min
High-performance Next.js
Workshop
Michele Riva
Michele Riva
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.