Skip to content

next/dynamic SSR is broken when not using a custom Document, or using a Document with getInitialProps #45151

Description

@sebpettersson

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:03:51 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T6000
Binaries:
  Node: 18.12.0
  npm: 8.19.2
  Yarn: N/A
  pnpm: 7.12.2
Relevant packages:
  next: 13.1.5-canary.2
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

Dynamic imports (next/dynamic)

Link to the code that reproduces this issue

https://github.com/sebpettersson/next-dynamic-bug

To Reproduce

Import a component with next/dynamic, build the project and then check the rendered markup.

next-dynamic-bug

https://next-dynamic-bug.vercel.app/

Describe the Bug

When using a component imported with next/dynamic, the markup will contain <template> instead of correct SSR content.

NOTE: If JavaScript is enabled it is easy to miss this bug since the <template> will be switched out for the correct content.


Additional findings

  1. This bug was introduced in 13.0.7.
  2. If a custom Document is used the, markup is rendered correctly - provided it is a functional component that doesn't use getInitialProps. If it is a class that extends Document from next/document, or a functional component that uses getInitialProps, the bug is still triggered.
  3. In dev mode, the bug is only triggered the first time the page is loaded. On subsequent page loads the correct markup is output.

Expected Behavior

That server side rendered content contains the correct markup when using components imported with next/dynamic.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Metadata

Metadata

Assignees

Labels

Lazy LoadingRelated to Next.js Lazy Loading (e.g., next/dynamic or React.lazy).linear: nextConfirmed issue that is tracked by the Next.js team.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions