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

router asPath hydration mismatch when rewrite is applied #34197

Closed
BRKalow opened this issue Feb 10, 2022 · 4 comments · Fixed by #34205
Closed

router asPath hydration mismatch when rewrite is applied #34197

BRKalow opened this issue Feb 10, 2022 · 4 comments · Fixed by #34205

Comments

@BRKalow
Copy link
Contributor

BRKalow commented Feb 10, 2022

Run next info (available from version 12.0.8 and up)

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
Binaries:
  Node: 14.17.6
  npm: 8.1.3
  Yarn: 1.22.17
  pnpm: 6.24.0
Relevant packages:
  next: 12.0.10
  react: 17.0.2
  react-dom: 17.0.2

What version of Next.js are you using?

12.0.10

What version of Node.js are you using?

14.17.6

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

When navigating to a route which has been rewritten and using asPath during SSR, a hydration mismatch occurs, specifically when using asPath to create an href to be passed to next/link.

Rewrite config: https://github.com/BRKalow/next-rewrite-hydration-mismatch/blob/main/next.config.js

Rewritten page: https://github.com/BRKalow/next-rewrite-hydration-mismatch/blob/main/pages/proxied/index.jsx

On initial load, the anchor tag rendered by next/link has an href of /proxied, whereas I would expect it to have an href of /

Expected Behavior

No hydration mismatch, the router state during SSR should ideally match the URL as reflected in the request. On initial load, the anchor tag rendered by next/link should have an href of /.

To Reproduce

https://github.com/BRKalow/next-rewrite-hydration-mismatch

@BRKalow BRKalow added the bug Issue was opened via the bug report template. label Feb 10, 2022
@BRKalow BRKalow changed the title router asPath hydration mismatch router asPath hydration mismatch when rewrite is applied Feb 10, 2022
@BRKalow
Copy link
Contributor Author

BRKalow commented Feb 10, 2022

Potential duplicate of #34144

@ijjk
Copy link
Member

ijjk commented Feb 10, 2022

Hi, so this is currently expected as the page being rewritten to is an automatically statically optimized page so it can't know the asPath on the server and only knows it on the client. The asPath value shouldn't be used inside of the rendering tree until router.isReady is true. We might need to document this better cc @molebox

Also note using typeof window in the rendering tree (related line) will cause hydration issues and needs a isMounted check before differing the rendering based on this

@BRKalow
Copy link
Contributor Author

BRKalow commented Feb 10, 2022

@ijjk Yeah I just had this realization regarding static generation 😅 . Definitely agreed there could be potential for docs improvement here, or even a development warning if asPath is used during render on the server.

I was just using the typeof window check so that I could showcase the difference of the href attribute, but yeah that's definitely a no-go generally.

Thanks for taking a look!

@ijjk ijjk added area: documentation and removed bug Issue was opened via the bug report template. labels Feb 10, 2022
@kodiakhq kodiakhq bot closed this as completed in #34205 Feb 11, 2022
kodiakhq bot pushed a commit that referenced this issue Feb 11, 2022
## Description

According to `Next.js` useRouter documentation:

- `asPath`: The path (including the query) shown in the browser without the configured basePath or locale.
- `pathname`: Current route. That is the path of the page in /pages, the configured basePath or locale is not included.

`asPath` should not be used as the props of components. There are many cases that `asPath` not working as expected. For example:

- `asPath` is different on server-side and client-side.
- `asPath` can contains `id` and `query`.

## Suggestion

- Warning the use of `asPath` can lead to the conflict of client and server-side.
- Update `useRouter` document.

## Bug

- [x] Related issues linked using `fixes #number`

Fixes: #34144
Fixes: #34016
Fixes: #34197
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2022
slukin1 pushed a commit to slukin1/tempppp that referenced this issue Jul 17, 2023
## Description

According to `Next.js` useRouter documentation:

- `asPath`: The path (including the query) shown in the browser without the configured basePath or locale.
- `pathname`: Current route. That is the path of the page in /pages, the configured basePath or locale is not included.

`asPath` should not be used as the props of components. There are many cases that `asPath` not working as expected. For example:

- `asPath` is different on server-side and client-side.
- `asPath` can contains `id` and `query`.

## Suggestion

- Warning the use of `asPath` can lead to the conflict of client and server-side.
- Update `useRouter` document.

## Bug

- [x] Related issues linked using `fixes #number`

Fixes: vercel/next.js#34144
Fixes: vercel/next.js#34016
Fixes: vercel/next.js#34197
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants