Unverified Commit 6f698405 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update matched path params priority (#37646)

This ensures we use the correct dynamic route params favoring params from the URL/matched-path over route-matches. This also ensures we properly cache `_next/data` requests client side when the page is not a `getServerSideProps` page. 

x-ref: https://github.com/vercel/next.js/pull/37574

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 89b961e1
...@@ -351,6 +351,7 @@ export function getUtils({ ...@@ -351,6 +351,7 @@ export function getUtils({
// on the parsed params, this is used to signal if we need // on the parsed params, this is used to signal if we need
// to parse x-now-route-matches or not // to parse x-now-route-matches or not
const defaultValue = defaultRouteMatches![key] const defaultValue = defaultRouteMatches![key]
const isOptional = defaultRouteRegex!.groups[key].optional
const isDefaultValue = Array.isArray(defaultValue) const isDefaultValue = Array.isArray(defaultValue)
? defaultValue.some((defaultVal) => { ? defaultValue.some((defaultVal) => {
...@@ -360,14 +361,14 @@ export function getUtils({ ...@@ -360,14 +361,14 @@ export function getUtils({
}) })
: value?.includes(defaultValue as string) : value?.includes(defaultValue as string)
if (isDefaultValue || typeof value === 'undefined') { if (isDefaultValue || (typeof value === 'undefined' && !isOptional)) {
hasValidParams = false hasValidParams = false
} }
// non-provided optional values should be undefined so normalize // non-provided optional values should be undefined so normalize
// them to undefined // them to undefined
if ( if (
defaultRouteRegex!.groups[key].optional && isOptional &&
(!value || (!value ||
(Array.isArray(value) && (Array.isArray(value) &&
value.length === 1 && value.length === 1 &&
......
...@@ -107,9 +107,13 @@ export async function downloadWasmSwc( ...@@ -107,9 +107,13 @@ export async function downloadWasmSwc(
const cacheFiles = await fs.promises.readdir(cacheDirectory) const cacheFiles = await fs.promises.readdir(cacheDirectory)
if (cacheFiles.length > MAX_VERSIONS_TO_CACHE) { if (cacheFiles.length > MAX_VERSIONS_TO_CACHE) {
cacheFiles.sort() cacheFiles.sort((a, b) => {
if (a.length < b.length) return -1
return a.localeCompare(b)
})
for (let i = MAX_VERSIONS_TO_CACHE - 1; i++; i < cacheFiles.length) { // prune oldest versions in cache
for (let i = 0; i++; i < cacheFiles.length - MAX_VERSIONS_TO_CACHE) {
await fs.promises await fs.promises
.unlink(path.join(cacheDirectory, cacheFiles[i])) .unlink(path.join(cacheDirectory, cacheFiles[i]))
.catch(() => {}) .catch(() => {})
......
...@@ -457,6 +457,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -457,6 +457,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (urlPathname.startsWith(`/_next/data/`)) { if (urlPathname.startsWith(`/_next/data/`)) {
parsedUrl.query.__nextDataReq = '1' parsedUrl.query.__nextDataReq = '1'
} }
const normalizedUrlPath = this.stripNextDataPath(urlPathname)
matchedPath = this.stripNextDataPath(matchedPath, false) matchedPath = this.stripNextDataPath(matchedPath, false)
if (this.nextConfig.i18n) { if (this.nextConfig.i18n) {
...@@ -512,15 +513,46 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -512,15 +513,46 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (pageIsDynamic) { if (pageIsDynamic) {
let params: ParsedUrlQuery | false = {} let params: ParsedUrlQuery | false = {}
const paramsResult = utils.normalizeDynamicRouteParams( let paramsResult = utils.normalizeDynamicRouteParams(
parsedUrl.query parsedUrl.query
) )
if (
!isDynamicRoute(normalizedUrlPath) ||
!isDynamicRoute(matchedPath)
) {
// we favor matching against req.url although if there's a
// rewrite and it's SSR we use the x-matched-path instead
let matcherRes = utils.dynamicRouteMatcher?.(normalizedUrlPath)
if (!matcherRes) {
matcherRes = utils.dynamicRouteMatcher?.(matchedPath)
}
const parsedResult = utils.normalizeDynamicRouteParams(
matcherRes || {}
)
if (parsedResult.hasValidParams) {
if (paramsResult.hasValidParams) {
Object.assign(paramsResult.params, parsedResult.params)
} else {
paramsResult = parsedResult
}
}
}
if (paramsResult.hasValidParams) { if (paramsResult.hasValidParams) {
params = paramsResult.params params = paramsResult.params
} else if (req.headers['x-now-route-matches']) { }
if (
req.headers['x-now-route-matches'] &&
(!paramsResult.hasValidParams ||
(isDynamicRoute(matchedPath) &&
isDynamicRoute(normalizedUrlPath)))
) {
const opts: Record<string, string> = {} const opts: Record<string, string> = {}
params = utils.getParamsFromRouteMatches( const routeParams = utils.getParamsFromRouteMatches(
req, req,
opts, opts,
parsedUrl.query.__nextLocale || '' parsedUrl.query.__nextLocale || ''
...@@ -529,15 +561,17 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -529,15 +561,17 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (opts.locale) { if (opts.locale) {
parsedUrl.query.__nextLocale = opts.locale parsedUrl.query.__nextLocale = opts.locale
} }
} else { paramsResult = utils.normalizeDynamicRouteParams(routeParams)
params = utils.dynamicRouteMatcher!(matchedPath) || {}
if (paramsResult.hasValidParams) {
params = paramsResult.params
}
} }
if (params) { if (params) {
if (!paramsResult.hasValidParams) { if (!paramsResult.hasValidParams) {
params = utils.normalizeDynamicRouteParams(params).params params = utils.normalizeDynamicRouteParams(params).params
} }
matchedPath = utils.interpolateDynamicPath(srcPathname, params) matchedPath = utils.interpolateDynamicPath(srcPathname, params)
req.url = utils.interpolateDynamicPath(req.url!, params) req.url = utils.interpolateDynamicPath(req.url!, params)
} }
......
...@@ -456,6 +456,17 @@ interface FetchDataOutput { ...@@ -456,6 +456,17 @@ interface FetchDataOutput {
text: string text: string
} }
interface FetchNextDataParams {
dataHref: string
isServerRender: boolean
parseJSON: boolean | undefined
hasMiddleware?: boolean
inflightCache: NextDataCache
persistCache: boolean
isPrefetch: boolean
isBackground?: boolean
}
function fetchNextData({ function fetchNextData({
dataHref, dataHref,
inflightCache, inflightCache,
...@@ -464,15 +475,8 @@ function fetchNextData({ ...@@ -464,15 +475,8 @@ function fetchNextData({
isServerRender, isServerRender,
parseJSON, parseJSON,
persistCache, persistCache,
}: { isBackground,
dataHref: string }: FetchNextDataParams): Promise<FetchDataOutput> {
isServerRender: boolean
parseJSON: boolean | undefined
hasMiddleware?: boolean
inflightCache: NextDataCache
persistCache: boolean
isPrefetch: boolean
}): Promise<FetchDataOutput> {
const { href: cacheKey } = new URL(dataHref, window.location.href) const { href: cacheKey } = new URL(dataHref, window.location.href)
const getData = (params?: { method?: 'HEAD' | 'GET' }) => const getData = (params?: { method?: 'HEAD' | 'GET' }) =>
fetchRetry(dataHref, isServerRender ? 3 : 1, { fetchRetry(dataHref, isServerRender ? 3 : 1, {
...@@ -560,18 +564,11 @@ function fetchNextData({ ...@@ -560,18 +564,11 @@ function fetchNextData({
}) })
if (inflightCache[cacheKey] !== undefined) { if (inflightCache[cacheKey] !== undefined) {
// we kick off a HEAD request in the background
// when a non-prefetch request is made to signal revalidation
if (!isPrefetch && persistCache && !backgroundCache[cacheKey]) {
backgroundCache[cacheKey] = getData({ method: 'HEAD' })
.catch(() => {})
.then(() => {
delete backgroundCache[cacheKey]
})
}
return inflightCache[cacheKey] return inflightCache[cacheKey]
} }
return (inflightCache[cacheKey] = getData()) return (inflightCache[cacheKey] = getData(
isBackground ? { method: 'HEAD' } : {}
))
} }
function tryToParseAsJSON(text: string) { function tryToParseAsJSON(text: string) {
...@@ -1573,22 +1570,23 @@ export default class Router implements BaseRouter { ...@@ -1573,22 +1570,23 @@ export default class Router implements BaseRouter {
? existingInfo ? existingInfo
: undefined : undefined
const fetchNextDataParams: FetchNextDataParams = {
dataHref: this.pageLoader.getDataHref({
href: formatWithValidation({ pathname, query }),
skipInterpolation: true,
asPath: resolvedAs,
locale,
}),
hasMiddleware: true,
isServerRender: this.isSsr,
parseJSON: true,
inflightCache: this.sdc,
persistCache: !isPreview,
isPrefetch: false,
}
const data = await withMiddlewareEffects({ const data = await withMiddlewareEffects({
fetchData: () => fetchData: () => fetchNextData(fetchNextDataParams),
fetchNextData({
dataHref: this.pageLoader.getDataHref({
href: formatWithValidation({ pathname, query }),
skipInterpolation: true,
asPath: resolvedAs,
locale,
}),
hasMiddleware: true,
isServerRender: this.isSsr,
parseJSON: true,
inflightCache: this.sdc,
persistCache: !isPreview,
isPrefetch: false,
}),
asPath: resolvedAs, asPath: resolvedAs,
locale: locale, locale: locale,
router: this, router: this,
...@@ -1633,13 +1631,6 @@ export default class Router implements BaseRouter { ...@@ -1633,13 +1631,6 @@ export default class Router implements BaseRouter {
}) })
)) ))
// TODO: we only bust the data cache for SSP routes
// although middleware can skip cache per request with
// x-middleware-cache: no-cache
if (routeInfo.__N_SSP && data?.dataHref) {
delete this.sdc[data?.dataHref]
}
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const { isValidElementType } = require('next/dist/compiled/react-is') const { isValidElementType } = require('next/dist/compiled/react-is')
if (!isValidElementType(routeInfo.Component)) { if (!isValidElementType(routeInfo.Component)) {
...@@ -1674,7 +1665,7 @@ export default class Router implements BaseRouter { ...@@ -1674,7 +1665,7 @@ export default class Router implements BaseRouter {
isServerRender: this.isSsr, isServerRender: this.isSsr,
parseJSON: true, parseJSON: true,
inflightCache: this.sdc, inflightCache: this.sdc,
persistCache: !!routeInfo.__N_SSG && !isPreview, persistCache: !isPreview,
isPrefetch: false, isPrefetch: false,
})) }))
...@@ -1700,6 +1691,33 @@ export default class Router implements BaseRouter { ...@@ -1700,6 +1691,33 @@ export default class Router implements BaseRouter {
} }
}) })
// Only bust the data cache for SSP routes although
// middleware can skip cache per request with
// x-middleware-cache: no-cache as well
if (routeInfo.__N_SSP && fetchNextDataParams.dataHref) {
const cacheKey = new URL(
fetchNextDataParams.dataHref,
window.location.href
).href
delete this.sdc[cacheKey]
}
// we kick off a HEAD request in the background
// when a non-prefetch request is made to signal revalidation
if (
!this.isPreview &&
routeInfo.__N_SSG &&
process.env.NODE_ENV !== 'development'
) {
fetchNextData(
Object.assign({}, fetchNextDataParams, {
isBackground: true,
persistCache: false,
inflightCache: backgroundCache,
})
).catch(() => {})
}
if (routeInfo.__N_RSC) { if (routeInfo.__N_RSC) {
props.pageProps = Object.assign(props.pageProps, { props.pageProps = Object.assign(props.pageProps, {
__flight__: useStreamedFlightData __flight__: useStreamedFlightData
...@@ -1906,8 +1924,8 @@ export default class Router implements BaseRouter { ...@@ -1906,8 +1924,8 @@ export default class Router implements BaseRouter {
isServerRender: this.isSsr, isServerRender: this.isSsr,
parseJSON: true, parseJSON: true,
inflightCache: this.sdc, inflightCache: this.sdc,
persistCache: false, persistCache: !this.isPreview,
isPrefetch: false, isPrefetch: true,
}), }),
asPath: asPath, asPath: asPath,
locale: locale, locale: locale,
......
...@@ -194,6 +194,31 @@ describe('Middleware Rewrite', () => { ...@@ -194,6 +194,31 @@ describe('Middleware Rewrite', () => {
expect(await element.text()).toEqual('About Bypassed Page') expect(await element.text()).toEqual('About Bypassed Page')
}) })
if (!(global as any).isNextDev) {
it('should cache data requests correctly', async () => {
const browser = await webdriver(next.url, '/')
await check(async () => {
const hrefs = await browser.eval(
`Object.keys(window.next.router.sdc)`
)
for (const url of [
'/en/about.json?override=external',
'/en/about.json?override=internal',
'/en/rewrite-me-external-twice.json',
'/en/rewrite-me-to-about.json?override=internal',
'/en/rewrite-me-to-vercel.json',
'/en/rewrite-to-ab-test.json',
]) {
if (!hrefs.some((href) => href.includes(url))) {
return JSON.stringify(hrefs, null, 2)
}
}
return 'yes'
}, 'yes')
})
}
it(`should allow to rewrite keeping the locale in pathname`, async () => { it(`should allow to rewrite keeping the locale in pathname`, async () => {
const res = await fetchViaHTTP(next.url, '/fr/country', { const res = await fetchViaHTTP(next.url, '/fr/country', {
country: 'spain', country: 'spain',
......
...@@ -71,7 +71,11 @@ describe('Middleware Production Prefetch', () => { ...@@ -71,7 +71,11 @@ describe('Middleware Production Prefetch', () => {
const mapped = hrefs.map((href) => const mapped = hrefs.map((href) =>
new URL(href).pathname.replace(/^\/_next\/data\/[^/]+/, '') new URL(href).pathname.replace(/^\/_next\/data\/[^/]+/, '')
) )
assert.deepEqual(mapped, ['/ssg-page.json']) assert.deepEqual(mapped, [
'/made-up.json',
'/ssg-page-2.json',
'/ssg-page.json',
])
return 'yes' return 'yes'
}, 'yes') }, 'yes')
}) })
......
...@@ -533,7 +533,7 @@ describe('Required Server Files', () => { ...@@ -533,7 +533,7 @@ describe('Required Server Files', () => {
}) })
it('should match the root dynamic page correctly', async () => { it('should match the root dynamic page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index', undefined, { const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
headers: { headers: {
'x-matched-path': '/[slug]', 'x-matched-path': '/[slug]',
}, },
......
...@@ -448,7 +448,7 @@ describe('should set-up next', () => { ...@@ -448,7 +448,7 @@ describe('should set-up next', () => {
const html3 = await renderViaHTTP( const html3 = await renderViaHTTP(
appPort, appPort,
'/catch-all/[[..rest]]', '/catch-all/[[...rest]]',
undefined, undefined,
{ {
headers: { headers: {
...@@ -732,7 +732,7 @@ describe('should set-up next', () => { ...@@ -732,7 +732,7 @@ describe('should set-up next', () => {
}) })
it('should match the root dyanmic page correctly', async () => { it('should match the root dyanmic page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index', undefined, { const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
headers: { headers: {
'x-matched-path': '/[slug]', 'x-matched-path': '/[slug]',
}, },
......
...@@ -494,6 +494,59 @@ describe('should set-up next', () => { ...@@ -494,6 +494,59 @@ describe('should set-up next', () => {
expect(data2.random).not.toBe(data.random) expect(data2.random).not.toBe(data.random)
}) })
it('should favor valid route params over routes-matches', async () => {
const html = await renderViaHTTP(appPort, '/fallback/first', undefined, {
headers: {
'x-matched-path': '/fallback/first',
'x-now-route-matches': '1=fallback%2ffirst',
},
})
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect($('#fallback').text()).toBe('fallback page')
expect($('#slug').text()).toBe('first')
expect(data.hello).toBe('world')
const html2 = await renderViaHTTP(appPort, `/fallback/second`, undefined, {
headers: {
'x-matched-path': '/fallback/[slug]',
'x-now-route-matches': '1=fallback%2fsecond',
},
})
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect($2('#fallback').text()).toBe('fallback page')
expect($2('#slug').text()).toBe('second')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)
})
it('should favor valid route params over routes-matches optional', async () => {
const html = await renderViaHTTP(appPort, '/optional-ssg', undefined, {
headers: {
'x-matched-path': '/optional-ssg',
'x-now-route-matches': '1=optional-ssg',
},
})
const $ = cheerio.load(html)
const data = JSON.parse($('#props').text())
expect(data.params).toEqual({})
const html2 = await renderViaHTTP(appPort, `/optional-ssg`, undefined, {
headers: {
'x-matched-path': '/optional-ssg',
'x-now-route-matches': '1=optional-ssg%2fanother',
},
})
const $2 = cheerio.load(html2)
const data2 = JSON.parse($2('#props').text())
expect(isNaN(data2.random)).toBe(false)
expect(data2.params).toEqual({})
})
it('should return data correctly with x-matched-path', async () => { it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
appPort, appPort,
...@@ -569,7 +622,7 @@ describe('should set-up next', () => { ...@@ -569,7 +622,7 @@ describe('should set-up next', () => {
const html3 = await renderViaHTTP( const html3 = await renderViaHTTP(
appPort, appPort,
'/catch-all/[[..rest]]', '/catch-all/[[...rest]]',
undefined, undefined,
{ {
headers: { headers: {
...@@ -966,7 +1019,7 @@ describe('should set-up next', () => { ...@@ -966,7 +1019,7 @@ describe('should set-up next', () => {
}) })
it('should match the root dynamic page correctly', async () => { it('should match the root dynamic page correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index', undefined, { const res = await fetchViaHTTP(appPort, '/slug-1', undefined, {
headers: { headers: {
'x-matched-path': '/[slug]', 'x-matched-path': '/[slug]',
}, },
......
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