Code Splitting: From 9.9 MB to 671 KB
From one 9.9 MB JavaScript file to 192 chunks at 671 KB gzipped. Three steps: route splitting, Three.js lazy loading, and dead code removal. Without losing any functionality.

Code splitting for your AI website sounds technical. But the principle is simple: stop sending code your visitor does not need. Our Lovable website sent 9.9 MB of JavaScript to every visitor. After code splitting, that was 671 KB. Same website, 93% less data.
In episode 2 we showed that our JavaScript bundle scored red in our SEO audit. In this episode, we show exactly how we fixed it — in three steps, without changing any functionality.
Your AI website ships everything to everyone
Here is what happens when someone visits your Lovable website. They want to see your homepage. Maybe your services. Maybe your contact page. What they get is everything.
For us, "everything" meant literally everything:
- 67 admin pages — dashboards, CRM, invoice management, email client, analytics, LinkedIn planner, financial overview
- 10 client portal pages — offer inspection, project overview, invoice history
- 5 developer pages — time tracking, capacity planning, project management
- Three.js 3D rendering — animations with 2,000 particles and rotating data blocks
- All public pages — the entire marketing website
All in one JavaScript file. 9.9 MB.
A visitor who just wanted to see the homepage had to download an entire business management system. On a fast network, you might not notice. On mobile — and more than 60% of your visitors are on mobile — that means seconds of waiting.
Why this costs you money
This is not a technical detail. It hits your business directly.
Google penalizes slow websites. Core Web Vitals have been an official ranking factor since 2021. Your Largest Contentful Paint (LCP) — the time until the largest element on your page is visible — is guaranteed to fail with a 9.9 MB bundle. Google ranks you lower than a competitor with the same content but a faster website.
Mobile visitors leave. 53% of mobile users abandon a website that takes longer than 3 seconds to load. With 9.9 MB, you lose visitors before they read your first word.
You pay more for ads. Google Ads rewards fast websites with lower cost per click. A slow website literally costs you more — the same click costs more money.
Your bounce rate increases. Every extra second of load time increases your bounce rate by 32%. With 9.9 MB, you are looking at multiple extra seconds.
The three-step approach
We reduced our bundle in three steps: route splitting, library lazy loading, and dead code removal. Each step is valuable on its own. Together, they reduced our bundle by 93%.
Step 1: split your routes — the biggest win
The principle is simple: code that only admins need does not need to be sent to public visitors. Code for the client portal does not need to be sent to people who are just viewing your homepage.
In React, this is called `React.lazy()`. Instead of loading all pages at once:
```javascript
// ❌ Old: everything gets bundled into one file
import AdminDashboard from './pages/admin/AdminDashboard';
import SharedInbox from './pages/admin/SharedInbox';
import CRMPipeline from './pages/admin/CRMPipeline';
```
You load them only when needed:
```javascript
// ✅ New: only loaded when the user navigates there
const AdminDashboard = React.lazy(() => import('./pages/admin/AdminDashboard'));
const SharedInbox = React.lazy(() => import('./pages/admin/SharedInbox'));
const CRMPipeline = React.lazy(() => import('./pages/admin/CRMPipeline'));
```
The difference: in the old situation, every page is in your main bundle. In the new situation, Vite creates separate files that are only downloaded when someone actually navigates to that page.
We did this for 85 pages:
| Category | Lazy imports | What is in them | |
|---|---|---|---|
| Admin | 67 | Dashboards, CRM, email, invoices, analytics, LinkedIn, content management | |
| Client portal | 10 | Offer inspection, projects, invoice history, authentication | |
| Developer | 5 | Time tracking, capacity planning, project management | |
| Auth/setup | 3 | Password reset, digital signature, setup |
49 public pages stayed static. Those need to load fast and are converted to HTML by our prerender system (episode 4). You want those in the main bundle.
The result: a public visitor now only downloads the code for the marketing website. The 67 admin pages, the 10 client portal pages, the 5 developer pages — they do not exist for them. They only load when an authenticated user navigates there.
Step 2: lazy-load heavy libraries — Three.js
Our website has 3D animations on the homepage. Rotating particles, glowing data blocks, it looks great. But Three.js — the library that makes this possible — is 831 KB. That is more than the entire rest of the homepage combined.
The problem: if Three.js is in your main bundle, every visitor has to download those 831 KB before they see anything. Even if the 3D animation only comes into view after scrolling.
The solution: we load Three.js only when needed, and show a CSS gradient as placeholder in the meantime.
```javascript
// The wrapper component
const Hero3DBackgroundInner = React.lazy(() =>
import('./Hero3DBackgroundInner').then(m => ({ default: m.Hero3DBackground }))
);
// The CSS gradient that appears instantly
const Fallback = () => (
<div className="absolute inset-0 overflow-hidden">
<div className="absolute inset-0 bg-gradient-to-br
from-orange-500/10 via-blue-500/10 to-purple-500/10
animate-pulse" />
</div>
);
// Suspense shows the gradient while Three.js loads
export const Hero3DBackground = () => (
<Suspense fallback={<Fallback />}>
<Hero3DBackgroundInner />
</Suspense>
);
```
What happens here: the visitor sees a subtle animated gradient immediately. In the background, the browser downloads Three.js. Once it is ready, the 3D animation fades in. On a fast network, this takes 50 milliseconds — most visitors do not even notice the switch.
The result: 831 KB out of the main bundle. The homepage loads instantly, the 3D animation follows.
We applied this pattern in two places: the Hero3DBackground and the ScrollGuided3DEffect. Same principle, same result.
Step 3: remove dead code — the easiest win
The third step is the simplest and most often forgotten: find code that is not used anywhere and delete it.
What we found:
1,105 lines of dead offer code. Six old components and five pages for an offer system that had been completely rewritten. The old version was still in the bundle, imported nowhere, but Vite bundled it anyway.
The AI builder watermark. Lovable adds an external script to every website it generates — `gptengineer.js`. This script loads on every page, including preconnect and dns-prefetch hints. It does nothing for your visitors, but it costs bandwidth and load time.
Unused imports. Components that were once imported but no longer used. A `bundleOptimization.ts` file of 337 lines that was called by no other file.
Removing dead code sounds trivial, but the difference adds up. Every kilobyte you remove is a kilobyte less that every visitor has to download, on every visit.
The result
| Metric | Before | After | Difference | |
|---|---|---|---|---|
| Main bundle | 9.9 MB | 2.6 MB | -74% | |
| Gzipped | 2.6 MB | 671 KB | -74% | |
| JavaScript files | 1 | 192 | 192 separate chunks | |
| Three.js | In main bundle | Separate lazy chunk | 831 KB saved | |
| Dead code | 1,400+ lines | 0 | Removed |

