Suspense rendering problems with Next.js
See original GitHub issue- Check if updating to the latest Preact version resolves the issue
Using Next.js (SSR) -> package.json:
"next": "12.1.2",
"next-plugin-preact": "3.0.6",
"preact": "10.7.0",
"preact-render-to-string": "5.1.20",
"react": "npm:@preact/compat@17.0.3",
"react-dom": "npm:@preact/compat@17.0.3",
Describe the bug
Note the different html elements, styles applied on them and how they get rendered. The Content
component just does a SWR api call with 2s sleep so suspense can get triggered.
isClient
is used to use suspense once on the client since (afaik) Suspense does not work on the server.
- Same html element:
Let’s say I have the following content on the page:
{isClient() ? (
<Suspense fallback={<h1 style={{ color: 'red' }}>Fallback</h1>}>
<Content>
<h1 style={{ color: 'blue' }}>Content</h1>
</Content>
</Suspense>
) : (
<h1 style={{ color: 'green' }}>Else</h1>
)}
- retrieved from the server: “Else” colored green (as expected)
- rendered while loading: “Else” colored in green (???)
- rendered when suspense done: “Content” colored in green (???)
- Different html elements:
{isClient() ? (
<Suspense fallback={<h1 style={{ color: 'red' }}>Fallback</h1>}>
<Content>
<h2 style={{ color: 'blue' }}>Content</h2>
</Content>
</Suspense>
) : (
<h3 style={{ color: 'green' }}>Else</h3>
)}
- retrieved from the server: “Else” colored green, h3 tag (as expected)
- rendered while loading: “Else” colored green, h3 tag (???)
- rendered when suspense done: “Content” colored blue, h2 element (as expected), but there is also “Else” colored green, h3 tag (???) in the DO
Bugs
- if the same html element is used as content, preact suspense just patches the existing html element (from the else part) which applies wrong html properties (style, classname,…)
- if different html elements are used, the “else” part persists in the DOM
- suspense fallback never renders
Expected behavior Suspense should work as expected: should render the fallback & should re-render(?) the consuming component once the suspended request is finished.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How to use React Suspense in Next.js - Learn JSX
How to use React Suspense in Next.js. ... works the same as the componentDidCatch to handle errors in case the rendering has some...
Read more >Streaming and Suspense - Data Fetching - Next.js beta docs
These steps are sequential and blocking, meaning the server can only render the HTML for a page once all the data has been...
Read more >Next.js server-side data fetching within `Suspense ...
You can theoretically have a Suspense component in a page component, but it will be somewhat worthless. You won't get that page in...
Read more >Suspense - SWR
When using suspense mode on the server-side (including pre-rendering in Next.js), it's required to provide the initial data via fallbackData or fallback. This ......
Read more >Impact of React Suspense used with Nextjs on SEO? - Reddit
I have to build many Nextjs sites, and even though I have ... I assume that Suspense is making the page loading faster...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this issue presents itself because we stay in hydration mode where we don’t patch missmatches in DOM-attributes as this is commonly seen as a bug between
Server
andClient
.So DOM arrives from server as
then we switch to
hydrate()
and enter theisClient()
branch, this suspends which makes us getWe hydrate but don’t patch the innerText nor the style, nor the nodeType. When the Suspense resolves we see that the DOM still remains the same on
We yet again haven’t patched any of the needed things, I think this is because of our deferred hydration tricks we use when we are revolving around Suspending boundaries because we stay in hydration mode even though we will have to patch the
nodeType
, … ideallyWDYT @marvinhagemeister @developit
@Alex289 can you open a new issue for that? It seems like the error you’re seeing is not related to the issue here.