Hey everyone, I'm Gil. I'm a performance architect at Wix, and today I want to walk you through how we use various code splitting techniques to improve the performance of Wix websites. Code splitting allows us to break down JavaScript into smaller chunks or in React into multiple components, and load them only when necessary, ensuring users download only the code they need. By applying these techniques, we significantly reduce the JavaScript sent to the client side, leading to major improvement in user experience.
As I mentioned, I'm a performance architect at Wix, which means my focus is on improving the performance of millions of Wix websites. Since we use and rely heavily on React, a significant portion of my work involves debugging the performance of React applications, because essentially every Wix website is a React application. Given the vast range of features Wix websites offer, code splitting is absolutely essential for us. The techniques I'm about to share have made a huge impact. But before we dive into the specific code splitting methods, I'd like to explain how we measure performance at Wix. In production, we measure performance using Core Web Vitals, a set of metrics developed by Google to assess the performance of live websites. Specifically for JavaScript loading, we focus on INP, which means interaction to next paint, which evaluates the website's interactivity. In the graph, you can see the INP scores for Wix and our competitors reported by Google. Since interactivity is closely tied to JavaScript loading, reducing the amount of JavaScript significantly improves INP. When a website loads more JavaScript, the main thread becomes more occupied with parsing and executing code. This leads to delay in user triggered events, like, for example, clicks and hovers, which in turn negatively affect interactivity and INP. So as you can see in the graph, we significantly improved our INP scores, going from a very low score to achieving one of the best INP scores compared to our competitors. This improvement is largely due to the code splitting techniques I'm about to share with you today.
Now, let's dive into the various code splitting strategies we use to minimize JavaScript download in our React applications. The first one is dynamic import, and there are a few ways we use dynamic imports in our React application. So the most basic way we use dynamic imports is loading React components dynamically after interaction with React Lazy. Let's see a short and basic code example for this. So as you can see, we're using dynamic imports, and we're using React Lazy. And this is, of course, to not bundle, in this case, collapse component into the main bundle because we do want to load it only after the user opened some other component. So we're using React State to handle the state if we should show the component or not. And then based on the state, we render the component, the Lazy component with suspense, and only then, only when the component is rendered, React will load and hydrate the component because we're using React Lazy. So suspense have a fallback, and it's important because the component is not shown until it's loaded. So we do need a fallback for that. But then React will render the component. So a very basic example. I'm sure many of you already know this.
Comments