Skip to content

Commit

Permalink
Add test for link back to original page (vercel#41297)
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
timneutkens authored and Kikobeats committed Oct 24, 2022
1 parent 3660cd1 commit 29d6196
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/next/client/components/reducer.ts
Expand Up @@ -471,11 +471,16 @@ function shouldHardNavigate(
string
]

// If dynamic parameter in tree doesn't match up with segment path a hard navigation is triggered.
if (Array.isArray(currentSegment) && !matchSegment(currentSegment, segment)) {
return true
}
// 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 (Array.isArray(currentSegment)) {
return true
}

// If the existing segment did not match soft navigation is triggered.
return false
}
const lastSegment = flightSegmentPath.length <= 2

if (lastSegment) {
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/app/linking/about/page.js
@@ -0,0 +1,3 @@
export default function AboutPage() {
return <p id="about-page">About page</p>
}
15 changes: 15 additions & 0 deletions test/e2e/app-dir/app/app/linking/layout.js
@@ -0,0 +1,15 @@
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}
</>
)
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app/app/linking/page.js
@@ -0,0 +1,3 @@
export default function Page() {
return <p id="home-page">Home page</p>
}
18 changes: 18 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -1570,6 +1570,24 @@ describe('app dir', () => {
.text()
).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', () => {
Expand Down

0 comments on commit 29d6196

Please sign in to comment.