next.js: Cancel Rendering Route Error when Router Push hash

Verify canary release

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

Provide environment information

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.15.0
      npm: 8.5.5
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 12.1.7-canary.26
      react: 18.1.0
      react-dom: 18.1.0

What browser are you using? (if relevant)

Google Chrome Version 102.0.5005.61 (Official Build) (arm64)

How are you deploying your application? (if relevant)

Locally and Vercel

Describe the Bug

Similar to https://github.com/vercel/next.js/issues/36830 (fixed by https://github.com/vercel/next.js/pull/36828)

trying to store more information in hash, getting “Cancel rendering route” error when pushing updated hash to router:

image

Expected Behavior

Pages changes without error.

To Reproduce

git clone https://github.com/michalbundyra/hash-cancel-rendering-route
cd hash-cancel-rendering-route
yarn
yarn dev
  1. Go to http://localhost:3000/
  2. Click “to world”
  3. Error pops!

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 10
  • Comments: 23 (7 by maintainers)

Most upvoted comments

I’m using this workaround

router
  .replace( // or push or whatever you want
    {
      pathname: window.location.pathname,
      hash: newHash,
      query: window.location.search,
    },
    null,
    {
      shallow: true,
    }
  )
  .catch((e) => {
    // workaround for https://github.com/vercel/next.js/issues/37362
    if (!e.cancelled) {
      throw e
    }
  })

Any updates for this?

I’m going to use the following until there is a fix

import { useRouter } from 'next/router';

const router = useRouter();
const pathAndSlug= router.asPath.split('#')[0];
const newPath = `${pathAndSlug}#${hash}`;
  
window.location.replace(newPath);

Having the same error just by clicking quickly on nav links with hashtag hrefs. Seems that the error is triggered when the hash link is clicked before it fully scrolled up/down to the hash clicked before.

React 18.1 / Next 12.1.6

Adding router.isReady check worked for me:

  const router = useRouter();

  React.useEffect(() => {
    if (router.isReady) {
      router.replace(
        {
          hash: newHash,
        },
        null,
        {
          shallow: true,
        },
      );
    }
  }, [router]);

Also wondering if there’s a solution to this yet.

A standard accessibility practice is to have a link to the main tag of a page at the very top of a page’s body so that screen reader users can use it to skip over hearing the navigation sections at the beginning of a page over-and-over again. I’m having trouble doing that because of this issue.

I’ve given my main tag an id of “main” and am attempting to use the router to push the current page to it, and am getting the same error.

I’m getting this error with the following code:

       router.push(
            {
                pathname: '/',
                hash: 'hash',
            },
            undefined,
            { shallow: true }
        );

The hash bit seems to be the offender. Removing it removes the error.

If I switch from hash to query params, there is no error:

      router.push(
            {
                pathname: '/',
                query: { section: "section" },
            },
            undefined,
            { shallow: true }
       );

Versions: “react”: “18.2.0”, “next”: “12.2.5”,

any solution to this yet ? It comes after I upgraded react to 18 either with next version 12 or 13 error is appearing… there is no hash routing in my app and it is only happening for dynamic routes.