Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak cache + revalidate fetch warning #58505

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 40 additions & 0 deletions test/e2e/app-dir/logging/app/cache-revalidate/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export const fetchCache = 'default-cache'

export default async function Page() {
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,
},
}
)

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

return <div>Hello World!</div>
}
50 changes: 50 additions & 0 deletions test/e2e/app-dir/logging/fetch-warning.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { check } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'app-dir - fetch warnings',
{
skipDeployment: true,
files: __dirname,
},
({ next }) => {
beforeAll(async () => {
// we don't need verbose logging (enabled by default in this Next app) for these tests to work
// we avoid enabling it since it's not currently compatible with Turbopack.
await next.stop()
await next.deleteFile('next.config.js')
await next.start()
await next.fetch('/cache-revalidate')
})

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?request-string on /cache-revalidate 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 /cache-revalidate 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 /cache-revalidate specified "cache: default" and "revalidate: 3", only one should be specified.`
)
? 'fail'
: 'success'
}, 'success')
})
}
)
Loading