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.query is empty in catch all routes when middleware, basePath and trailingSlashes are used. #45340

Open
1 task done
Escapado opened this issue Jan 27, 2023 · 0 comments
Labels
bug Issue was opened via the bug report template.

Comments

@Escapado
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #22 SMP Tue Jan 10 18:39:00 UTC 2023
Binaries:
  Node: 16.17.0
  npm: 8.15.0
  Yarn: 1.22.19
  pnpm: 7.1.0
Relevant packages:
  next: 13.1.5
  eslint-config-next: 13.1.5
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/adoring-diffie-lllgcy?selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A20%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A20%7D%5D&file=%2Fnext.config.js

To Reproduce

  1. Open Codesandbox
  2. In the preview URL bar change the URL from
    https://lllgcy-3000.preview.csb.app/ to https://lllgcy-3000.preview.csb.app/de-de/some/.
  3. Watch the console output, or the render output - a double render occurs, first with route.query === {slug: "some"} and on the second render with route.query === {}.

Describe the Bug

When using the new middleware with dynamic routes and a config with a basePath other than "/" and trailingSlash option set to true all dynamic pages render twice, with some delay between the renders, but on the second render the router.query object is empty.
This is true regardless of whether a useRouter hook or the withRouter HOC is used. Interestingly enough the rest of the router object looks very similar on the second render so asPath, basePath, pathname and route are preserved but query is not.

  • The bug goes away when removing/renaming the middleware.
  • The bug goes away when setting trailingSlash to false.
  • The bug goes away when setting the basePath:"/" or removing the option entirely
  • The bug does not happen on non-dynamic routes.

There is a closed issue that might be related to this, but the linked PR does not fix this issue.

Expected Behavior

I would not expect a double render to occur.
And in case there is a technical reason to re-render I would expect the second render to preserve the router.query object.

In our project we use the basePath for i18n naming conventions, we can't disable the trailingSlash option, we need dynamic routes for our product catalogue and we need middleware for multiple things so we can not work around this problem easily and so we can't migrate to Next 13.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@Escapado Escapado added the bug Issue was opened via the bug report template. label Jan 27, 2023
ijjk added a commit that referenced this issue Jan 30, 2023
This ensures we don't apply the trailing slash redirect for `_next/data`
requests as it can cause props to fail to resolve on client transition.
This also fixes `missing` fields not being applied correctly for
`headers` and `redirects` as the field wasn't being passed through.

## Bug

- [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)

Closes: #45398
Fixes: #45393
x-ref: #45340
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

1 participant