Skip to content

Commit 3cd9264

Browse files
authored
tweak cache + revalidate fetch warning (#58505)
### What? When using a `Request` object with fetch, we'll log a warning indicating that using the `cache` property in addition to `revalidate` is unsupported. ### Why? `Request` sets some defaults on the request init, such as `cache: "default"`. This makes the warning confusing and there's no way to avoid it aside from switching the resource argument to be a URL string instead. ### How? This keeps existing behavior but omits the log in the case where a request object is used and no explicit cache overrides are specified. Fixes #58109
1 parent d6d6d56 commit 3cd9264

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

packages/next/src/server/lib/patch-fetch.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,13 @@ export function patchFetch({
266266
typeof _cache === 'string' &&
267267
typeof curRevalidate !== 'undefined'
268268
) {
269-
Log.warn(
270-
`fetch for ${fetchUrl} on ${staticGenerationStore.urlPathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
271-
)
269+
// when providing fetch with a Request input, it'll automatically set a cache value of 'default'
270+
// we only want to warn if the user is explicitly setting a cache value
271+
if (!(isRequestInput && _cache === 'default')) {
272+
Log.warn(
273+
`fetch for ${fetchUrl} on ${staticGenerationStore.urlPathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
274+
)
275+
}
272276
_cache = undefined
273277
}
274278

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export const fetchCache = 'default-cache'
2+
3+
export default async function Page() {
4+
await fetch(
5+
new Request(
6+
'https://next-data-api-endpoint.vercel.app/api/random?request-input'
7+
),
8+
{
9+
next: {
10+
revalidate: 3,
11+
},
12+
}
13+
)
14+
15+
await fetch(
16+
new Request(
17+
'https://next-data-api-endpoint.vercel.app/api/random?request-input-cache-override',
18+
{
19+
cache: 'force-cache',
20+
}
21+
),
22+
{
23+
next: {
24+
revalidate: 3,
25+
},
26+
}
27+
)
28+
29+
await fetch(
30+
'https://next-data-api-endpoint.vercel.app/api/random?request-string',
31+
{
32+
next: {
33+
revalidate: 3,
34+
},
35+
cache: 'force-cache',
36+
}
37+
)
38+
39+
return <div>Hello World!</div>
40+
}
File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { check } from 'next-test-utils'
2+
import { createNextDescribe } from 'e2e-utils'
3+
4+
createNextDescribe(
5+
'app-dir - fetch warnings',
6+
{
7+
skipDeployment: true,
8+
files: __dirname,
9+
},
10+
({ next }) => {
11+
beforeAll(async () => {
12+
// we don't need verbose logging (enabled by default in this Next app) for these tests to work
13+
// we avoid enabling it since it's not currently compatible with Turbopack.
14+
await next.stop()
15+
await next.deleteFile('next.config.js')
16+
await next.start()
17+
await next.fetch('/cache-revalidate')
18+
})
19+
20+
it('should log when request input is a string', async () => {
21+
await check(() => {
22+
return next.cliOutput.includes(
23+
'fetch for https://next-data-api-endpoint.vercel.app/api/random?request-string on /cache-revalidate specified "cache: force-cache" and "revalidate: 3", only one should be specified'
24+
)
25+
? 'success'
26+
: 'fail'
27+
}, 'success')
28+
})
29+
30+
it('should log when request input is a Request instance', async () => {
31+
await check(() => {
32+
return next.cliOutput.includes(
33+
'fetch for https://next-data-api-endpoint.vercel.app/api/random?request-input-cache-override on /cache-revalidate specified "cache: force-cache" and "revalidate: 3", only one should be specified.'
34+
)
35+
? 'success'
36+
: 'fail'
37+
}, 'success')
38+
})
39+
40+
it('should not log when overriding cache within the Request object', async () => {
41+
await check(() => {
42+
return next.cliOutput.includes(
43+
`fetch for https://next-data-api-endpoint.vercel.app/api/random?request-input on /cache-revalidate specified "cache: default" and "revalidate: 3", only one should be specified.`
44+
)
45+
? 'fail'
46+
: 'success'
47+
}, 'success')
48+
})
49+
}
50+
)

0 commit comments

Comments
 (0)