Skip to content

Commit 783acc5

Browse files
authored
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. ```
1 parent 0a81cc0 commit 783acc5

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,17 @@ export function patchFetch({
9191
// Error caused by malformed URL should be handled by native fetch
9292
url = undefined
9393
}
94+
const fetchUrl = url?.href ?? ''
9495
const fetchStart = Date.now()
9596
const method = init?.method?.toUpperCase() || 'GET'
9697

9798
return await getTracer().trace(
9899
AppRenderSpan.fetch,
99100
{
100101
kind: SpanKind.CLIENT,
101-
spanName: ['fetch', method, url?.toString() ?? input.toString()]
102-
.filter(Boolean)
103-
.join(' '),
102+
spanName: ['fetch', method, fetchUrl].filter(Boolean).join(' '),
104103
attributes: {
105-
'http.url': url?.toString(),
104+
'http.url': fetchUrl,
106105
'http.method': method,
107106
'net.peer.name': url?.hostname,
108107
'net.peer.port': url?.port || undefined,
@@ -175,7 +174,7 @@ export function patchFetch({
175174
typeof curRevalidate !== 'undefined'
176175
) {
177176
console.warn(
178-
`Warning: fetch for ${input.toString()} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
177+
`Warning: fetch for ${fetchUrl} on ${staticGenerationStore.pathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
179178
)
180179
_cache = undefined
181180
}
@@ -219,7 +218,7 @@ export function patchFetch({
219218
if (isOnlyNoStore) {
220219
if (_cache === 'force-cache' || revalidate === 0) {
221220
throw new Error(
222-
`cache: 'force-cache' used on fetch for ${input.toString()} with 'export const fetchCache = 'only-no-store'`
221+
`cache: 'force-cache' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-no-store'`
223222
)
224223
}
225224
revalidate = 0
@@ -228,7 +227,7 @@ export function patchFetch({
228227

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

@@ -284,7 +283,7 @@ export function patchFetch({
284283
try {
285284
cacheKey =
286285
await staticGenerationStore.incrementalCache.fetchCacheKey(
287-
isRequestInput ? (input as Request).url : input.toString(),
286+
fetchUrl,
288287
isRequestInput ? (input as RequestInit) : init
289288
)
290289
} catch (err) {
@@ -329,7 +328,6 @@ export function patchFetch({
329328
}
330329
}
331330

332-
const fetchUrl = url?.toString() ?? ''
333331
const fetchIdx = staticGenerationStore.nextFetchId ?? 1
334332
staticGenerationStore.nextFetchId = fetchIdx + 1
335333

test/e2e/app-dir/app-static/app-static.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,12 @@ createNextDescribe(
974974

975975
return 'success'
976976
}, 'success')
977+
978+
if (!isNextDeploy) {
979+
expect(next.cliOutput).toContain(
980+
'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.'
981+
)
982+
}
977983
})
978984

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

0 commit comments

Comments
 (0)