- Published on
Why Your Next.js Page Sends Too Much JavaScript
- Authors

- Name
- Mehdi Akiki
Reference · Through the layers
The page has a title, some text, and a small selector. It still sends a lot of JavaScript.
Before replacing the framework, find out which part of the page needs to run in the browser. A tiny interactive control can pull in much more code when its parent is marked as a client component.
Let's use a report page. Its introduction totals an imported report history. The selector needs state, but doesn't use those historical rows. Those parts don't have to share the same client entry point.
Find the first client import
In Next.js App Router, 'use client' marks a boundary in the module graph. Modules imported through that boundary can become part of the browser's bundle. It doesn't mean every visually nested component must be a client component: server-rendered content can also be passed through props such as children.
The server and client component guide describes this distinction. It matters because moving JSX around without changing imports may leave the same JavaScript in the browser.
Start at the route and find the directive. Then follow its imports. Does a stateful selector import a formatting library used only for the static introduction? Does one client layout import every page tool? Are you importing a whole package for one small function?
Use the bundle output to confirm the suspicion. The most familiar dependency name isn't always the largest cost.
Put the selector behind its own boundary
The change we want is small:
Before: client page -> ./report-history -> archived rows
-> ./ReportPicker
After: server page -> ./report-history -> summary text
-> ./ReportPicker -> small options prop
The import boundary changes here. Moving the same JSX into another file without changing that boundary wouldn't be enough.
The server page can prepare the values the control needs. Here is a small page in app/reports/page.tsx:
import ReportPicker from "./ReportPicker";
import { reportHistory } from "./report-history";
export default function ReportsPage() {
const eventCount = reportHistory.reduce((sum, row) => sum + row.events, 0);
const options = [
{ id: "weekly", label: "Weekly report" },
{ id: "monthly", label: "Monthly report" },
];
return (
<main>
<h1>Reports</h1>
<p>{eventCount} events in the archived reports. Choose a period.</p>
<ReportPicker options={options} />
</main>
);
}
Keep the browser behavior in app/reports/ReportPicker.tsx:
"use client";
import { useState } from "react";
export default function ReportPicker({ options }: { options: { id: string; label: string }[] }) {
const [selected, setSelected] = useState(options[0]?.id ?? "");
return (
<label>
Period
<select value={selected} onChange={(event) => setSelected(event.target.value)}>
{options.map((option) => (
<option key={option.id} value={option.id}>
{option.label}
</option>
))}
</select>
</label>
);
}
The selector still needs JavaScript. The introduction doesn't need state just because it sits next to a selector. In the old arrangement, the client page imported reportHistory and performed the reduction in that client module. In this version, that import stays on the server; the browser receives the rendered summary and the two small options.
The teaching module in report-history.ts contains only three rows. In an application this could be an unnecessarily imported archive with many more rows. No byte saving is claimed for this tiny example. The specific path to inspect is page.tsx -> report-history.ts, and the production bundle decides whether it really carried the data before the move.
The example stops at selecting a value; fetching or rendering the chosen report is a separate behavior to add and check. Keep private report data out of a shipped JavaScript module in the first place. Moving a file later cannot recover data already delivered to a browser.
In a real page, pass the fields the control actually uses. Moving a component to the server but sending the full database result back as a prop can leave a large transfer, and may expose fields the browser never needed.
Measure more than the JavaScript file
Compare the production route before and after the change through CI. Record transferred JavaScript, parsed code, and the files loaded when the user first interacts. Keep cold and warm cache cases separate.
Use the same record for both runs: route /reports, before and after commit IDs, browser and CPU conditions, transferred client JavaScript, RSC payload bytes, and time until the first selector interaction responds. Store the bundle's import path beside those measurements. These fields are the comparison plan; there are no measured values here yet.
Also inspect the React Server Component payload. A page can send less JavaScript and still send a large serialized data tree. Look at which output actually became smaller before claiming a loading improvement.
Hydration is the browser work that makes the rendered controls interactive. A page can show text early and still take time before the selector responds. Try the interaction under a realistic CPU limit and check the browser's long tasks.
Don't decide from a development server. Development behavior and output include work that isn't representative of the production page. Use the built release you intend to ship and keep the comparison tied to its commit.
Check the behavior the move could break
Does the selector still have its label? Can it be used with a keyboard? Is the initial selection visible in the HTML? Does the page preserve the chosen value when the application needs it to?
For fixed options, the example's initial state is straightforward. If options later change after navigation or data refresh, decide what should happen to the current selection. When an API starts returning new values, use a compatible contract too; the old mobile client example shows why a new server and an old decoder need a rollout plan. A performance refactor shouldn't quietly reset user input or keep a value that is no longer available.
Keep server-only operations on the server. A database import that accidentally reaches a client module is more serious than a bundle-size issue. Use the framework's supported server-only checks, and review the fields crossing the boundary.
Stop when you know what changed
After narrowing the boundary, a heavy dependency may still be in another client component. Follow that import rather than repeating the same edit everywhere.
A local model is a larger version of this tradeoff. The WebLLM tagging feature separates model loading, generation, and the failure state so typing can remain useful.
Lazy loading can help when a control is rarely opened. It can also move the wait to the first click. Decide whether that tradeoff fits the feature, then measure the click as well as the initial load.
Compare the route again using the same browser conditions and the production commits from before and after the move. Did less JavaScript arrive? Does the selector respond sooner? If neither changed, follow the remaining client imports before moving more components.