Bundle treemap: from one block of 9.9 MB to 192 chunks
From one file of 9.9 MB to 192 chunks where the main bundle is 671 KB gzipped. The exact same website, same functionality, but 93% less data for a public visitor.
Why this goes beyond speed
Code splitting is not just a performance optimization. It has direct SEO impact.
Better Core Web Vitals. A smaller bundle means a faster LCP. Google measures this and uses it in your ranking. Our LCP went from "failing" to "good" — purely through code splitting and lazy-loading Three.js.
Lower bounce rate. A website that loads in 1 second instead of 4 seconds loses fewer visitors. Fewer bounces means a higher ranking.
Better crawl budget. Google has a limited budget for crawling your website. A faster website gets crawled more often and more deeply. More pages indexed, faster.
Lower ad costs. Google Ads Quality Score factors in load speed. A faster website = lower cost per click.
How to check this on your own website
The quick check is simple:
1. Open Chrome DevTools (F12) → "Network" tab → reload your page
2. Filter by JS — how much JavaScript is being loaded? How many MB?
3. Check the sources — do you see admin components, dashboards, or portal code? Those should not be there for a public visitor.
If you see more than 2 MB of JavaScript for a marketing website, there is work to do. If you see admin code loading on your public pages, code splitting is priority number one.
The context: this is step 3 of 10
Code splitting is an important step, but it is not the only one. In episode 2 we showed all twelve SEO problems we found. Code splitting solves the bundle problem, but you also need prerendering to make your website visible to crawlers (episode 4), and image optimization to reduce your total page weight.
Together they make the difference between a website that is invisible and a website that performs.
Frequently asked questions
Does code splitting work with Lovable?
Yes. Lovable generates React code, and React.lazy() is a standard React feature. You can apply code splitting to any Lovable website by adjusting the imports. You do not need to leave or migrate from Lovable.
Does code splitting break my website?
No, if done correctly. The functionality stays exactly the same. The only change is when code is loaded — not whether code is loaded. Every page still works, admin pages just load only when an admin visits them.
What does code splitting cost?
Time, not money. The adjustment itself is mechanical: change `import X from` to `React.lazy(() => import())`. For us, that was 85 imports, which took a few hours. The result — 93% smaller — is permanent.
Can I do this myself or do I need a developer?
If you are comfortable with React code, you can do this yourself. The pattern is always the same. If you do not have a technical background, a developer or integration specialist can do this in a day.
Should I also optimize my images?
Absolutely. Code splitting reduces your JavaScript, but unoptimized images are often an even bigger problem. We had 30 MB of images — more than three times our JavaScript bundle. We cover that in episode 7.
---
This is episode 3 of the 10-part series "The invisible website." Previous episode: Lovable SEO audit: every problem we found. Next: prerendering — the free fix that makes your website visible.
Want to know if your AI website sends too much JavaScript? Get in touch for a free audit, or see our custom applications to learn how we optimize websites.