-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add partial support for
"use cache"
in metadata route handlers (#74835
) Adds support for using `"use cache"` in the special metadata route handlers like [`sitemap.ts`](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generating-a-sitemap-using-code-js-ts), [`opengraph-image.tsx`](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/opengraph-image#generate-images-using-code-js-ts-tsx), [`icon.tsx`](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/app-icons#generate-icons-using-code-js-ts-tsx), and other [metadata files](https://nextjs.org/docs/app/api-reference/file-conventions/metadata). reverts #71225 fixes #74146 closes NAR-51 As a follow-up we need to ensure that opengraph image responses do not bail out of static generation when `dynamicIO` is enabled.
- Loading branch information
1 parent
58255f9
commit 6cc85de
Showing
16 changed files
with
610 additions
and
352 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
38 changes: 38 additions & 0 deletions
38
test/e2e/app-dir/use-cache-metadata-route-handler/app/icon.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' | ||
import { setTimeout } from 'timers/promises' | ||
|
||
export const size = { width: 32, height: 32 } | ||
export const contentType = 'image/png' | ||
|
||
async function fetchIconLetter() { | ||
'use cache' | ||
|
||
// Simulate I/O | ||
await setTimeout(100) | ||
|
||
return 'N' | ||
} | ||
|
||
export default async function Icon() { | ||
const letter = await fetchIconLetter() | ||
|
||
return new ImageResponse( | ||
( | ||
<div | ||
style={{ | ||
fontSize: 24, | ||
background: 'black', | ||
width: '100%', | ||
height: '100%', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
color: 'white', | ||
}} | ||
> | ||
{letter} | ||
</div> | ||
), | ||
{ ...size } | ||
) | ||
} |
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/use-cache-metadata-route-handler/app/manifest.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,12 @@ | ||
import type { MetadataRoute } from 'next' | ||
import { getSentinelValue } from './sentinel' | ||
import { setTimeout } from 'timers/promises' | ||
|
||
export default async function manifest(): Promise<MetadataRoute.Manifest> { | ||
'use cache' | ||
|
||
// Simulate I/O | ||
await setTimeout(100) | ||
|
||
return { name: getSentinelValue() } | ||
} |
38 changes: 38 additions & 0 deletions
38
test/e2e/app-dir/use-cache-metadata-route-handler/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' | ||
|
||
export const alt = 'About Acme' | ||
export const size = { width: 1200, height: 630 } | ||
export const contentType = 'image/png' | ||
|
||
async function fetchPostData() { | ||
'use cache' | ||
|
||
return { title: 'Test', created: Date.now() } | ||
} | ||
|
||
export default async function Image() { | ||
const post = await fetchPostData() | ||
|
||
return new ImageResponse( | ||
( | ||
<div | ||
style={{ | ||
fontSize: 48, | ||
background: 'white', | ||
width: '100%', | ||
height: '100%', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
flexDirection: 'column', | ||
}} | ||
> | ||
<h1>{post.title}</h1> | ||
<p style={{ fontSize: 32 }}> | ||
{new Date(post.created).toLocaleTimeString()} | ||
</p> | ||
</div> | ||
), | ||
size | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
test/e2e/app-dir/use-cache-metadata-route-handler/app/products/sitemap.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,20 @@ | ||
import type { MetadataRoute } from 'next' | ||
import { getSentinelValue } from '../sentinel' | ||
import { setTimeout } from 'timers/promises' | ||
|
||
export async function generateSitemaps() { | ||
return [{ id: 0 }, { id: 1 }] | ||
} | ||
|
||
export default async function sitemap({ | ||
id, | ||
}: { | ||
id: number | ||
}): Promise<MetadataRoute.Sitemap> { | ||
'use cache' | ||
|
||
// Simulate I/O | ||
await setTimeout(100) | ||
|
||
return [{ url: `https://acme.com/${id}?sentinel=${getSentinelValue()}` }] | ||
} |
14 changes: 14 additions & 0 deletions
14
test/e2e/app-dir/use-cache-metadata-route-handler/app/robots.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,14 @@ | ||
import type { MetadataRoute } from 'next' | ||
import { getSentinelValue } from './sentinel' | ||
import { setTimeout } from 'timers/promises' | ||
|
||
export default async function robots(): Promise<MetadataRoute.Robots> { | ||
'use cache' | ||
|
||
// Simulate I/O | ||
await setTimeout(100) | ||
|
||
return { | ||
rules: { userAgent: '*', allow: `/${getSentinelValue()}` }, | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/use-cache-metadata-route-handler/app/sentinel.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,7 @@ | ||
const { PHASE_PRODUCTION_BUILD } = require('next/constants') | ||
|
||
export function getSentinelValue() { | ||
return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD | ||
? 'buildtime' | ||
: 'runtime' | ||
} |
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/use-cache-metadata-route-handler/app/sitemap.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,12 @@ | ||
import type { MetadataRoute } from 'next' | ||
import { getSentinelValue } from './sentinel' | ||
import { setTimeout } from 'timers/promises' | ||
|
||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> { | ||
'use cache' | ||
|
||
// Simulate I/O | ||
await setTimeout(100) | ||
|
||
return [{ url: `https://acme.com?sentinel=${getSentinelValue()}` }] | ||
} |
10 changes: 10 additions & 0 deletions
10
test/e2e/app-dir/use-cache-metadata-route-handler/next.config.js
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,10 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
experimental: { | ||
dynamicIO: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
Oops, something went wrong.