Unverified Commit 51f9f5df authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Apply filtering after rewrite as well (#46390

x-ref: [slack
thread](https://vercel.slack.com/archives/C04MEB9L9RQ/p1677279320253929?thread_ts=1677255545.574209&cid=C04MEB9L9RQ)

## 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 de473366
...@@ -1052,6 +1052,61 @@ export default class Router implements BaseRouter { ...@@ -1052,6 +1052,61 @@ export default class Router implements BaseRouter {
return this.change('replaceState', url, as, options) return this.change('replaceState', url, as, options)
} }
async _bfl(as: string, resolvedAs?: string, locale?: string | false) {
if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) {
let matchesBflStatic = false
let matchesBflDynamic = false
for (const curAs of [as, resolvedAs]) {
if (curAs) {
const asNoSlash = removeTrailingSlash(
new URL(curAs, 'http://n').pathname
)
const asNoSlashLocale = addBasePath(
addLocale(curAs, locale || this.locale)
)
if (
asNoSlash !==
removeTrailingSlash(new URL(this.asPath, 'http://n').pathname)
) {
matchesBflStatic =
matchesBflStatic ||
!!this._bfl_s?.has(asNoSlash) ||
!!this._bfl_s?.has(asNoSlashLocale)
for (const normalizedAS of [asNoSlash, asNoSlashLocale]) {
// if any sub-path of as matches a dynamic filter path
// it should be hard navigated
const curAsParts = normalizedAS.split('/')
for (
let i = 0;
!matchesBflDynamic && i < curAsParts.length + 1;
i++
) {
const currentPart = curAsParts.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 (matchesBflStatic || matchesBflDynamic) {
handleHardNavigation({
url: addBasePath(addLocale(as, locale || this.locale)),
router: this,
})
return new Promise(() => {})
}
}
}
}
}
}
private async change( private async change(
method: HistoryMethod, method: HistoryMethod,
url: string, url: string,
...@@ -1068,36 +1123,8 @@ export default class Router implements BaseRouter { ...@@ -1068,36 +1123,8 @@ export default class Router implements BaseRouter {
// any time without notice. // any time without notice.
const isQueryUpdating = (options as any)._h === 1 const isQueryUpdating = (options as any)._h === 1
if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) { if (!isQueryUpdating) {
const asNoSlash = removeTrailingSlash(new URL(as, 'http://n').pathname) await this._bfl(as, undefined, options.locale)
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
}
}
} }
let shouldResolveHref = let shouldResolveHref =
...@@ -1482,6 +1509,14 @@ export default class Router implements BaseRouter { ...@@ -1482,6 +1509,14 @@ export default class Router implements BaseRouter {
isMiddlewareRewrite, isMiddlewareRewrite,
}) })
if (!isQueryUpdating) {
await this._bfl(
as,
'resolvedAs' in routeInfo ? routeInfo.resolvedAs : undefined,
nextState.locale
)
}
if ('route' in routeInfo && isMiddlewareMatch) { if ('route' in routeInfo && isMiddlewareMatch) {
pathname = routeInfo.route || route pathname = routeInfo.route || route
route = pathname route = pathname
......
/* eslint-env jest */ /* eslint-env jest */
import { createNextDescribe, FileRef } from 'e2e-utils'
import cheerio from 'cheerio'
import path from 'path' import path from 'path'
import { withQuery } from 'next-test-utils' import cheerio from 'cheerio'
import { check, withQuery } from 'next-test-utils'
import { createNextDescribe, FileRef } from 'e2e-utils'
createNextDescribe( createNextDescribe(
'app-dir with middleware', 'app-dir with middleware',
...@@ -11,6 +11,17 @@ createNextDescribe( ...@@ -11,6 +11,17 @@ createNextDescribe(
skipDeployment: true, skipDeployment: true,
}, },
({ next }) => { ({ next }) => {
it('should filter correctly after middleware rewrite', async () => {
const browser = await next.browser('/start')
await browser.eval('window.beforeNav = 1')
await browser.eval('window.next.router.push("/rewrite-to-app")')
await check(async () => {
return browser.eval('document.documentElement.innerHTML')
}, /app-dir/)
})
describe.each([ describe.each([
{ {
title: 'Serverless Functions', title: 'Serverless Functions',
......
...@@ -4,6 +4,7 @@ export default function SSRPage() { ...@@ -4,6 +4,7 @@ export default function SSRPage() {
const headersObj = Object.fromEntries(headers()) const headersObj = Object.fromEntries(headers())
return ( return (
<> <>
<p>app-dir</p>
<p id="headers">{JSON.stringify(headersObj)}</p> <p id="headers">{JSON.stringify(headersObj)}</p>
</> </>
) )
......
...@@ -22,6 +22,11 @@ export async function middleware(request) { ...@@ -22,6 +22,11 @@ export async function middleware(request) {
} }
} }
if (request.nextUrl.pathname.includes('/rewrite-to-app')) {
request.nextUrl.pathname = '/headers'
return NextResponse.rewrite(request.nextUrl)
}
return NextResponse.next({ return NextResponse.next({
request: { request: {
headers, headers,
......
module.exports = { module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
clientRouterFilter: true,
}, },
} }
export default function Page() {
return <p>hello from pages/[slug]</p>
}
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