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

Returning a string from a page component causes hydration mismatch error #65985

Closed
tom-sherman opened this issue May 20, 2024 · 6 comments
Closed
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@tom-sherman
Copy link

Link to the code that reproduces this issue

https://github.com/tom-sherman/next-text-page-hydration-mismatch

To Reproduce

  1. Return a string from a page component
  2. Request the route

Current vs. Expected behavior

Current: Hydration mismatch error in dev and prod builds.

Uncaught Error: Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:

- A server/client branch `if (typeof window !== 'undefined')`.
- Variable input such as `Date.now()` or `Math.random()` which changes each time it's called.
- Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.

It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.

https://react.dev/link/hydration-mismatch

  ...
    <OuterLayoutRouter parallelRouterKey="children" segmentPath={[...]} template={<RenderFromTemplateContext>} ...>
      <RenderFromTemplateContext>
        <ScrollAndFocusHandler segmentPath={[...]}>
          <InnerScrollAndFocusHandler segmentPath={[...]} focusAndScrollRef={{apply:false, ...}}>
            <ErrorBoundary errorComponent={undefined} errorStyles={undefined} errorScripts={undefined}>
              <LoadingBoundary hasLoading={false} loading={undefined} loadingStyles={undefined} ...>
                <NotFoundBoundary notFound={[...]} notFoundStyles={[...]}>
                  <NotFoundErrorBoundary pathname="/" notFound={[...]} notFoundStyles={[...]} asNotFound={undefined} ...>
                    <RedirectBoundary>
                      <RedirectErrorBoundary router={{...}}>
                        <InnerLayoutRouter parallelRouterKey="children" url="/" tree={[...]} childNodes={Map} ...>
+                         hi
-                         <text>

Expected: no error

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 20.9.0
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 9.1.0
Relevant Packages:
  next: 14.3.0-canary.72 // Latest available version is detected (14.3.0-canary.72).
  eslint-config-next: N/A
  react: 19.0.0-beta-04b058868c-20240508
  react-dom: 19.0.0-beta-04b058868c-20240508
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Runtime

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Vercel (Deployed), Other (Deployed)

Additional context

Not sure this is a huge issue as returning a string from a route shouldn't be used that much.

@tom-sherman tom-sherman added the bug Issue was opened via the bug report template. label May 20, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label May 20, 2024
@icyJoseph
Copy link
Contributor

I can't reproduce this. Does it happen in incognito mode as well? or maybe I started your project wrongly?

https://stackblitz.com/github/tom-sherman/next-text-page-hydration-mismatch?file=README.md

Screenshot 2024-05-20 at 16 56 18

@tom-sherman
Copy link
Author

Have you tried locally? Stackblitz isn't 100% accurate for streaming rendering.

@icyJoseph
Copy link
Contributor

Did that just now, and still works fine. Tried with canary on React 19 as well, no issue.

Could it be a Node.js version thing?

@tom-sherman
Copy link
Author

I narrowed it down to a specific chrome extension: https://chromewebstore.google.com/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh

I may close this then, but before doing so I'm going to investigate a bit more exactly what that extension is doing to cause the mismatch.

@icyJoseph
Copy link
Contributor

Probably edits the HTML with a pre tag or something like that

Copy link
Contributor

github-actions bot commented Jun 5, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Jun 5, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

2 participants