New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next/dynamic
SSR is broken when not using a custom Document
, or using a Document
with getInitialProps
#45151
Comments
Hi @sebpettersson, thanks a lot for making this issue. We've been trying to fix this bug since a week ago. I tried to reproduce the issue with Looks like @huozhi is already working on the PR to fix this. Thanks for working on it! |
I think this also causes a problem when using a dynamic component and attempting to compile down to static.. You don't actually get the compiled HTML. Here is what I think may also be related as in it might also be a side effect of something similar |
## Bug Previously the `React.lazy` and Loadable preloading are creating different module loading promises with loader. Now we change to wait the loader in `React.lazy` to make sure for SSR case they're preloaded. The case to trigger this bug is: When adding `Document.getInitialProps` (aka. gIP), rendering goes to process the gIP first which would make the lazy elements executed before gIP, then preloading will happen after lazy which leads to Suspense resolves too fast without content. Fixes #45151 - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
## Issue To address the problem that we introduced in 13.0.7 (#42589) where we thought we could use same implementation `next/dynamic` for both `pages/` and `app/` directory. But it turns out it leads to many problems, such as: * SSR preloading could miss the content, especially with nested dynamic calls * Closes #45213 * Introducing suspense boundary into `next/dynamic` with extra wrapped `<Suspense>` outside will lead to content is not resolevd during SSR * Related #45151 * Closes #45099 * Unexpected hydration errors for suspense boundaries. Though react removed this error but the 18.3 is not out yet. * Closes #44083 * Closes #45246 ## Solution Separate the dynamic implementation for `app/` dir and `pages/`. For `app/` dir we can encourage users to: * Directly use `React.lazy` + `Suspense` for SSR'd content, and `next/dynamic` * For non SSR components since it requires some internal integeration with next.js. For `pages/` dir we still keep the original implementation If you want to use `<Suspense>` with dynamic `fallback` value, use `React.lazy` + `Suspense` directly instead of picking up `next/dynamic` * Closes #45116 This will solve various issue before react 18.3 is out and let users still progressively upgrade to new versions of next.js. ## Bug Fix - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
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.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
Document
is used the, markup is rendered correctly - provided it is a functional component that doesn't usegetInitialProps
. If it is aclass
that extendsDocument
fromnext/document
, or a functional component that usesgetInitialProps
, the bug is still triggered.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
The text was updated successfully, but these errors were encountered: