Skip to content
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

Using Preact, some components get rendered multiple times as duplicates #19399

Closed
jackyef opened this issue Nov 21, 2020 · 7 comments · Fixed by #19442
Closed

Using Preact, some components get rendered multiple times as duplicates #19399

jackyef opened this issue Nov 21, 2020 · 7 comments · Fixed by #19442
Assignees
Labels
kind: bug Confirmed bug that is on the backlog
Milestone

Comments

@jackyef
Copy link
Contributor

jackyef commented Nov 21, 2020

Bug report

Describe the bug

When using Preact (via next-plugin-preact), some components get rendered multiple times as duplicates.

Unfortunately, I still haven't found the root cause. The only information I have right now is that the issue doesn't occur if I use React instead.

To Reproduce

Please see the following video (it's only about 1 minute) to see how to reproduce it.
https://streamable.com/nbkpuz

For references:

  1. This is the site that is using preact in the video:
  2. This is the site that is using react:

Expected behavior

Using Preact should have the same result with using React.

System information

  • OS: Windows
  • Browser: Chrome
  • Version of Next.js: 10.0.2
  • Version of Node.js: 12.18.0
  • Deployment: Vercel

Additional context

Add any other context about the problem here.

The duplicated component was rendered lazily using a combination of IntersectionObserver and next/dynamic. The code for the component can be seen here and it is rendered here. The component basically fetches data from an API using react-query and then renders some UI.

@jackyef jackyef added the template: bug A user has filled out the bug report template. Issue needs triaging label Nov 21, 2020
@marvinhagemeister
Copy link

Had a quick look at this and it's caused by calling hydrate() on a a previously rendered result that doesn't match what's in the virtual tree. The widget where the duplication happens is lazily loaded, but the DOM nodes of a previously loaded instance is already present in the DOM. When replacing next.js' hydrate() call with render() everything works.

Firing up a codesandbox it seems like React removes DOM nodes that are not in the virtual tree on hydration whereas Preact leaves them intact.

@srdjan
Copy link

srdjan commented Nov 21, 2020

that seems to be the same problem as preactjs/preact#2838

@jackyef
Copy link
Contributor Author

jackyef commented Nov 22, 2020

@marvinhagemeister @srdjan Thanks for taking a look!

Interesting, I tried downgrading to next@10.0.1 and the problem no longer occurs even when I am using preact. It seems like previously, hydrate is only used for initial render, but since next@10.0.2 page navigations also call hydrate instead of render. Was this intended to help improve the performance on page navigation? cc: @Timer

@AlexMeah
Copy link

Also seeing incorrect page content with Preact in 10.0.2. Seems like dom elements aren't correctly cleaned up on route change, downgrading to 10.0.1 fixes the issue.

@developit
Copy link
Contributor

It seems odd to use hydrate() as a way to update existing rendered trees - if that works it's certainly relying on unspecified behavior in react.

@Timer Timer added kind: bug Confirmed bug that is on the backlog priority: p0 and removed template: bug A user has filled out the bug report template. Issue needs triaging labels Nov 23, 2020
@Timer Timer self-assigned this Nov 23, 2020
@Timer Timer added this to the iteration 13 milestone Nov 23, 2020
@Timer Timer added the point: 2 label Nov 23, 2020
@kodiakhq kodiakhq bot closed this as completed in #19442 Nov 23, 2020
kodiakhq bot pushed a commit that referenced this issue Nov 23, 2020
@Timer
Copy link
Member

Timer commented Nov 23, 2020

Fixed in next@^10.0.3-canary.1.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Confirmed bug that is on the backlog
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants