-
Notifications
You must be signed in to change notification settings - Fork 27.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server rendered routes with hashes do not work with browser back/forward navigation #56112
Comments
I don't see this issue happening on the docs, but I do experience this in my app. Currently also looking for a solution... |
Same here |
Is there any workaround or smth we can use to avoid this issue? thanks |
I can reproduce the bug in our project. Any plans for a fix? |
I cannot reproduce it anymore on the docs page.
Behavior:
Desired behavior:
|
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 |
Update hash based list to query based in dashboard to mitigate Nextjs 14 issue vercel/next.js#56112
Confirming the behavior described by @arshseraphic using |
Having same issue using "next": "14.2.5" with app router. Has anybody found a workaround? |
Same behavior with As a workaround, I add below component to global layout, hold "use client"
import { useState, useEffect, useCallback } from 'react';
import { useRouter, usePathname } from 'next/navigation';
export default function FollowHashRoute() {
const [latestPath, setLatestPath] = useState('');
const pathname = usePathname();
const router = useRouter();
const onPopState = useCallback((e: PopStateEvent) => {
const newPathname = new URL(document.location.href).pathname;
if (newPathname !== latestPath) {
setLatestPath(newPathname);
router.replace(document.location.href);
}
}, [router, latestPath]);
useEffect(() => {
window.addEventListener('popstate', onPopState);
return () => {
window.removeEventListener('popstate', onPopState);
};
}, [onPopState]);
if (pathname !== latestPath) {
setLatestPath(pathname);
}
return null;
} |
Hi all, has anyone found a workaround to this? I just experiencing the same thing. |
We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate. Why was this issue marked with the
|
There is a problem when history going BACK from e.g., /world/area/@xxx to /facts#fact-2016-05-05_120 the location does change, while the page stayed the same. Similar issue report of next.js: vercel/next.js#56112 As a workaround, add a global FollowHashRoute component to take record of pathname changes, maintain an internal `latestPath` state. Listen to `popstate` event (which fires during history BACK/FORWARD), force a router navigation when detect bad case (URL pathname not match latest known pathname).
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Link to the code that reproduces this issue
https://github.com/benhonda/nextjs-router-bug
To Reproduce
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
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.
The text was updated successfully, but these errors were encountered: