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

Add url to patched fetch response #51665

Merged
merged 3 commits into from
Jun 23, 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
11 changes: 9 additions & 2 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ export function patchFetch({
body: bodyBuffer.toString('base64'),
status: res.status,
tags,
url: res.url,
},
revalidate: normalizedRevalidate,
},
Expand All @@ -390,10 +391,12 @@ export function patchFetch({
console.warn(`Failed to set fetch cache`, input, err)
}

return new Response(bodyBuffer, {
const response = new Response(bodyBuffer, {
headers: new Headers(res.headers),
status: res.status,
})
Object.defineProperty(response, 'url', { value: res.url })
return response
}
return res
})
Expand Down Expand Up @@ -467,10 +470,14 @@ export function patchFetch({
method: init?.method || 'GET',
})

return new Response(decodedBody, {
const response = new Response(decodedBody, {
headers: resData.headers,
status: resData.status,
})
Object.defineProperty(response, 'url', {
value: entry.value.data.url,
})
return response
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/response-cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface CachedFetchValue {
data: {
headers: { [k: string]: string }
body: string
url: string
status?: number
tags?: string[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function unstable_cache<T extends Callback>(
body: JSON.stringify(result),
status: 200,
tags,
url: '',
},
revalidate:
typeof options.revalidate !== 'number'
Expand Down
22 changes: 22 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 @@ -553,6 +553,7 @@ createNextDescribe(
'partial-gen-params/[lang]/[slug]/page.js',
'react-fetch-deduping-edge/page.js',
'react-fetch-deduping-node/page.js',
'response-url/page.js',
'route-handler-edge/revalidate-360/route.js',
'route-handler/post/route.js',
'route-handler/revalidate-360-isr/route.js',
Expand Down Expand Up @@ -1126,6 +1127,27 @@ createNextDescribe(
}
})

it('should produce response with url from fetch', async () => {
const res = await next.fetch('/response-url')
expect(res.status).toBe(200)

const html = await res.text()
const $ = cheerio.load(html)

expect($('#data-url-default-cache').text()).toBe(
'https://next-data-api-endpoint.vercel.app/api/random?a1'
)
expect($('#data-url-no-cache').text()).toBe(
'https://next-data-api-endpoint.vercel.app/api/random?b2'
)
expect($('#data-url-cached').text()).toBe(
'https://next-data-api-endpoint.vercel.app/api/random?a1'
)
expect($('#data-value-default-cache').text()).toBe(
$('#data-value-cached').text()
)
})

it('should properly error when dynamic = "error" page uses dynamic', async () => {
const res = await next.fetch('/dynamic-error/static-bailout-1')
const outputIndex = next.cliOutput.length
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/app-dir/app-static/app/response-url/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const fetchCache = 'default-cache'

export default async function Page() {
const [urlDefaultCache, valueDefaultCache] = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?a1'
).then((res) => Promise.all([res.url, res.text()]))

const urlNoCache = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?b2',
{ cache: 'no-store' }
).then((res) => res.url)

const [urlCached, valueCached] = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?a1'
).then((res) => Promise.all([res.url, res.text()]))

return (
<>
<p id="data-url-default-cache">{urlDefaultCache}</p>
<p id="data-value-default-cache">{valueDefaultCache}</p>
<p id="data-url-no-cache">{urlNoCache}</p>
<p id="data-url-cached">{urlCached}</p>
<p id="data-value-cached">{valueCached}</p>
</>
)
}