-
Notifications
You must be signed in to change notification settings - Fork 26.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
Jump link anchors dont work #11109
Comments
I get that it finds the documentation section with your link (maybe), but will changing the href to this will resolve the error? |
same error with that or anything that has |
I believe I'm also experiencing this issue. Although I am not getting the error, the jump link to a different page doesn't work. It goes to that page but not the 'jump' section. |
@thislogancall |
I thought I had it resolved, but in production I'm having the same issue. |
I'm trying to use anchor tags inside markdown, it works on this page here: Seems like there is a lot of custom code to support anchor tags in their docs: Markdown natively supports anchor tags though using: This will create the tags for you. But then you have to add the id="anchor-tag" dynamically to the tag using a regex:
|
Same issue. Page doesn't scroll to jump link if come from another page. Jump links only work within the same page |
Same problem |
Any updates here? In the light of NextJs 10 release. |
My ultra-nonsensical solution for this problem, as I needed it asap: useEffect(() => {
const path = window.location.hash
if (path && path.includes('#')) {
const id = path.replace('#', '')
const el = window.document.getElementById(id)
const r = el.getBoundingClientRect()
window.scrollTo({
top: r.top,
behavior: 'smooth'
})
}
}) |
Here is how i adapted the above because it doesn't work on mobile for me: React.useEffect(() => {
const path = window.location.hash
if (path && path.includes("#")) {
setTimeout(() => {
const id = path.replace("#", "")
const el = window.document.getElementById(id)
const r = el.getBoundingClientRect()
window.top.scroll({
top: pageYOffset + r.top,
behavior: "smooth",
})
}, 600)
} |
We were still seeing this no scrolling to anchor issue in next 10 (on initial page load. Anchors scroll fine if you're already navigating from other pages). For some reason the useEffect(() => {
const path = window.location.hash;
if (path && path.includes('#')) {
const id = path.replace('#', '');
if (id) {
document
.querySelector('#' + id)
.scrollIntoView({ behavior: 'smooth' });
}
}
}); |
Next.js ^10 fixed it for ASCII anchors, I'm still having problems with CJK characters. |
I have no idea about CJK chars but I think you can use Unicode instead of direct CJK this link may be useful: |
This issue is still present in Next.js |
has anyone figured out a solution to this. I can't get anchor tags working with next.js at all (same page content). |
Still having this issue. Anchor tag links work when I'm already on the same page as the destination content, but not when I'm on different pages. Are there any plans to support this functionality or should we just go forward with the less than ideal imperative scroll logic in useEffect? |
I have this issue in Next.js 11.1.1. also |
Same here. The links navigate to the page and the hash is preserved, but it doesn't jump to the linked section. If you refresh, the hash remains but the rest of the path disappears so you end up on the home page with the hash appended. |
Have this exact same issue. Why is this not a priority for the Nextjs team? This is breaking a pretty common web pattern for any sites built on top of Nextjs. |
Came across this and using ref to scroll into view. e.g. when navigating to const ExamplePage = () => {
const router = useRouter();
const mySection = useRef(null);
const myOtherSection = useRef(null);
useEffect(() => {
const path = router.asPath;
if (path && path.includes('/#')) {
const sectionName = path.replace('/#', '');
switch (sectionName) {
case 'mySection':
mySection.current.scrollIntoView({ behavior: 'smooth' });
break;
case 'myOtherSection':
myOtherSection.current.scrollIntoView({ behavior: 'smooth' });
break;
default:
break;
}
}
}, [router.asPath]);
return (
<div>
<section ref={mySection}>
My section
</section>
<section ref={myOtherSection}>
My section
</section>
</div>
)
} |
May a suggest:
|
Maybe it's useful for somebody: It's using the native browser function by just reseting the hash. useEffect(() => {
const hash = window.location.hash
window.location.hash = ''
window.location.hash = hash
}) |
Experiencing this issue in NextJS 11 too. I'm linking from A combination of the techniques above is what I ended up going with as a work-around for now: useEffect(() => {
const hash = window.location.hash
if (hash) {
setTimeout(()=> {
document
.querySelector(hash)
.scrollIntoView({ behavior: "smooth" })
}, 100)
}
}) |
Just use |
I am surprised to see this issue which breaks essential scroll to fragment spec. The above fixes are all based client side JS / DOM telling the user agent to scroll into the fragment via either re-setting the The issue is, in the context of Next.js, the DOM is strangely not fully ready at the hook execution, i. e. This will require Next.js core fix I am afraid. |
Been trying to fix this by subscribing to a on dom ready event in
On window load would work, although the UX might be undesirable as the user could already interacted with the page bofore all assets are loaded:
...hopefully that will give someone a good idea how to come up with a robust fix. In theory it could work by subscribing to one of Router events and do the fragment scrolling business there but I am unable to get the router events working in my app right now. |
Found a simple HTML work around no js required
|
Just install https://www.npmjs.com/package/react-scrollchor and worked! Have a nice day of coding guys! |
Where is the script tag meant to be placed? is it in the page component head tag? |
in regard to my earlier comments and my particular case - the culprit was custom auth logic which delayed DOM rendering on some pages which in turn prevented scroll-to-anchor behaviour on SSR pages. hence it was a very custom source of app bug in my case. to be clear I never experienced the explicit js error described in the original issue above. I trust that original bug was fixed and provided that assumption is correct, this GH issue should be fixed to prevent more confusion @Timer I trust other suggested solutions mainly around using finally, previously mentioned events |
This was super helpful... thanks a lot |
I ended up integrating https://www.npmjs.com/package/react-scroll and that worked pretty well. It'd be nice to use a simple Link tag though.
|
Seeing the same issue on |
make sure that the #subsection element is not behind some async logic or anything that would cause that element not be in the DOM yet i. e. at render time in the full refresh scenario |
fixes #11109 ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
This is a quick uncomplicated fix, thanks! PS Changed to this, otherwise a '#' is added to every url:
|
Yeah I found @dmudro 's answer helpful as well. As he says setTimeout is not optimal but I think we all know this. Importantly he encourages us to find the potential problem causing our errors |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
When clicking an anchor tag in a different page with a jump to id tag, it raises an error and doesnt go.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
<a className='nav-link' href='https://github.com/zeit/next.js/#documentation' > Docs </a>
Expected behavior
should redirect to github and scroll to documentation tag
Screenshots
System information
Additional context
I've tried this in another project without nextjs and it works fine i also tried in a random
place like
https://jsfiddle.net/uqxck4jv/
The text was updated successfully, but these errors were encountered: