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 browser navigation buttons not working with shallow routing and middleware #43919

Merged
merged 5 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 7 additions & 9 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -1456,15 +1456,13 @@ export default class Router implements BaseRouter {

// we don't attempt resolve asPath when we need to execute
// middleware as the resolving will occur server-side
const isMiddlewareMatch = await matchesMiddleware({
asPath: as,
locale: nextState.locale,
router: this,
})

if (options.shallow && isMiddlewareMatch) {
pathname = this.pathname
}
const isMiddlewareMatch =
!options.shallow &&
(await matchesMiddleware({
asPath: as,
locale: nextState.locale,
router: this,
}))

if (isQueryUpdating && isMiddlewareMatch) {
shouldResolveHref = false
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/middleware-shallow-link/app/middleware.js
@@ -0,0 +1,9 @@
import { NextResponse } from 'next/server'

export async function middleware() {
return NextResponse.next()
}

export const config = {
matcher: '/(.*$)',
}
26 changes: 26 additions & 0 deletions test/e2e/middleware-shallow-link/app/pages/index.js
@@ -0,0 +1,26 @@
import Link from 'next/link'
import React from 'react'

const Page = () => {
return (
<>
<h1>Content for page 1</h1>
<div>
<Link
data-next-shallow-push
shallow
href={{ query: { params: 'testParams' } }}
>
Shallow push
</Link>
</div>
<div>
<Link data-next-page href="/page2">
Go to page 2
</Link>
</div>
</>
)
}

export default Page
25 changes: 25 additions & 0 deletions test/e2e/middleware-shallow-link/app/pages/page2.js
@@ -0,0 +1,25 @@
import Link from 'next/link'
import React from 'react'

const Page = () => {
return (
<>
<h1>Content for page 2</h1>
<Link
data-next-shallow-replace
replace
shallow
href={{ query: { params: 'testParams' } }}
>
Shallow replace
</Link>
<div>
<button data-go-back onClick={() => window.history.back()}>
Go Back
</button>
</div>
</>
)
}

export default Page
44 changes: 44 additions & 0 deletions test/e2e/middleware-shallow-link/index.test.ts
@@ -0,0 +1,44 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import webdriver from 'next-webdriver'
import { BrowserInterface } from 'test/lib/browsers/base'
import { join } from 'path'

describe('browser-shallow-navigation', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
'middleware.js': new FileRef(join(__dirname, 'app/middleware.js')),
},
})
})

afterAll(() => next.destroy())

it('should render the correct page', async () => {
const browser = await webdriver(next.url, '/')

/// do shallow push
await browser.elementByCss('[data-next-shallow-push]').click()
await browser.waitForElementByCss('[data-next-page]')

// go to another page
await browser.elementByCss('[data-next-page]').click()
await browser.waitForElementByCss('[data-next-shallow-replace]')

// do shadow replace
await browser.elementByCss('[data-next-shallow-replace]').click()
await browser.waitForElementByCss('[data-go-back]')

// go back using history api
await browser.elementByCss('[data-go-back]').click()

// get page h1
let title = await browser.elementByCss('h1').text()
expect(title).toContain('Content for page 1')
})
})