AI intelligent summary
GPT
The article explains how to use bundle-analyzer to analyze and optimize the size of Next.js packages to improve webpage loading speed, covering its usage, analyzing module dependencies, and providing optimization guidance, including reducing the size of the Clerk package and namespace objects.
This article introduces how to use bundle-analyzer to inspect and optimize the bundle size built with Next.js, thereby improving webpage loading speed. It also covers the usage of
bundle-analyzer, analyzes the size of each module or dependency in the Next.js project, and provides guidance on optimizing the bundle size. Additionally, it addresses the issue of excessive space occupied by the Clerk package and namespace objects, and how to shave off the package size of non-critical functions through dynamic imports.Introduction
This article uses this blog site, which is built with Next.js and delivered through Cloudflare CDN, as a case study to document its optimization process. For more in-depth and authoritative guidelines, please consult the following link
The specific solutions provided may be subject to limitations due to the underlying framework, website architecture, or my technical level. Your understanding is appreciated, and feel free to point out any inaccuracies.
The significance of optimization varies
There is a complete difference between reducing 10ms and 1000ms of page loading time with the same amount of work. Over-optimization will reduce the efficiency of development.
Whether to follow the guidelines
Not following some guidelines has little to no negative effect, but some can even result in noticeable delay.
Any optimization methods should ultimately be guided by experiments and actual statistical data.
According to bundle-analyzer results, a significant number of components are large or dynamically imported, which suggests lazy or on-demand loading.
Install and configure bundle-analyzer
Run ANALYZE=true yarn build to inspect the bundle size
According to the figure below, the
Clerk package and the namespace object consume the most space in the bundle.
Each rectangle represents a module or dependency, and its size reflects the relative footprint in the bundle. The larger the rectangle, the more space the module occupies in the final build, vice versa
Optimization of the Clerk dependency size
- Analysis: Clerk is an optional feature in the project, but it is currently statically imported, which means it gets bundled regardless of whether it’s used.
- Solution: Use on-demand dynamic import, i.e., load it conditionally based on configuration settings such as environment variables.
Optimizing the Namespace Object
- Analysis: The namespace object holds resources used for dynamic imports. Its large size results from numerous theme-related modules that could potentially be switched to. Even if they aren't included in the final bundle, the reference mappings alone occupy significant space.
- Solution: Remove theme-related resources that are not meant to be used, and consider discarding them entirely.
Dynamic Imports and Lazy Loading
For non-critical, heavy resources that are not needed on the initial screen, optimizing bundle size by loading modules only based on size or specific conditions is recommended.
- Basic dynamic import
This involves converting static imports into dynamic imports, but the module will still be loaded regardless.
Example:
<ComponentA />- Conditional dynamic import
This converts static imports to dynamic imports that load only when certain conditions are met.
Example:
{showMore && <ComponentB />}<input type="text" placeholder="Country search..." className={styles.input} onChange={async (e) => { const { value } = e.currentTarget; // Dynamically load libraries const Fuse = (await import('fuse.js')).default; const _ = (await import('lodash')).default; const fuse = new Fuse(countries, { keys: ['name'], threshold: 0.3, }); const searchResult = fuse.search(value).map((result) => result.item); const updatedResults = searchResult.length ? searchResult : countries; setResults(updatedResults); // Fake analytics hit console.info({ searchedAt: _.now(), }); }} />
Not at the moment
Main Reference Not available
acceptable become a member Telegram's Little Warehouse of Seven Rows | Internet Memories | Blogs cap Seven lines Technical Exchange Group Find more tips oh 🥰 and also discuss various issues in the chat group ❓
Welcome to the Bottom comment section. Share your thoughts and experiences with Let's discuss and improve together!
- Author:Qi Xing
- URL:https://blog.qixing1217.top/article/optimize-nextjs-bundle-size-with-bundle-analyzer
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!







