Skip to content
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

Link with hash doesn't seem to work in next13 #42157

Closed
1 task done
shanejonas opened this issue Oct 29, 2022 · 16 comments · Fixed by #49521
Closed
1 task done

Link with hash doesn't seem to work in next13 #42157

shanejonas opened this issue Oct 29, 2022 · 16 comments · Fixed by #49521
Labels
bug Issue was opened via the bug report template.

Comments

@shanejonas
Copy link

shanejonas commented Oct 29, 2022

Verify canary release

  • 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 shanejonas added the bug Issue was opened via the bug report template. label Oct 29, 2022
@1377sayaba
Copy link

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

@cachho
Copy link

cachho commented Nov 6, 2022

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.

@mikeschneiderdotme
Copy link

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

@returnvoid
Copy link

Adding shallow={true} worked for me:

Go there

@jayantasamaddar
Copy link

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

Does not work in Next 13.2.3

@khan-ajamal
Copy link

Anyone figured it out yet. I am stilling facing this issue in next@13.3.0

@bhavesh-chaudhari
Copy link

I am facing this issue too with next@13.1.2

@drajamal
Copy link

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.

@jmalexan
Copy link

Having this as well, next@13.3. Using tags at the moment to workaround.

@SaveliiLukash
Copy link

Next 13.4.2, still issues with #

@fzsf163
Copy link

fzsf163 commented May 23, 2023

"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.

@stephen-spar
Copy link

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.

@Afrozefathima
Copy link

For me link with # works but general link not navigating

@Afrozefathima
Copy link

Like is not working in next js 13.4.2

@fzsf163
Copy link

fzsf163 commented May 26, 2023

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.

ijjk pushed a commit that referenced this issue May 28, 2023
…r push/replace (#49521)

## Problem
Relative hash/query handling in `next/link` (e.g. `<Link
href="#hello">`) is broken in App Router, especially if you're on a
nested route.

This wasn't a problem in `/pages` because the href always get fully
resolved in `<NextLink>`; i.e. if you have `<Link href="#hash" />` on
`/hello`, it'll resolve the href to `/hello#hash` and use that
everywhere.

However, in App Router, `<Link>` no longer uses the current location to
resolve the href:
https://github.com/vercel/next.js/blob/5451564f364399003b05939fa6dd7d32c1dabec7/packages/next/src/client/link.tsx#L450-L457

Therefore navigating with `new URL(href, location.origin)` will skip the
current path and always apply the relative hash/query to the _root_
route:
https://github.com/vercel/next.js/blob/5451564f364399003b05939fa6dd7d32c1dabec7/packages/next/src/client/components/app-router.tsx#L208-L215

## Solution
Not 100% sure if this is the best solution, but since `app-router` is
already reading `window.location`, I'm using `location.href` as the base
URL to resolve the href.

I grep'd for `location.origin` and checked other callsites; seems like
only `app-router` deals with user specified hrefs.

Fixes #42157 &
#44139, and potentially
#48554 and
#22838

## Test Plan
```
pnpm testheadless test/e2e/app-dir/navigation
```

---------
hydRAnger pushed a commit to hydRAnger/next.js that referenced this issue Jun 12, 2023
…r push/replace (vercel#49521)

## Problem
Relative hash/query handling in `next/link` (e.g. `<Link
href="#hello">`) is broken in App Router, especially if you're on a
nested route.

This wasn't a problem in `/pages` because the href always get fully
resolved in `<NextLink>`; i.e. if you have `<Link href="#hash" />` on
`/hello`, it'll resolve the href to `/hello#hash` and use that
everywhere.

However, in App Router, `<Link>` no longer uses the current location to
resolve the href:
https://github.com/vercel/next.js/blob/5451564f364399003b05939fa6dd7d32c1dabec7/packages/next/src/client/link.tsx#L450-L457

Therefore navigating with `new URL(href, location.origin)` will skip the
current path and always apply the relative hash/query to the _root_
route:
https://github.com/vercel/next.js/blob/5451564f364399003b05939fa6dd7d32c1dabec7/packages/next/src/client/components/app-router.tsx#L208-L215

## Solution
Not 100% sure if this is the best solution, but since `app-router` is
already reading `window.location`, I'm using `location.href` as the base
URL to resolve the href.

I grep'd for `location.origin` and checked other callsites; seems like
only `app-router` deals with user specified hrefs.

Fixes vercel#42157 &
vercel#44139, and potentially
vercel#48554 and
vercel#22838

## Test Plan
```
pnpm testheadless test/e2e/app-dir/navigation
```

---------
@github-actions
Copy link
Contributor

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 github-actions bot locked as resolved and limited conversation to collaborators Jun 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.