Unverified Commit 66b82931 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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)
parent 5c6d0707
...@@ -1070,6 +1070,11 @@ export default class Router implements BaseRouter { ...@@ -1070,6 +1070,11 @@ export default class Router implements BaseRouter {
if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) { if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) {
const asNoSlash = removeTrailingSlash(new URL(as, 'http://n').pathname) const asNoSlash = removeTrailingSlash(new URL(as, 'http://n').pathname)
if (
asNoSlash !==
removeTrailingSlash(new URL(this.asPath, 'http://n').pathname)
) {
const matchesBflStatic = this._bfl_s?.has(asNoSlash) const matchesBflStatic = this._bfl_s?.has(asNoSlash)
let matchesBflDynamic = false let matchesBflDynamic = false
const asNoSlashParts = asNoSlash.split('/') const asNoSlashParts = asNoSlash.split('/')
...@@ -1078,12 +1083,11 @@ export default class Router implements BaseRouter { ...@@ -1078,12 +1083,11 @@ export default class Router implements BaseRouter {
// it should be hard navigated // it should be hard navigated
for (let i = 0; i < asNoSlashParts.length + 1; i++) { for (let i = 0; i < asNoSlashParts.length + 1; i++) {
const currentPart = asNoSlashParts.slice(0, i).join('/') const currentPart = asNoSlashParts.slice(0, i).join('/')
if (this._bfl_d?.has(currentPart)) { if (currentPart && this._bfl_d?.has(currentPart)) {
matchesBflDynamic = true matchesBflDynamic = true
break break
} }
} }
// if the client router filter is matched then we trigger // if the client router filter is matched then we trigger
// a hard navigation // a hard navigation
if (!isQueryUpdating && (matchesBflStatic || matchesBflDynamic)) { if (!isQueryUpdating && (matchesBflStatic || matchesBflDynamic)) {
...@@ -1094,6 +1098,7 @@ export default class Router implements BaseRouter { ...@@ -1094,6 +1098,7 @@ export default class Router implements BaseRouter {
return false return false
} }
} }
}
let shouldResolveHref = let shouldResolveHref =
isQueryUpdating || isQueryUpdating ||
......
...@@ -27,13 +27,27 @@ createNextDescribe( ...@@ -27,13 +27,27 @@ createNextDescribe(
])( ])(
'should match redirects in pages correctly $path', 'should match redirects in pages correctly $path',
async ({ pathname }) => { async ({ pathname }) => {
const browser = await next.browser('/') let browser = await next.browser('/')
await browser.eval(`next.router.push("${pathname}")`) await browser.eval(`next.router.push("${pathname}")`)
await check(async () => { await check(async () => {
const href = await browser.eval('location.href') const href = await browser.eval('location.href')
return href.includes('example.vercel.sh') ? 'yes' : href return href.includes('example.vercel.sh') ? 'yes' : href
}, 'yes') }, '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)
}
}
} }
) )
......
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