Skip to content

Commit

Permalink
tweak cache + revalidate fetch warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Nov 15, 2023
1 parent 9ab8828 commit 0c60ccf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,13 @@ export function patchFetch({
typeof _cache === 'string' &&
typeof curRevalidate !== 'undefined'
) {
Log.warn(
`fetch for ${fetchUrl} on ${staticGenerationStore.urlPathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
)
// when providing fetch with a Request input, it'll automatically set a cache value of 'default'
// we only want to warn if the user is explicitly setting a cache value
if (!(isRequestInput && _cache === 'default')) {
Log.warn(
`fetch for ${fetchUrl} on ${staticGenerationStore.urlPathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
)
}
_cache = undefined
}

Expand Down
26 changes: 26 additions & 0 deletions test/e2e/app-dir/logging/app/default-cache/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ export default async function Page() {
'https://next-data-api-endpoint.vercel.app/api/random?auto-cache'
).then((res) => res.text())

// the following fetch requests aren't rendered in UI, just asserted on in server logs
await fetch(
new Request(
'https://next-data-api-endpoint.vercel.app/api/random?request-input'
),
{
next: {
revalidate: 3,
},
}
)

await fetch(
new Request(
'https://next-data-api-endpoint.vercel.app/api/random?request-input-cache-override',
{
cache: 'force-cache',
}
),
{
next: {
revalidate: 3,
},
}
)

return (
<>
<p>/force-cache</p>
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/app-dir/logging/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,41 @@ createNextDescribe(
}, /success/)
})
}

describe('cache + revalidate warning', () => {
beforeAll(async () => {
await next.fetch('/default-cache')
})
it('should log when request input is a string', async () => {
await check(() => {
return next.cliOutput.includes(
'fetch for https://next-data-api-endpoint.vercel.app/api/random?revalidate-3-force-cache on /default-cache specified "cache: force-cache" and "revalidate: 3", only one should be specified'
)
? 'success'
: 'fail'
}, 'success')
})

it('should log when request input is a Request instance', async () => {
await check(() => {
return next.cliOutput.includes(
'fetch for https://next-data-api-endpoint.vercel.app/api/random?request-input-cache-override on /default-cache specified "cache: force-cache" and "revalidate: 3", only one should be specified.'
)
? 'success'
: 'fail'
}, 'success')
})

it('should not log when overriding cache within the Request object', async () => {
await check(() => {
return next.cliOutput.includes(
`fetch for https://next-data-api-endpoint.vercel.app/api/random?request-input on /default-cache specified "cache: default" and "revalidate: 3", only one should be specified.`
)
? 'fail'
: 'success'
}, 'success')
})
})
}

describe('with verbose logging', () => {
Expand Down

0 comments on commit 0c60ccf

Please sign in to comment.