Navigating to hash does not trigger router change
See original GitHub issueBug report
Describe the bug
I would like to navigate to a hash on the same page, and be able to react to that change from different components. More precisely what I want to achieve is to jump to a collapsible section and expand it automatically based on that the hash in the URL matches the id of that section.
When creating a NextLink with a hash, router
of useRouter
does not change/trigger
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
My link:
<NextLink href="#section">
<a>Jump to section</a>
</NextLink>
My component where I expect the change:
//...
const router = useRouter()
return (
<Collapsible expanded={router.asPath.endsWith("#section")}>
Lorem ipsum
</Collapsible>
)
Expected behavior
Navigating to a hash should trigger useRouter()
System information
- OS: Ubuntu 20.04
- Browser Chrome Dev
- Version of Next.js: 9.4.4
- Version of Node.js: 12.6.0
Additional context
Found this: https://github.com/vercel/next.js/issues/5161
It states the issue is resolved, but I cannot figure out how to achieve this behaviour.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
How to detect change in the URL hash in Next.js?
You can listen to hash changes using hashChangeStart event from router.events . const Test = () => { const router = useRouter(); ...
Read more >Navigation Guards | Vue Router
beforeEnter guards only trigger when entering the route, they don't trigger when the params , query or hash change e.g. going from /users/2...
Read more >Step 5: Display a Target Without Changing the Hash
The router is smart enough to detect that the current hash did not change and therefore skips the navigation to the route. Fortunately,...
Read more >Understanding Next.js routeChangeStart and router events
The hashChangeStart event is triggered when the hash of a URL starts to change, but not the page. In comparison, the hashChangeComplete event...
Read more >Common Routing Tasks - Angular
To use the Angular router, an application needs to have at least two components so that it can navigate from one to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@tomdohnal you might be onto something, since this has been marked as a good first issue, you might be right. Would you open a PR with your suggestion? If you don’t have the time, I can try myself, if you want, and I can credit you for the idea in the PR. 🙂
I looked into the issue and it seems to be caused by the fact that the
notify
method inpackages/next/next-server/lib/router/router.ts
is not being called for hash changes.If I replace this
with this
in the
change
method, it starts working.I’m happy to send a PR fixing this. However, I suppose you’d like to have a test for this behaviour and I’m not sure about what the best way to test it is. Maybe create a new test fixture in the
test/integration/client-navigation/pages/nav
? Something likeas-path-hash
and a test function intest/integration/client-navigation/test/index.test.js
?