Skip to content

Refactor component imports to use dynamic loading in layout and MDXComponents#8373

Open
MaxwellCohen wants to merge 1 commit intoreactjs:mainfrom
MaxwellCohen:dynamic-loading
Open

Refactor component imports to use dynamic loading in layout and MDXComponents#8373
MaxwellCohen wants to merge 1 commit intoreactjs:mainfrom
MaxwellCohen:dynamic-loading

Conversation

@MaxwellCohen
Copy link

@MaxwellCohen MaxwellCohen commented Mar 22, 2026

I noticed that src/components/Layout/Page.tsx and src/components/MDX/MDXComponents.tsx import several components that aren't used on every page, resulting in unnecessary JS on initial page load. Leveraging next.js's dynamic import provides a simple way to lazy-load, and Webpack/Turbopack will tree-shake or bundle excess JavaScript from other files, so only what is needed is bundled for each page.

Since rect.dev uses HTTP/2, which supports multiplexing for serving JS files; we can afford to have multiple smaller JS files vs. HTTP/1, which is limited to about 6 downloads at a time.

This PR results in about 70KB of initial pageload savings.

@github-actions
Copy link

Size changes

Details

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

⚠️ Global Bundle Size Increased

Page Size (compressed)
global 115.61 KB (🟡 +482 B)
Details

The global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

If you want further insight into what is behind the changes, give @next/bundle-analyzer a try!

Seven Pages Changed Size

The following pages changed size from the code in this PR compared to its base branch:

Page Size (compressed) First Load
/404 56.1 KB (🟢 -70.91 KB) 171.71 KB
/500 56.11 KB (🟢 -70.91 KB) 171.72 KB
/[[...markdownPath]] 59.61 KB (🟢 -69.85 KB) 175.22 KB
/_error 192 B (🟡 +2 B) 115.8 KB
/errors 56.46 KB (🟢 -70.81 KB) 172.07 KB
/errors/[errorCode] 56.44 KB (🟢 -70.8 KB) 172.05 KB
/llms.txt 256 B (🟡 +3 B) 115.86 KB
Details

Only the gzipped size is provided here based on an expert tip.

First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If next/link is used, subsequent page loads would only need to download that page's bundle (the number in the "Size" column), since the global bundle has already been downloaded.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 10% or more, there will be a red status indicator applied, indicating that special attention should be given to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant