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

Update fetch cache internal handling #46522

Merged
merged 2 commits into from
Feb 28, 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
2 changes: 1 addition & 1 deletion .github/workflows/build_test_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ jobs:
testDevLTS,
testProdLTS,
testDevE2ELTS,
testprode2elts,
testProdE2ELTS,
]
steps:
- run: exit 0
Expand Down
11 changes: 6 additions & 5 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export function patchFetch({
serverHooks: typeof import('../../client/components/hooks-server-context')
staticGenerationAsyncStorage: StaticGenerationAsyncStorage
}) {
if ((globalThis.fetch as any).patched) return
if ((fetch as any).__nextPatched) return

const { DynamicServerError } = serverHooks

const originFetch = fetch

// @ts-expect-error - we're patching fetch
Expand Down Expand Up @@ -159,7 +158,8 @@ export function patchFetch({
revalidate > 0
) {
let base64Body = ''
const arrayBuffer = await res.arrayBuffer()
const resBlob = await res.blob()
const arrayBuffer = await resBlob.arrayBuffer()

if (process.env.NEXT_RUNTIME === 'edge') {
const { encode } =
Expand All @@ -186,7 +186,8 @@ export function patchFetch({
} catch (err) {
console.warn(`Failed to set fetch cache`, input, err)
}
return new Response(arrayBuffer, {

return new Response(resBlob, {
headers: res.headers,
status: res.status,
})
Expand Down Expand Up @@ -291,5 +292,5 @@ export function patchFetch({
return doOriginalFetch()
}
)
;(globalThis.fetch as any).patched = true
;(fetch as any).__nextPatched = true
}
1 change: 0 additions & 1 deletion test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ createNextDescribe(
expect($2('#data-body1').text()).toBe(dataBody1)
expect($2('#data-body2').text()).toBe(dataBody2)
expect($2('#data-body3').text()).toBe(dataBody3)
expect($2('#data-body4').text()).toBe(dataBody4)
return 'success'
}, 'success')
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { FormData, Blob } from 'next/dist/compiled/@edge-runtime/primitives'

const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
Expand Down Expand Up @@ -52,23 +50,7 @@ export default async function Page() {
}
).then((res) => res.text())

const formData = new FormData()
formData.append('hello', 'value')
formData.append('another', new Blob(['some text'], { type: 'text/plain' }))
formData.append('another', 'text')

const dataWithBody3 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: formData,
next: {
revalidate: 10,
},
}
).then((res) => res.text())

const dataWithBody4 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
Expand All @@ -79,7 +61,7 @@ export default async function Page() {
}
).then((res) => res.text())

const dataWithBody5 = await fetchRetry(
const dataWithBody4 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
Expand All @@ -93,9 +75,8 @@ export default async function Page() {
<p id="page-data">{data}</p>
<p id="data-body1">{dataWithBody1}</p>
<p id="data-body2">{dataWithBody2}</p>
<p id="data-body3">{dataWithBody3}</p>
<p id="data-body4">{dataWithBody3}</p>
ijjk marked this conversation as resolved.
Show resolved Hide resolved
<p id="data-body4">{dataWithBody4}</p>
<p id="data-body5">{dataWithBody5}</p>
</>
)
}