{"success":true,"name":"lazy-load-heavy-client-widgets-with-next-dynamic","description":"Adding a heavy client-side library to a Next.js page","category":"ai-brimind","path":"ai-brimind/lazy-load-heavy-client-widgets-with-next-dynamic/SKILL.md","file":"SKILL.md","content":"---\nname: lazy-load-heavy-client-widgets-with-next-dynamic\ndescription: \"Adding a heavy client-side library to a Next.js page\"\n---\n\n# Lazy-Load Heavy Client Widgets With next/dynamic\n\n## When to use\nProject is Next.js, and you are about to import a big client-only library (chart, editor, map, 3D) directly into a page.\n\n## Example request\nimporting a full chart library at the top of the dashboard page → every visitor downloads it before seeing anything; next/dynamic loads it only when rendered. — Split the heavy leaf. | a map widget that crashes on the server because it touches window → dynamic(() => import(...), { ssr: false }) keeps it client-only, scoped to that one widget. — Narrow ssr:false. | wrapping the whole page in dynamic ssr:false to fix one widget → the entire page loses server rendering for one component's problem. — Blast radius too big. | a modal or below-the-fold editor used by 5% of visitors → load it on interaction, not in the initial bundle. — Pay only when used.\n\n## Instructions\nAct like a bundle accountant who asks 'does every visitor pay for this?' before each heavy import in Next.js. When adding a large client-side library: 1) Estimate the cost and usage: is this widget on every visitor's critical path, or used by a minority (modals, editors, admin charts, maps)? If minority or below the fold, it should not sit in the initial bundle. 2) Use the built-in code splitting: const Chart = dynamic(() => import('./Chart')) loads the component only when it actually renders; add a small loading placeholder so layout stays stable. For libraries that touch window during import, add { ssr: false } — but scope it to the smallest wrapper component, never the page, so the rest keeps server rendering. 3) Check the import chain: a heavy library imported by a shared utils file leaks into every page that imports the utils. Keep heavy imports inside the component that uses them. 4) Before adding the library at all, ask whether something already present covers the need — a second chart or date library in one project is a smell. 5) If unsure whether an import is heavy, check the bundle analyzer or the package size instead of guessing. Only ship the import once you can say who pays for it and when it loads, and explain why the dynamic boundary sits at that exact component — not just that the feature renders.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #508\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}