question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`query` not updating on `shallow` push when middleware is defined

See original GitHub issue

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: #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022 Binaries: Node: 14.18.1 npm: 6.14.15 Yarn: 1.22.17 pnpm: N/A Relevant packages: next: 12.2.2 eslint-config-next: N/A 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)

No response

Describe the Bug

In v12.2.2, when pushing a route using shallow: true within a catchAll route and with a middleware defined, query is not updated.

Say we’ve got the /pages/[...catalog].tsx page, we are on /shoes and we want to go to /shoes/black.

// /middleware.ts

export function middleware() {}

// /pages/[...catalog].tsx

const CatalogPage = () => {
  const { query, push } = useRouter();
  return (
    <>
      <button
        onClick={() => {
          push("/[...catalog]", "/shoes/black", { shallow: true });
        }}
      >
        Push
      </button>
      <pre>{JSON.stringify({ query }, null, 2)}</pre>
    </>
  );
};

When clicking the button, query will not be updated regardless of the path changing.

Expected Behavior

It is expected that query is updated after pushing the new route, as per the documentation.

Link to reproduction

https://codesandbox.io/s/cranky-burnell-295k32

To Reproduce

  1. After loading the Sandbox, click on the Go to shoes link. It should take you to /shoes. There you can see the actual value for query, which should be query.catalog = ["shoes"]
  2. Now click the push button, which will set the url to /shoes/black. You’ll see that the query value stays the same, when it should’ve been query.catalog = ["shoes", "black"].
  3. Reload the page from within the Sandbox browser, and you’ll see the correct expected value for query
  4. Now change the middleware.js filename to something else like __middleware.js, so it doesn’t get picked up by next.
  5. Go back to /shoes
  6. Click the push button again, and you’ll see that this time the query value gets correctly updated

Edit 1

It seems like regular dynamic routes are also affected by this issue. See the updated reproduction including regular dynamic routes.

In v12.2.2, when pushing a route using shallow: true within a catchAll dynamic route and with a middleware defined, query is not updated.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:9
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ijjkcommented, Jul 28, 2022

Hi, this has been updated in v12.2.4-canary.3 of Next.js, please update and give it a try!

0reactions
github-actions[bot]commented, Aug 27, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shallow Routing - Next.js
When shallow routing is used with middleware it will not ensure the new page matches the current page like previously done without middleware....
Read more >
Routes.push() not working as expected at NextJS
I am trying to redirect the user to the homepage when he tries to access the dashboard of the app without being authenticated,...
Read more >
Controllers - Laravel - The PHP Framework For Web Artisans
Controllers are not required to extend a base class. However, you will not have access to convenient features such as the middleware and...
Read more >
Understanding Next.js routeChangeStart and router events
Nested routing in Next.js; Dynamic routing in Next.js; Shallow routing ... will be pushed by the router as a property of the query...
Read more >
Redux Essentials, Part 8: RTK Query Advanced Patterns
Updating the Edit Post Form​. As with adding posts, the first step is to define a new mutation endpoint in our API slice....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found