vercel / next.js

The React Framework
https://nextjs.org
MIT License
122.95k stars 26.27k forks source link

Server rendered routes with hashes do not work with browser back/forward navigation #56112

Open benhonda opened 9 months ago

benhonda commented 9 months ago

Link to the code that reproduces this issue

https://github.com/benhonda/nextjs-router-bug

To Reproduce

  1. Go to https://nextjs.org/docs
  2. Click on any of the "hashed" routes (located in the right-hand sidebar on desktop screens)
  3. Navigate to a different page by clicking on a page route (located in the left-hand sidebar on desktop screens)
  4. Click the back button on your browser. The URL changes and the page may scroll, but the page content remains from Step 3.

Current vs. Expected behavior

Currently, the URL changes and the page may scroll, but the page content does not change.

I expect the page to navigate back to the previous content (as specified in the URL) and for my scroll position to be at the specified hash.

Verify canary release

Provide environment information

OS:
MacOS Monterey 12.6.8
Intel chip

Tested browsers:
Firefox 117.0.1, Chrome 117.0.5938.92, Safari 16.5.2

Which area(s) are affected? (Select all that apply)

App Router

Additional context

The "Link to the code that reproduces this issue" just leads to an empty template but I have a good excuse - I swear! I came across this issue in my local development, where I needed to set hash links to footnotes for A11y purposes, however since the issue also appears to be present on the nextjs docs site, I decided to just use that as my example for convenience.

stijnwesterhof commented 9 months ago

I don't see this issue happening on the docs, but I do experience this in my app. Currently also looking for a solution...

evoX0 commented 6 months ago

Same here next 14.0.1

evoX0 commented 5 months ago

Is there any workaround or smth we can use to avoid this issue? thanks

bircha commented 4 months ago

I can reproduce the bug in our project. Any plans for a fix?

itsjavi commented 3 months ago

I cannot reproduce it anymore on the docs page. My problem is similar (using Next.js 14 app router):

Behavior:

Desired behavior:

arshseraphic commented 1 month ago

I have similar issue, using next 14.2.3 App Router.

I am on page

https://www.bettingtipsafrica.com/betting-sites/

then i click Countries in right sidebar which take me to hash link

https://www.bettingtipsafrica.com/betting-sites/#other-countries

When i choose some country or click any link in footer or header , for example i click Tanzania

https://www.bettingtipsafrica.com/betting-sites/tanzania/

it take me to page and show correct content, which is good, now when i click back button in browser,

url changed to https://www.bettingtipsafrica.com/betting-sites/#other-countries.

which is also fine. but content did not change

content still showing of page https://www.bettingtipsafrica.com/betting-sites/tanzania/

It should be showing content of

https://www.bettingtipsafrica.com/betting-sites/#other-countries

you can verify content by going to top of page and see main headin