Skip to content

Commit

Permalink
fix(next): keep hash when navigating from app to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
blurrah committed Sep 29, 2023
1 parent d664f9a commit 6db6e8b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,13 @@ export function navigateReducer(

// Handle case when navigating to page in `pages` from `app`
if (typeof flightData === 'string') {
return handleExternalUrl(state, mutable, flightData, pendingPush)
return handleExternalUrl(
state,
mutable,
// flightData omits the hash, use the href including the hash as the external url when the href has a different path
flightData ? href : flightData,
pendingPush
)
}

let currentTree = state.tree
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-size: 14px;
line-height: 1;
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/navigation/app/hash-link-to-pages-router/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Link from 'next/link'
import './global.css'

export default function HashPage() {
return (
<div style={{ fontFamily: 'sans-serif', fontSize: '16px' }}>
<p>Hash To Pages Router Page</p>
<Link href="/some#non-existent" id="link-to-pages-router">
To pages router
</Link>
</div>
)
}
9 changes: 9 additions & 0 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@ createNextDescribe(
expect(await browser.url()).toBe(next.url + '/some')
})

it('should not omit the hash while navigating from app to pages', async () => {
const browser = await next.browser('/hash-link-to-pages-router')
await browser
.elementByCss('#link-to-pages-router')
.click()
.waitForElementByCss('#link-to-app')
await check(() => browser.url(), next.url + '/some#non-existent')
})

if (!isNextDev) {
// this test is pretty hard to test in playwright, so most of the heavy lifting is in the page component itself
// it triggers a hover on a link to initiate a prefetch request every second, and so we check that
Expand Down

0 comments on commit 6db6e8b

Please sign in to comment.