Skip to content

Commit

Permalink
Fix rstatic opt of routes generation for static metadata files
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 18, 2023
1 parent 6106666 commit 7225b20
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/next/src/lib/metadata/get-metadata-route.ts
@@ -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'

Expand Down Expand Up @@ -32,7 +32,8 @@ export function normalizeMetadataRoute(page: string) {
let route = page
if (isMetadataRoute(page)) {
// Remove the file extension, e.g. /route-path/robots.txt -> /route-path
const pathnamePrefix = page.slice(0, -(path.basename(page).length + 1))
const { dir, name: baseName, ext } = path.parse(page)
const pathnamePrefix = page.slice(0, -(baseName.length + 1))
const suffix = getMetadataRouteSuffix(pathnamePrefix)

if (route === '/sitemap') {
Expand All @@ -47,16 +48,17 @@ 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(page, [], true)

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'
)
Expand Down
2 changes: 2 additions & 0 deletions test/.gitignore
@@ -1,2 +1,4 @@
!node_modules
.vscode

e2e/**/tsconfig.json
22 changes: 22 additions & 0 deletions test/e2e/app-dir/metadata/metadata.test.ts
Expand Up @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions test/lib/next-modes/base.ts
Expand Up @@ -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')
}
Expand Down

0 comments on commit 7225b20

Please sign in to comment.