Handle same page reload case in filter (#46324)

Follow-up to https://github.com/vercel/next.js/pull/46317 this ensures
same page case is handled as well.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
This commit is contained in:
JJ Kasper 2023-02-23 17:18:59 -08:00 committed by GitHub
parent 5c6d07073c
commit 66b82931a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 21 deletions

View file

@ -1070,28 +1070,33 @@ export default class Router implements BaseRouter {
if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) {
const asNoSlash = removeTrailingSlash(new URL(as, 'http://n').pathname)
const matchesBflStatic = this._bfl_s?.has(asNoSlash)
let matchesBflDynamic = false
const asNoSlashParts = asNoSlash.split('/')
// if any sub-path of as matches a dynamic filter path
// it should be hard navigated
for (let i = 0; i < asNoSlashParts.length + 1; i++) {
const currentPart = asNoSlashParts.slice(0, i).join('/')
if (this._bfl_d?.has(currentPart)) {
matchesBflDynamic = true
break
if (
asNoSlash !==
removeTrailingSlash(new URL(this.asPath, 'http://n').pathname)
) {
const matchesBflStatic = this._bfl_s?.has(asNoSlash)
let matchesBflDynamic = false
const asNoSlashParts = asNoSlash.split('/')
// if any sub-path of as matches a dynamic filter path
// it should be hard navigated
for (let i = 0; i < asNoSlashParts.length + 1; i++) {
const currentPart = asNoSlashParts.slice(0, i).join('/')
if (currentPart && this._bfl_d?.has(currentPart)) {
matchesBflDynamic = true
break
}
}
// if the client router filter is matched then we trigger
// a hard navigation
if (!isQueryUpdating && (matchesBflStatic || matchesBflDynamic)) {
handleHardNavigation({
url: addBasePath(addLocale(as, options.locale || this.locale)),
router: this,
})
return false
}
}
// if the client router filter is matched then we trigger
// a hard navigation
if (!isQueryUpdating && (matchesBflStatic || matchesBflDynamic)) {
handleHardNavigation({
url: addBasePath(addLocale(as, options.locale || this.locale)),
router: this,
})
return false
}
}

View file

@ -27,13 +27,27 @@ createNextDescribe(
])(
'should match redirects in pages correctly $path',
async ({ pathname }) => {
const browser = await next.browser('/')
let browser = await next.browser('/')
await browser.eval(`next.router.push("${pathname}")`)
await check(async () => {
const href = await browser.eval('location.href')
return href.includes('example.vercel.sh') ? 'yes' : href
}, 'yes')
if (pathname.includes('/blog')) {
browser = await next.browser('/blog/first')
await browser.eval('window.beforeNav = 1')
// check 5 times to ensure a reload didn't occur
for (let i = 0; i < 5; i++) {
await waitFor(500)
expect(
await browser.eval('document.documentElement.innerHTML')
).toContain('hello from pages/blog/[slug]')
expect(await browser.eval('window.beforeNav')).toBe(1)
}
}
}
)