Skip to content

Commit

Permalink
Handle same page reload case in filter (#46324)
Browse files Browse the repository at this point in the history
Follow-up to #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)
  • Loading branch information
ijjk authored Feb 24, 2023
1 parent 5c6d070 commit 66b8293
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
47 changes: 26 additions & 21 deletions packages/next/src/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 (
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
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
)

Expand Down

0 comments on commit 66b8293

Please sign in to comment.