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.

v12.2 up to v12.3.2-canary.22 handleHardNavigation Invariant: attempted to hard navigate to the same URL

See original GitHub issue

Verify canary release

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

Provide environment information

Deployed to Vercel

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.13.1
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 12.3.2-canary.22
      eslint-config-next: 12.3.1
      react: 18.2.0
      react-dom: 18.2.0

What browser are you using? (if relevant)

Not relevant

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

It seems that since Next.js v12.2 the internal router does an extra fetch for the current page.js. This didn’t happen in the older versions of Nextjs (see screenshots below). If the current URL is a dynamic route and is rewritten (our case in Middleware) the extra page.js fetch will result in a 404. This causes an attempt to reload the current page which then runs into the handleHardNavigation error Invariant: attempted to hard navigate to the same URL.

Current production Next.js v12.1: image

Develop Next.js v12.2: image image

Expected Behavior

Should just load the page without errors.

Link to reproduction

https://router-3zllgxvkw-fdmediagroep.vercel.app/nieuws

To Reproduce

Minimal reproduction repo: https://github.com/FDMediagroep/router-bug

Next Middleware rewrite rules:

    ...
    {
      source: /^\/nieuws$/,
      destination: '/nieuws/overview/1',
    },
    {
      source: /^\/nieuws\?page=(?<pageNr>\d+)/gi,
      destination: '/nieuws/overview/$<pageNr>',
    },
    ...

Navigating to /nieuws will result in the error as described in the bug description for Next.js since v12.2. But not on the older version v12.1.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:60
  • Comments:57 (13 by maintainers)

github_iconTop GitHub Comments

22reactions
willemliufdmgcommented, Oct 5, 2022

This issue has been closed but the problem has not been resolved.

22reactions
bramvdpluijmcommented, Jul 18, 2022

Currently experiencing this issue as well when not using any _middleware.

Read more comments on GitHub >

github_iconTop Results From Across the Web

attempted to hard navigate to the same URL - PullAnswer
This causes an attempt to reload the current page which then runs into the handleHardNavigation error Invariant: attempted to hard navigate to the...
Read more >
Next.js by Vercel - The React Framework
Production grade React applications that scale. The world's leading companies use Next.js by Vercel to build static and dynamic websites and web ...
Read more >
v12.2 up to v12.3.2-canary.22 handleHardNavigation Invariant
This causes an attempt to reload the current page which then runs into the handleHardNavigation error Invariant: attempted to hard navigate to the...
Read more >
Next.js: packages/next/shared/lib/router/router.ts | Fossies
1 // tslint:disable:no-console 2 import type { ComponentType } from 'react' ... new Error( 829 `Invariant: attempted to hard navigate to the same...
Read more >
Android Studio 3.2 Canary 14 error - Stack Overflow
Had the same issue - you need to update your Canary build - Currently 3.2 canary 18 as of time of writing. Also,...
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