Skip to content

No next/link prefetch and client-side navigation for rewritten routes with query parameters #35696

@dklymenk

Description

@dklymenk

Verify canary release

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

Provide environment information

    Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT Mon, 21 Mar 2022 22:59:40 +0000
    Binaries:
      Node: 16.13.1
      npm: 8.1.2
      Yarn: 1.22.17
      pnpm: N/A
    Relevant packages:
      next: 12.1.3-canary.0
      react: 17.0.2
      react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

next start

Describe the Bug

If you have an optional catch-all route and use rewrites to convert query parameters into route parameters, links that have those query parameters don't automatically prefetch the props json even when the json is pregenerated at built time.

Expected Behavior

Prefetch and client-side navigation should work for links/routes with query params the same way as for any other internal link. No page reload, etc.

To Reproduce

I have prepared a minimal repository where the issue is reproduced. https://github.com/dklymenk/next-query-rewrite-router-issue

See my last commit to get the idea of what it takes to reproduce the issue starting from a clean template.

Here are some must-haves
  1. Delete the default index.tsx page.
  2. Create an optional catch-all page file [[...slug]] to catch our potential parameters.
  3. Add getStaticPaths and getStaticProps to enable SSG.
  4. Add some links with query params.
  5. Make sure you render the query param props on the page.
  6. Add a rewrite to next.config.js to transform query params into route params /?s=query1 to /query1, etc.

yarn build, yarn start and go to localhost:3000.

You can open dev tools on the network to see that there is no prefetch happening and if you click on any of the links with query params the page will reload (no client-side navigation).

Interestingly enough, if you remove the / on the href, the preload works (you can see json preloaded on network tab), but clicking on the link seems to load only the index.json (props for the case with no query params).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.lockedstaleThe issue has not seen recent activity.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions