Unverified Commit fda1ecc3 authored by Adam Mcgrath's avatar Adam Mcgrath Committed by GitHub

Add url to patched fetch response (#51665)

### What?

Add `url` property to response returned from patched fetch

### Why?

So that libraries that rely on `response.url` work with the app directory and fetch cacheing

### How?

Using the same code as https://github.com/vercel/edge-runtime/blob/edge-runtime%402.4.3/packages/primitives/src/primitives/fetch.js#L164

fixes #51642 
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent c3c6214c
...@@ -378,6 +378,7 @@ export function patchFetch({ ...@@ -378,6 +378,7 @@ export function patchFetch({
body: bodyBuffer.toString('base64'), body: bodyBuffer.toString('base64'),
status: res.status, status: res.status,
tags, tags,
url: res.url,
}, },
revalidate: normalizedRevalidate, revalidate: normalizedRevalidate,
}, },
...@@ -390,10 +391,12 @@ export function patchFetch({ ...@@ -390,10 +391,12 @@ export function patchFetch({
console.warn(`Failed to set fetch cache`, input, err) console.warn(`Failed to set fetch cache`, input, err)
} }
return new Response(bodyBuffer, { const response = new Response(bodyBuffer, {
headers: new Headers(res.headers), headers: new Headers(res.headers),
status: res.status, status: res.status,
}) })
Object.defineProperty(response, 'url', { value: res.url })
return response
} }
return res return res
}) })
...@@ -467,10 +470,14 @@ export function patchFetch({ ...@@ -467,10 +470,14 @@ export function patchFetch({
method: init?.method || 'GET', method: init?.method || 'GET',
}) })
return new Response(decodedBody, { const response = new Response(decodedBody, {
headers: resData.headers, headers: resData.headers,
status: resData.status, status: resData.status,
}) })
Object.defineProperty(response, 'url', {
value: entry.value.data.url,
})
return response
} }
} }
} }
......
...@@ -18,6 +18,7 @@ export interface CachedFetchValue { ...@@ -18,6 +18,7 @@ export interface CachedFetchValue {
data: { data: {
headers: { [k: string]: string } headers: { [k: string]: string }
body: string body: string
url: string
status?: number status?: number
tags?: string[] tags?: string[]
} }
......
...@@ -86,6 +86,7 @@ export function unstable_cache<T extends Callback>( ...@@ -86,6 +86,7 @@ export function unstable_cache<T extends Callback>(
body: JSON.stringify(result), body: JSON.stringify(result),
status: 200, status: 200,
tags, tags,
url: '',
}, },
revalidate: revalidate:
typeof options.revalidate !== 'number' typeof options.revalidate !== 'number'
......
...@@ -553,6 +553,7 @@ createNextDescribe( ...@@ -553,6 +553,7 @@ createNextDescribe(
'partial-gen-params/[lang]/[slug]/page.js', 'partial-gen-params/[lang]/[slug]/page.js',
'react-fetch-deduping-edge/page.js', 'react-fetch-deduping-edge/page.js',
'react-fetch-deduping-node/page.js', 'react-fetch-deduping-node/page.js',
'response-url/page.js',
'route-handler-edge/revalidate-360/route.js', 'route-handler-edge/revalidate-360/route.js',
'route-handler/post/route.js', 'route-handler/post/route.js',
'route-handler/revalidate-360-isr/route.js', 'route-handler/revalidate-360-isr/route.js',
...@@ -1126,6 +1127,27 @@ createNextDescribe( ...@@ -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 () => { it('should properly error when dynamic = "error" page uses dynamic', async () => {
const res = await next.fetch('/dynamic-error/static-bailout-1') const res = await next.fetch('/dynamic-error/static-bailout-1')
const outputIndex = next.cliOutput.length const outputIndex = next.cliOutput.length
......
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>
</>
)
}
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