Unverified Commit 96923ed8 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix static opt of routes generation for static metadata files (#48528)

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

For static metadata files, we should always generate static routes
instead of generate dynamic routes, so that they won't be deployed as
serverless functions which executing file reading in deployment
parent 902bb404
import { isMetadataRoute } from './is-metadata-route'
import { isMetadataRoute, isMetadataRouteFile } from './is-metadata-route'
import path from '../../shared/lib/isomorphic/path'
import { djb2Hash } from '../../shared/lib/hash'
......@@ -47,16 +47,18 @@ export function normalizeMetadataRoute(page: string) {
// Support both /<metadata-route.ext> and custom routes /<metadata-route>/route.ts.
// If it's a metadata file route, we need to append /[id]/route to the page.
if (!route.endsWith('/route')) {
const { dir, name, ext } = path.parse(route)
const isStaticMetadataFile = isMetadataRouteFile(route, [], true)
const { dir, name: baseName, ext } = path.parse(route)
const isSingleRoute =
page.startsWith('/sitemap') ||
page.startsWith('/robots') ||
page.startsWith('/manifest') ||
page === '/favicon.ico'
isStaticMetadataFile
route = path.join(
dir,
`${name}${suffix ? `-${suffix}` : ''}${ext}`,
`${baseName}${suffix ? `-${suffix}` : ''}${ext}`,
isSingleRoute ? '' : '[[...__metadata_id__]]',
'route'
)
......
!node_modules
.vscode
e2e/**/tsconfig.json
......@@ -782,6 +782,28 @@ createNextDescribe(
}
})
if (isNextStart) {
describe('static optimization', () => {
it('should build static files into static route', async () => {
expect(
await next.hasFile(
'.next/server/app/opengraph/static/opengraph-image.png.meta'
)
).toBe(true)
expect(
await next.hasFile(
'.next/server/app/opengraph/static/opengraph-image.png.body'
)
).toBe(true)
expect(
await next.hasFile(
'.next/server/app/opengraph/static/opengraph-image.png/[[...__metadata_id__]]/route.js'
)
).toBe(false)
})
})
}
describe('react cache', () => {
it('should have same title and page value on initial load', async () => {
const browser = await next.browser('/cache-deduping')
......
......@@ -384,6 +384,9 @@ export class NextInstance {
}
// TODO: block these in deploy mode
public async hasFile(filename: string) {
return fs.pathExists(path.join(this.testDir, filename))
}
public async readFile(filename: string) {
return fs.readFile(path.join(this.testDir, filename), 'utf8')
}
......
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