Skip to content

Hash change re-renders the entire app, _app, page and components involved #34729

@gurkerl83

Description

@gurkerl83

Verify canary release

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

Provide environment information

Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64
Binaries:
Node: 16.13.0
npm: 8.1.0
Yarn: 3.2.0
pnpm: N/A
Relevant packages:
next: 12.1.0
react: 18.0.0-rc.0
react-dom: 18.0.0-rc.0

What browser are you using? (if relevant)

Chrome 101.0.4899.0 same in FF and Safari

How are you deploying your application? (if relevant)

next dev, next start

Describe the Bug

Navigation to anchors should not cause any re-render; however, changing the hash through NextJs building blocks Link and Router does cause a re-render.

Re-rendering an entire app from _app and the respective page with the page's content can get quite substantial.

E.g., in our case, MDX and quite large React components get re-rendered. On the other hand, the re-rendering leads us to identify a hard-to-track problem in loading MDX components, but this is off-topic to the current issue.

Looking at the sources, it seems the function of the router does not lead to a state change, which causes a re-rendering because the operations target Html primitives primarily. Logging the hashChangeStart and hashChangeComplete events seems something is causing a re-render afterward; maybe a new page-props object gets created, but this is just a vague assumption.

Another thing we see is when triggering an identical hash change several times using NextJs Link and Router primitives; the re-rendering occurs multiple times, one for each interaction. In this case, the browser history seems to be intact; a second click on the same link does not cause a push of another state to browser history; the browser's back button works; in any case, a re-render of the app gets executed.

Expected Behavior

Do not perform re-rendering if it is not needed.

To Reproduce

The sources of the reproducer are here. https://github.com/gurkerl83/NextJs-Hash-Bug

  1. Clone the repo
  2. Install dependencies
  • yarn
  1. Run development respectively production
  • yarn dev
  • yarn start
  1. Follow the instructions in the apps index page and see the console / http://localhost:3000

The link to the deployed version
https://next-js-hash-bug.vercel.app

Thx!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Linking and NavigatingRelated to Next.js linking (e.g., <Link>) and navigation.bugIssue was opened via the bug report template.lockedstaleThe issue has not seen recent activity.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions