next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Link with hash doesn't seem to work in next13

Open shanejonas opened this issue 2 years ago • 2 comments

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
Binaries:
  Node: 14.17.5
  npm: 6.14.14
  Yarn: 1.22.17
  pnpm: N/A
Relevant packages:
  next: 13.0.0
  eslint-config-next: 13.0.0
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Brave 106.0.5249.119

How are you deploying your application? (if relevant)

next dev

Describe the Bug

Linking with hash doesn't seem to work in next13

<Link href="#footer">
 Link To Footer
</Link>

becomes http://localhost:3000/undefined#footer

Expected Behavior

link should be to current page header but instead becomes http://localhost:3000/undefined#footer

Link to reproduction

https://codesandbox.io/p/github/shanejonas/nextjs-link-undefined-bug/draft/great-lake?file=%2Fapp%2Fpage.tsx

To Reproduce

click/hover over Link To Footer to see the link has undefined in it

Screenshot

image

shanejonas avatar Oct 29 '22 19:10 shanejonas

This is also happened for me So i downgraded to version 12.3

1377sayaba avatar Oct 29 '22 19:10 1377sayaba

I don't want to hijack your thread, but I found another issues with hashes in links.

<a href="#CCM.openWidget" title="Cookie consent configuration"> (ccm19 script) doesn't do anything in next13, when I use the same snippet in create-react-app, vue and wordpress. (I didn't want to open a thread for now, in case it might be a 3rd party issue)

Update: this is working now.

cachho avatar Nov 06 '22 20:11 cachho

I also have run into this issue, next/link is not appending the hash to the url and instead is routing to the hash address. <Link href='#hash'>Link</Link> before I upgraded to Next13: ~/currentRoute#hash after: ~/#hash

mikeschneiderdotme avatar Dec 16 '22 20:12 mikeschneiderdotme

Adding shallow={true} worked for me:

Go there

returnvoid avatar Feb 03 '23 23:02 returnvoid

Adding shallow={true} worked for me: Go there

Does not work in Next 13.2.3

jayantasamaddar avatar Mar 06 '23 05:03 jayantasamaddar

Anyone figured it out yet. I am stilling facing this issue in [email protected]

khan-ajamal avatar Apr 09 '23 06:04 khan-ajamal

I am facing this issue too with [email protected]

bhavesh-chaudhari avatar Apr 11 '23 05:04 bhavesh-chaudhari

For now, I am using the raw HTML a tag because my use case is just taking the user to a particular section on the same page.

drajamal avatar Apr 11 '23 10:04 drajamal

Having this as well, [email protected]. Using tags at the moment to workaround.

jmalexan avatar Apr 15 '23 02:04 jmalexan

Next 13.4.2, still issues with #

SaveliiLukash avatar May 16 '23 11:05 SaveliiLukash

"next": "13.4.3" I don't know if this solves the problem , but writting it like this works. May be.

<Link href='/#sectionID'><Link/>

Does anyone know how to get this '#id' part from the url ? I want to show active section. This is why I need to get this. I am pretty new at this.

fzsf163 avatar May 23 '23 19:05 fzsf163

Sorry if this isn't the right place, but I'm seeing the following issue using next: 13.4.1 on the app router w/ hashes:

  • be on the root route (i.e. /)
  • click on a <Link> with an href to a specific section on that page (ex. <Link href='#contact'>
    • this correctly jumps down to the element with id=contact
  • scroll somewhere else on the page & click on a Link with href='/'
  • the page automatically scrolls down to the element with id=contact
  • if you navigate to a different page and then navigate back to the root route using a Link with href='/' (i.e. without the #contact) without refreshing in between, it will also automatically scroll down to the element with id='contact'

guessing this is some sort of uncleared state issue? My workaround currently is to add an id to the top element of my root route and have links go to href='/#top-element' so the scroll position isn't disrupted.

stephen-spar avatar May 23 '23 21:05 stephen-spar

For me link with # works but general link not navigating

Afrozefathima avatar May 26 '23 17:05 Afrozefathima

Like <Link href='/link'> is not working in next js 13.4.2

Afrozefathima avatar May 26 '23 17:05 Afrozefathima

For me link with # works but general link not navigating

some code reference would help everyone to understand your problem better. If you are meaning this type of link-

<Link href="/about"></Link>

which takes you to about page. Then you need to put about folder inside app directory.

file structure should be like this


├───app
│   ├───about
│   ├───blog

the "/" indicates app route or folder then about directs to your about folder and page.js/tsx/jsx renders about page. You can switch /about with /blog to go to blog page.

fzsf163 avatar May 26 '23 17:05 fzsf163

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.

github-actions[bot] avatar Jun 28 '23 00:06 github-actions[bot]