Unverified Commit 783acc59 authored by Steven's avatar Steven Committed by GitHub

fix: warning fetch for `[object Request]` specified (#50003)

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.
```
parent 0a81cc0f
...@@ -91,6 +91,7 @@ export function patchFetch({ ...@@ -91,6 +91,7 @@ export function patchFetch({
// Error caused by malformed URL should be handled by native fetch // Error caused by malformed URL should be handled by native fetch
url = undefined url = undefined
} }
const fetchUrl = url?.href ?? ''
const fetchStart = Date.now() const fetchStart = Date.now()
const method = init?.method?.toUpperCase() || 'GET' const method = init?.method?.toUpperCase() || 'GET'
...@@ -98,11 +99,9 @@ export function patchFetch({ ...@@ -98,11 +99,9 @@ export function patchFetch({
AppRenderSpan.fetch, AppRenderSpan.fetch,
{ {
kind: SpanKind.CLIENT, kind: SpanKind.CLIENT,
spanName: ['fetch', method, url?.toString() ?? input.toString()] spanName: ['fetch', method, fetchUrl].filter(Boolean).join(' '),
.filter(Boolean)
.join(' '),
attributes: { attributes: {
'http.url': url?.toString(), 'http.url': fetchUrl,
'http.method': method, 'http.method': method,
'net.peer.name': url?.hostname, 'net.peer.name': url?.hostname,
'net.peer.port': url?.port || undefined, 'net.peer.port': url?.port || undefined,
...@@ -175,7 +174,7 @@ export function patchFetch({ ...@@ -175,7 +174,7 @@ export function patchFetch({
typeof curRevalidate !== 'undefined' typeof curRevalidate !== 'undefined'
) { ) {
console.warn( 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 _cache = undefined
} }
...@@ -219,7 +218,7 @@ export function patchFetch({ ...@@ -219,7 +218,7 @@ export function patchFetch({
if (isOnlyNoStore) { if (isOnlyNoStore) {
if (_cache === 'force-cache' || revalidate === 0) { if (_cache === 'force-cache' || revalidate === 0) {
throw new Error( 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 revalidate = 0
...@@ -228,7 +227,7 @@ export function patchFetch({ ...@@ -228,7 +227,7 @@ export function patchFetch({
if (isOnlyCache && _cache === 'no-store') { if (isOnlyCache && _cache === 'no-store') {
throw new Error( 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'`
) )
} }
...@@ -284,7 +283,7 @@ export function patchFetch({ ...@@ -284,7 +283,7 @@ export function patchFetch({
try { try {
cacheKey = cacheKey =
await staticGenerationStore.incrementalCache.fetchCacheKey( await staticGenerationStore.incrementalCache.fetchCacheKey(
isRequestInput ? (input as Request).url : input.toString(), fetchUrl,
isRequestInput ? (input as RequestInit) : init isRequestInput ? (input as RequestInit) : init
) )
} catch (err) { } catch (err) {
...@@ -329,7 +328,6 @@ export function patchFetch({ ...@@ -329,7 +328,6 @@ export function patchFetch({
} }
} }
const fetchUrl = url?.toString() ?? ''
const fetchIdx = staticGenerationStore.nextFetchId ?? 1 const fetchIdx = staticGenerationStore.nextFetchId ?? 1
staticGenerationStore.nextFetchId = fetchIdx + 1 staticGenerationStore.nextFetchId = fetchIdx + 1
......
...@@ -974,6 +974,12 @@ createNextDescribe( ...@@ -974,6 +974,12 @@ createNextDescribe(
return 'success' return 'success'
}, '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 () => { it('should cache correctly for cache: no-store', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment