Skip to content

Commit

Permalink
fix: warning fetch for [object Request] specified (#50003)
Browse files Browse the repository at this point in the history
This PR fixes a warning that previous looked like the following:

```
Warning: fetch for [object Request] specified "cache: default" and "revalidate: 60", only one should be specified.
```
  • Loading branch information
styfle committed May 18, 2023
1 parent 0a81cc0 commit 783acc5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 7 additions & 9 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,17 @@ export function patchFetch({
// Error caused by malformed URL should be handled by native fetch
url = undefined
}
const fetchUrl = url?.href ?? ''
const fetchStart = Date.now()
const method = init?.method?.toUpperCase() || 'GET'

return await getTracer().trace(
AppRenderSpan.fetch,
{
kind: SpanKind.CLIENT,
spanName: ['fetch', method, url?.toString() ?? input.toString()]
.filter(Boolean)
.join(' '),
spanName: ['fetch', method, fetchUrl].filter(Boolean).join(' '),
attributes: {
'http.url': url?.toString(),
'http.url': fetchUrl,
'http.method': method,
'net.peer.name': url?.hostname,
'net.peer.port': url?.port || undefined,
Expand Down Expand Up @@ -175,7 +174,7 @@ export function patchFetch({
typeof curRevalidate !== 'undefined'
) {
console.warn(
`Warning: fetch for ${input.toString()} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
`Warning: fetch for ${fetchUrl} on ${staticGenerationStore.pathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
)
_cache = undefined
}
Expand Down Expand Up @@ -219,7 +218,7 @@ export function patchFetch({
if (isOnlyNoStore) {
if (_cache === 'force-cache' || revalidate === 0) {
throw new Error(
`cache: 'force-cache' used on fetch for ${input.toString()} with 'export const fetchCache = 'only-no-store'`
`cache: 'force-cache' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-no-store'`
)
}
revalidate = 0
Expand All @@ -228,7 +227,7 @@ export function patchFetch({

if (isOnlyCache && _cache === 'no-store') {
throw new Error(
`cache: 'no-store' used on fetch for ${input.toString()} with 'export const fetchCache = 'only-cache'`
`cache: 'no-store' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-cache'`
)
}

Expand Down Expand Up @@ -284,7 +283,7 @@ export function patchFetch({
try {
cacheKey =
await staticGenerationStore.incrementalCache.fetchCacheKey(
isRequestInput ? (input as Request).url : input.toString(),
fetchUrl,
isRequestInput ? (input as RequestInit) : init
)
} catch (err) {
Expand Down Expand Up @@ -329,7 +328,6 @@ export function patchFetch({
}
}

const fetchUrl = url?.toString() ?? ''
const fetchIdx = staticGenerationStore.nextFetchId ?? 1
staticGenerationStore.nextFetchId = fetchIdx + 1

Expand Down
6 changes: 6 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,12 @@ createNextDescribe(

return 'success'
}, 'success')

if (!isNextDeploy) {
expect(next.cliOutput).toContain(
'Warning: fetch for https://next-data-api-endpoint.vercel.app/api/random?d4 on /force-cache specified "cache: force-cache" and "revalidate: 3", only one should be specified.'
)
}
})

it('should cache correctly for cache: no-store', async () => {
Expand Down

0 comments on commit 783acc5

Please sign in to comment.