Unverified Commit 3d499a62 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add test for link back to original page (#41297)

- Implements failing test
- Implemented handling for the case where the router tree does not match up with the segment path being navigated to. This is what caused the underlying error. 



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a 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 a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent d74f3402
...@@ -471,11 +471,16 @@ function shouldHardNavigate( ...@@ -471,11 +471,16 @@ function shouldHardNavigate(
string string
] ]
// Check if current segment matches the existing segment.
if (!matchSegment(currentSegment, segment)) {
// If dynamic parameter in tree doesn't match up with segment path a hard navigation is triggered. // If dynamic parameter in tree doesn't match up with segment path a hard navigation is triggered.
if (Array.isArray(currentSegment) && !matchSegment(currentSegment, segment)) { if (Array.isArray(currentSegment)) {
return true return true
} }
// If the existing segment did not match soft navigation is triggered.
return false
}
const lastSegment = flightSegmentPath.length <= 2 const lastSegment = flightSegmentPath.length <= 2
if (lastSegment) { if (lastSegment) {
......
export default function AboutPage() {
return <p id="about-page">About page</p>
}
import Link from 'next/link'
export default function Layout({ children }) {
return (
<>
<header>
<nav>
<Link href="/linking">Home</Link>
<Link href="/linking/about">About</Link>
</nav>
</header>
{children}
</>
)
}
export default function Page() {
return <p id="home-page">Home page</p>
}
...@@ -1570,6 +1570,24 @@ describe('app dir', () => { ...@@ -1570,6 +1570,24 @@ describe('app dir', () => {
.text() .text()
).toBe(`hello from app/dashboard/deployments/info/[id]. ID is: 123`) ).toBe(`hello from app/dashboard/deployments/info/[id]. ID is: 123`)
}) })
it('should handle next/link back to initially loaded page', async () => {
const browser = await webdriver(next.url, '/linking/about')
expect(
await browser
.elementByCss('a[href="/linking"]')
.click()
.waitForElementByCss('#home-page')
.text()
).toBe(`Home page`)
expect(
await browser
.elementByCss('a[href="/linking/about"]')
.click()
.waitForElementByCss('#about-page')
.text()
).toBe(`About page`)
})
}) })
describe('not-found', () => { describe('not-found', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment