Unverified Commit 8ff3d7ff authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix client filter case with redirects (#46317)

Follow-up to https://github.com/vercel/next.js/pull/46283 this ensures we properly handle redirects with matchers at the base and we don't use the normalized redirects when generating the filter as the breaks with i18n. 

x-ref: [slack thread](https://vercel.slack.com/archives/C040J1CGLHH/p1677028542232619)

## 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 4eb34ef3
...@@ -842,8 +842,8 @@ export default async function build( ...@@ -842,8 +842,8 @@ export default async function build(
] ]
if (config.experimental.clientRouterFilter) { if (config.experimental.clientRouterFilter) {
const nonInternalRedirects = redirects.filter( const nonInternalRedirects = (config._originalRedirects || []).filter(
(redir) => !(redir as any).internal (r: any) => !r.internal
) )
const clientRouterFilters = createClientRouterFilter( const clientRouterFilters = createClientRouterFilter(
appPageKeys, appPageKeys,
......
...@@ -30,7 +30,10 @@ export function createClientRouterFilter( ...@@ -30,7 +30,10 @@ export function createClientRouterFilter(
} }
subPath = `${subPath}/${curPart}` subPath = `${subPath}/${curPart}`
} }
dynamicPaths.add(subPath)
if (subPath) {
dynamicPaths.add(subPath)
}
} else { } else {
staticPaths.add(path) staticPaths.add(path)
} }
...@@ -53,7 +56,11 @@ export function createClientRouterFilter( ...@@ -53,7 +56,11 @@ export function createClientRouterFilter(
subPath = `${subPath}/${curPart}` subPath = `${subPath}/${curPart}`
} }
dynamicPaths.add(subPath) // if redirect has matcher at top-level we don't include this
// as it would match everything
if (subPath) {
dynamicPaths.add(subPath)
}
} else { } else {
staticPaths.add(path) staticPaths.add(path)
} }
......
...@@ -567,6 +567,10 @@ async function loadRedirects(config: NextConfig) { ...@@ -567,6 +567,10 @@ async function loadRedirects(config: NextConfig) {
// they are still valid // they are still valid
checkCustomRoutes(redirects, 'redirect') checkCustomRoutes(redirects, 'redirect')
// save original redirects before transforms
if (Array.isArray(redirects)) {
;(config as any)._originalRedirects = redirects.map((r) => ({ ...r }))
}
redirects = processRoutes(redirects, config, 'redirect') redirects = processRoutes(redirects, config, 'redirect')
checkCustomRoutes(redirects, 'redirect') checkCustomRoutes(redirects, 'redirect')
return redirects return redirects
......
...@@ -585,7 +585,9 @@ export default class DevServer extends Server { ...@@ -585,7 +585,9 @@ export default class DevServer extends Server {
if (this.nextConfig.experimental.clientRouterFilter) { if (this.nextConfig.experimental.clientRouterFilter) {
clientRouterFilters = createClientRouterFilter( clientRouterFilters = createClientRouterFilter(
Object.keys(appPaths), Object.keys(appPaths),
this.customRoutes.redirects.filter((r) => !(r as any).internal) ((this.nextConfig as any)._originalRedirects || []).filter(
(r: any) => !r.internal
)
) )
if ( if (
......
...@@ -1069,7 +1069,7 @@ export default class Router implements BaseRouter { ...@@ -1069,7 +1069,7 @@ export default class Router implements BaseRouter {
const isQueryUpdating = (options as any)._h === 1 const isQueryUpdating = (options as any)._h === 1
if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) { if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) {
const asNoSlash = removeTrailingSlash(as) const asNoSlash = removeTrailingSlash(new URL(as, '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('/')
...@@ -1087,7 +1087,10 @@ export default class Router implements BaseRouter { ...@@ -1087,7 +1087,10 @@ export default class Router implements BaseRouter {
// 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)) {
handleHardNavigation({ url: as, router: this }) handleHardNavigation({
url: addBasePath(addLocale(as, options.locale || this.locale)),
router: this,
})
return false return false
} }
} }
......
...@@ -54,6 +54,11 @@ module.exports = { ...@@ -54,6 +54,11 @@ module.exports = {
destination: 'https://example.vercel.sh', destination: 'https://example.vercel.sh',
permanent: false, permanent: false,
}, },
{
source: '/:path/to-redirect',
destination: 'https://example.vercel.sh',
permanent: false,
},
] ]
}, },
} }
...@@ -3,6 +3,9 @@ module.exports = { ...@@ -3,6 +3,9 @@ module.exports = {
locales: ['en', 'fr', 'nl', 'es'], locales: ['en', 'fr', 'nl', 'es'],
defaultLocale: 'en', defaultLocale: 'en',
}, },
experimental: {
clientRouterFilter: true,
},
redirects() { redirects() {
return [ return [
{ {
......
export default function Account() { export default function Account({ slug }) {
return ( return (
<p id="dynamic" className="title"> <p id="dynamic" className="title">
Welcome to a /dynamic/[slug] Welcome to a /dynamic/[slug]: {slug}
</p> </p>
) )
} }
export function getServerSideProps() { export function getServerSideProps({ params }) {
return { return {
props: {}, props: {
slug: params.slug,
},
} }
} }
...@@ -23,10 +23,11 @@ describe('Middleware Redirect', () => { ...@@ -23,10 +23,11 @@ describe('Middleware Redirect', () => {
function tests() { function tests() {
it('should redirect correctly with redirect in next.config.js', async () => { it('should redirect correctly with redirect in next.config.js', async () => {
const browser = await webdriver(next.url, '/') const browser = await webdriver(next.url, '/')
await browser.eval('window.beforeNav = 1')
await browser.eval('window.next.router.push("/to-new")') await browser.eval('window.next.router.push("/to-new")')
await browser.waitForElementByCss('#dynamic') await browser.waitForElementByCss('#dynamic')
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.elementByCss('#dynamic').text()).toBe(
'Welcome to a /dynamic/[slug]: new'
)
}) })
it('does not include the locale in redirects by default', async () => { it('does not include the locale in redirects by default', async () => {
......
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