-
Notifications
You must be signed in to change notification settings - Fork 26.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure metadata routes dont skip static optimization (#66097)
Follow-up to #65825 this ensures we don't skip the static optimization specifically for metadata routes as this most often should be static as they aren't dynamic content and are requested very frequently.
- Loading branch information
Showing
8 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ImageResponse } from 'next/og' | ||
|
||
// Image metadata | ||
export const size = { | ||
width: 32, | ||
height: 32, | ||
} | ||
export const contentType = 'image/png' | ||
|
||
// Image generation | ||
export default function Icon() { | ||
return new ImageResponse( | ||
( | ||
// ImageResponse JSX element | ||
<div | ||
style={{ | ||
fontSize: 24, | ||
background: 'black', | ||
width: '100%', | ||
height: '100%', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
color: 'white', | ||
}} | ||
> | ||
A | ||
</div> | ||
), | ||
// ImageResponse options | ||
{ | ||
// For convenience, we can re-use the exported icons size metadata | ||
// config to also set the ImageResponse's width and height. | ||
...size, | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { MetadataRoute } from 'next' | ||
|
||
export default function manifest(): MetadataRoute.Manifest { | ||
return { | ||
name: 'Next.js App', | ||
short_name: 'Next.js App', | ||
description: 'Next.js App', | ||
start_url: '/', | ||
display: 'standalone', | ||
background_color: '#fff', | ||
theme_color: '#fff', | ||
icons: [ | ||
{ | ||
src: '/favicon.ico', | ||
sizes: 'any', | ||
type: 'image/x-icon', | ||
}, | ||
], | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
test/production/app-dir/metadata-static/app/opengraph-image.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ImageResponse } from 'next/og' | ||
|
||
// Image metadata | ||
export const alt = 'About Acme' | ||
export const size = { | ||
width: 1200, | ||
height: 630, | ||
} | ||
|
||
export const contentType = 'image/png' | ||
|
||
// Image generation | ||
export default async function Image() { | ||
return new ImageResponse( | ||
( | ||
// ImageResponse JSX element | ||
<div | ||
style={{ | ||
fontSize: 128, | ||
background: 'white', | ||
width: '100%', | ||
height: '100%', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}} | ||
> | ||
About Acme | ||
</div> | ||
), | ||
// ImageResponse options | ||
{ | ||
// For convenience, we can re-use the exported opengraph-image | ||
// size config to also set the ImageResponse's width and height. | ||
...size, | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { MetadataRoute } from 'next' | ||
|
||
export default function robots(): MetadataRoute.Robots { | ||
return { | ||
rules: { | ||
userAgent: '*', | ||
allow: '/', | ||
disallow: '/private/', | ||
}, | ||
sitemap: 'https://acme.com/sitemap.xml', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { MetadataRoute } from 'next' | ||
|
||
export default function sitemap(): MetadataRoute.Sitemap { | ||
return [ | ||
{ | ||
url: 'https://acme.com', | ||
lastModified: new Date(), | ||
changeFrequency: 'yearly', | ||
priority: 1, | ||
}, | ||
{ | ||
url: 'https://acme.com/about', | ||
lastModified: new Date(), | ||
changeFrequency: 'monthly', | ||
priority: 0.8, | ||
}, | ||
{ | ||
url: 'https://acme.com/blog', | ||
lastModified: new Date(), | ||
changeFrequency: 'weekly', | ||
priority: 0.5, | ||
}, | ||
] | ||
} |
27 changes: 27 additions & 0 deletions
27
test/production/app-dir/metadata-static/metadata-static.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('app dir - metadata', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('should have statically optimized metadata routes by default', async () => { | ||
const prerenderManifest = JSON.parse( | ||
await next.readFile('.next/prerender-manifest.json') | ||
) | ||
|
||
for (const key of [ | ||
'/robots.txt', | ||
'/sitemap', | ||
'/opengraph-image', | ||
'/manifest.webmanifest', | ||
]) { | ||
expect(prerenderManifest.routes[key]).toBeTruthy() | ||
expect(prerenderManifest.routes[key].initialRevalidateSeconds).toBe(false) | ||
|
||
const res = await next.fetch(key) | ||
expect(res.status).toBe(200) | ||
expect(res.headers.get('x-nextjs-cache')).toBe('HIT') | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters