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

Fix going back to page after applying hash link #50006

Merged
merged 7 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/src/client/components/layout-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr

// State is mutated to ensure that the focus and scroll is applied only once.
focusAndScrollRef.apply = false
focusAndScrollRef.hashFragment = null
focusAndScrollRef.segmentPaths = []

handleSmoothScroll(
() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Link from 'next/link'

export default function Page() {
return (
<>
<p>Other Page</p>
<Link href="/hash-link-back-to-same-page" id="link-to-home">
Back to test home
</Link>
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Link from 'next/link'
import '../hash/global.css'

export default function HashPage() {
// Create list of 5000 items that all have unique id
const items = Array.from({ length: 5000 }, (_, i) => ({ id: i }))

return (
<div style={{ fontFamily: 'sans-serif', fontSize: '16px' }}>
<p>Hash Page</p>
<Link href="/hash-link-back-to-same-page#hash-6" id="link-to-6">
To 6
</Link>
<Link href="/hash-link-back-to-same-page#hash-50" id="link-to-50">
To 50
</Link>
<Link href="/hash-link-back-to-same-page#hash-160" id="link-to-160">
To 160
</Link>
<div>
{items.map((item) => {
if (item.id === 160) {
return (
<>
<div key={item.id}>
<div id={`hash-${item.id}`}>{item.id}</div>
</div>
<div key="to-other-page">
<div>
<Link
href="/hash-link-back-to-same-page/other"
id="to-other-page"
>
To other page
</Link>
</div>
</div>
</>
)
}
return (
<div key={item.id}>
<div id={`hash-${item.id}`}>{item.id}</div>
</div>
)
})}
</div>
</div>
)
}
50 changes: 49 additions & 1 deletion test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ createNextDescribe(
await check(
async () => {
const val = await browser.eval('window.pageYOffset')
require('console').error({ val })
return val.toString()
},
expectedScroll.toString(),
Expand All @@ -58,6 +57,55 @@ createNextDescribe(
})
})

describe('hash-link-back-to-same-page', () => {
it('should scroll to the specified hash', async () => {
const browser = await next.browser('/hash-link-back-to-same-page')

const checkLink = async (
val: number | string,
expectedScroll: number
) => {
await browser.elementByCss(`#link-to-${val.toString()}`).click()
await check(
async () => {
const val = await browser.eval('window.pageYOffset')
return val.toString()
},
expectedScroll.toString(),
true,
// Try maximum of 15 seconds
15
)
}

await checkLink(6, 114)
await checkLink(50, 730)
await checkLink(160, 2270)

await browser
.elementByCss('#to-other-page')
// Navigate to other
.click()
// Wait for other ot load
.waitForElementByCss('#link-to-home')
// Navigate back to hash-link-back-to-same-page
.click()
// Wait for hash-link-back-to-same-page to load
.waitForElementByCss('#to-other-page')

await check(
async () => {
const val = await browser.eval('window.pageYOffset')
return val.toString()
},
(0).toString(),
true,
// Try maximum of 15 seconds
15
)
})
})

describe('not-found', () => {
it('should trigger not-found in a server component', async () => {
const browser = await next.browser('/not-found/servercomponent')
Expand Down