diff --git a/packages/next/src/shared/lib/router/router.ts b/packages/next/src/shared/lib/router/router.ts index 9f5b79ee9f052..d31c8cb4701be 100644 --- a/packages/next/src/shared/lib/router/router.ts +++ b/packages/next/src/shared/lib/router/router.ts @@ -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 + } } } diff --git a/test/e2e/app-dir/app/index.test.ts b/test/e2e/app-dir/app/index.test.ts index 26aa384422195..2ede44cf0681e 100644 --- a/test/e2e/app-dir/app/index.test.ts +++ b/test/e2e/app-dir/app/index.test.ts @@ -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) + } + } } )