Question presented to candidate: "Our dashboard page includes a heavy chart editor that most users never open. How does Next.js split code, and how would you keep the editor out of the initial bundle?"
What a strong answer should cover:
- Next.js splits JavaScript per route automatically, and Server Components add no client JavaScript at all.
next/dynamic(built onReact.lazyand Suspense) loads a Client Component in its own chunk when it is first rendered.- Options:
loadingfor a placeholder,ssr: falseto skip server rendering for browser-only components. ssr: falseis only allowed in Client Components; in a Server Component the build fails.- You can also
await import()a library inside an event handler to load it on demand.
Clarifying questions expected:
- "Is the component needed for the first view?" β if not, lazy load it.
- "Does it use browser-only APIs?" β then
ssr: false.
Code / implementation expected: Yes β a toggle that lazy loads a heavy component.