Commit 25716c8e authored by James Daniels's avatar James Daniels Committed by Jiachi Liu

Deployment adapter: fix metadata for "/" route (#85820)

Updates cases where `/` isn't normalized fully when interpolating into
data paths and adds regression tests.

---------
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent bf9c9004
...@@ -727,8 +727,7 @@ export async function handleBuildComplete({ ...@@ -727,8 +727,7 @@ export async function handleBuildComplete({
// need to add matching .rsc output // need to add matching .rsc output
if (isAppPage) { if (isAppPage) {
const rscPathname = const rscPathname = normalizePagePath(output.pathname) + '.rsc'
(output.pathname === '/' ? '/index' : output.pathname) + '.rsc'
outputs.appPages.push({ outputs.appPages.push({
...output, ...output,
pathname: rscPathname, pathname: rscPathname,
...@@ -864,7 +863,7 @@ export async function handleBuildComplete({ ...@@ -864,7 +863,7 @@ export async function handleBuildComplete({
const dataPathname = path.posix.join( const dataPathname = path.posix.join(
'/_next/data', '/_next/data',
buildId, buildId,
page + '.json' normalizePagePath(page) + '.json'
) )
outputs.pages.push({ outputs.pages.push({
...output, ...output,
...@@ -1002,9 +1001,8 @@ export async function handleBuildComplete({ ...@@ -1002,9 +1001,8 @@ export async function handleBuildComplete({
if (output.type === AdapterOutputType.APP_PAGE) { if (output.type === AdapterOutputType.APP_PAGE) {
outputs.appPages.push({ outputs.appPages.push({
...output, ...output,
pathname: pathname: normalizePagePath(output.pathname) + '.rsc',
(output.pathname === '/' ? '/index' : output.pathname) + '.rsc', id: normalizePagePath(output.pathname) + '.rsc',
id: (output.id === '/' ? '/index' : output.pathname) + '.rsc',
}) })
outputs.appPages.push(output) outputs.appPages.push(output)
} else { } else {
...@@ -1058,15 +1056,18 @@ export async function handleBuildComplete({ ...@@ -1058,15 +1056,18 @@ export async function handleBuildComplete({
} }
if (meta?.segmentPaths) { if (meta?.segmentPaths) {
const normalizedRoute = normalizePagePath(route)
const segmentsDir = path.join( const segmentsDir = path.join(
appDistDir, appDistDir,
`${route}${prefetchSegmentDirSuffix}` `${normalizedRoute}${prefetchSegmentDirSuffix}`
) )
for (const segmentPath of meta.segmentPaths) { for (const segmentPath of meta.segmentPaths) {
const outputSegmentPath = const outputSegmentPath =
path.join(route + prefetchSegmentDirSuffix, segmentPath) + path.join(
prefetchSegmentSuffix normalizedRoute + prefetchSegmentDirSuffix,
segmentPath
) + prefetchSegmentSuffix
const fallbackPathname = path.join( const fallbackPathname = path.join(
segmentsDir, segmentsDir,
...@@ -1114,10 +1115,11 @@ export async function handleBuildComplete({ ...@@ -1114,10 +1115,11 @@ export async function handleBuildComplete({
route: string, route: string,
isAppPage: boolean isAppPage: boolean
): Promise<AppRouteMeta> => { ): Promise<AppRouteMeta> => {
const basename = route.endsWith('/') ? `${route}index` : route
const meta: AppRouteMeta = isAppPage const meta: AppRouteMeta = isAppPage
? JSON.parse( ? JSON.parse(
await fs await fs
.readFile(path.join(appDistDir, `${route}.meta`), 'utf8') .readFile(path.join(appDistDir, `${basename}.meta`), 'utf8')
.catch(() => '{}') .catch(() => '{}')
) )
: {} : {}
...@@ -1192,7 +1194,7 @@ export async function handleBuildComplete({ ...@@ -1192,7 +1194,7 @@ export async function handleBuildComplete({
let filePath = path.join( let filePath = path.join(
isAppPage ? appDistDir : pagesDistDir, isAppPage ? appDistDir : pagesDistDir,
`${route === '/' ? 'index' : route}.${isAppPage && !dataRoute ? 'body' : 'html'}` `${normalizePagePath(route)}.${isAppPage && !dataRoute ? 'body' : 'html'}`
) )
// we use the static 404 for notFound: true if available // we use the static 404 for notFound: true if available
...@@ -1268,7 +1270,7 @@ export async function handleBuildComplete({ ...@@ -1268,7 +1270,7 @@ export async function handleBuildComplete({
if (dataRoute) { if (dataRoute) {
let dataFilePath = path.join( let dataFilePath = path.join(
pagesDistDir, pagesDistDir,
`${route === '/' ? 'index' : route}.json` `${normalizePagePath(route)}.json`
) )
if (isAppPage) { if (isAppPage) {
...@@ -1593,16 +1595,18 @@ export async function handleBuildComplete({ ...@@ -1593,16 +1595,18 @@ export async function handleBuildComplete({
prefixRouteKeys: true, prefixRouteKeys: true,
includeSuffix: true, includeSuffix: true,
}) })
const isDataRoute = dataRoutePages.has(page)
const destination = path.posix.join( const destination = path.posix.join(
'/', '/',
config.basePath, config.basePath,
...(dataRoutePages.has(page) ? [`_next/data`, buildId] : ''), ...(isDataRoute ? [`_next/data`, buildId] : ''),
...(page === '/' ...(page === '/'
? [shouldLocalize ? '$nextLocale.json' : 'index.json'] ? [shouldLocalize ? '$nextLocale.json' : 'index.json']
: [ : [
shouldLocalize ? '$nextLocale' : '', shouldLocalize ? '$nextLocale' : '',
page + page +
'.json' + (isDataRoute ? '.json' : '') +
getDestinationQuery(routeRegex.routeKeys || {}), getDestinationQuery(routeRegex.routeKeys || {}),
]) ])
) )
......
...@@ -99,6 +99,8 @@ describe('adapter-config', () => { ...@@ -99,6 +99,8 @@ describe('adapter-config', () => {
} else { } else {
expect(output.pathname).toStartWith('/docs/_next/static') expect(output.pathname).toStartWith('/docs/_next/static')
} }
// ensure / -> /index normalizing is correct
expect(output.pathname.includes('/.')).toBe(false)
const stats = await fs.promises.stat(output.filePath) const stats = await fs.promises.stat(output.filePath)
expect(stats.isFile()).toBe(true) expect(stats.isFile()).toBe(true)
...@@ -118,18 +120,35 @@ describe('adapter-config', () => { ...@@ -118,18 +120,35 @@ describe('adapter-config', () => {
expect(typeof prerenderOutput.config.bypassToken).toBe('string') expect(typeof prerenderOutput.config.bypassToken).toBe('string')
expect(Array.isArray(prerenderOutput.config.allowHeader)).toBe(true) expect(Array.isArray(prerenderOutput.config.allowHeader)).toBe(true)
expect(Array.isArray(prerenderOutput.config.allowQuery)).toBe(true) expect(Array.isArray(prerenderOutput.config.allowQuery)).toBe(true)
// ensure / -> /index normalizing is correct
expect(prerenderOutput.pathname.includes('/.')).toBe(false)
} catch (err) { } catch (err) {
require('console').error(`invalid prerender ${prerenderOutput.id}`, err) require('console').error(`invalid prerender ${prerenderOutput.id}`, err)
throw err throw err
} }
} }
const indexPrerender = prerenderOutputs.find(
(item) => item.pathname === '/docs'
)
expect(indexPrerender?.fallback?.initialHeaders).toEqual({
'content-type': 'text/html; charset=utf-8',
vary: 'rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch',
'x-next-cache-tags': '_N_T_/layout,_N_T_/page,_N_T_/,_N_T_/index',
'x-nextjs-prerender': '1',
'x-nextjs-stale-time': '300',
})
expect(indexPrerender?.fallback?.initialRevalidate).toBe(false)
for (const route of nodeOutputs) { for (const route of nodeOutputs) {
try { try {
expect(route.id).toBeString() expect(route.id).toBeString()
expect(route.config).toBeObject() expect(route.config).toBeObject()
expect(route.pathname).toBeString() expect(route.pathname).toBeString()
expect(route.runtime).toBe('nodejs') expect(route.runtime).toBe('nodejs')
// ensure / -> /index normalizing is correct
expect(route.pathname.includes('/.')).toBe(false)
const stats = await fs.promises.stat(route.filePath) const stats = await fs.promises.stat(route.filePath)
expect(stats.isFile()).toBe(true) expect(stats.isFile()).toBe(true)
...@@ -154,6 +173,8 @@ describe('adapter-config', () => { ...@@ -154,6 +173,8 @@ describe('adapter-config', () => {
expect(route.id).toBeString() expect(route.id).toBeString()
expect(route.config).toBeObject() expect(route.config).toBeObject()
expect(route.pathname).toBeString() expect(route.pathname).toBeString()
// ensure / -> /index normalizing is correct
expect(route.pathname.includes('/.')).toBe(false)
expect(route.runtime).toBe('edge') expect(route.runtime).toBe('edge')
expect(route.config.env).toEqual( expect(route.config.env).toEqual(
expect.objectContaining({ expect.objectContaining({
......
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