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
This commit is contained in:
Jiachi Liu 2023-04-18 14:13:55 +02:00 committed by GitHub
parent 902bb40454
commit 96923ed894
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 4 deletions

View file

@ -1,4 +1,4 @@
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'
)

2
test/.gitignore vendored
View file

@ -1,2 +1,4 @@
!node_modules
.vscode
e2e/**/tsconfig.json

View file

@ -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')

View file

@ -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')
}