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

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

Merged
merged 5 commits into from
May 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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