Unverified Commit 4d37f665 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

ensure fetch is patched before preloading entries (#72363)

If a user creates a custom fetch implementation in module scope, the
logic to preload entries during `next start` will result in fetch not
being patched before the userland implementation. As a result, it won't
opt into any caching heuristics from our patched fetch.

Fixes #71881 (in addition to the other PR in this stack)
parent 1c22d142
...@@ -277,6 +277,11 @@ export default class NextNodeServer extends BaseServer< ...@@ -277,6 +277,11 @@ export default class NextNodeServer extends BaseServer<
for (const page of Object.keys(appPathsManifest || {})) { for (const page of Object.keys(appPathsManifest || {})) {
await loadComponents({ distDir: this.distDir, page, isAppPath: true }) await loadComponents({ distDir: this.distDir, page, isAppPath: true })
.then(async ({ ComponentMod }) => { .then(async ({ ComponentMod }) => {
// we need to ensure fetch is patched before we require the page,
// otherwise if the fetch is patched by user code, we will be patching it
// too late and there won't be any caching behaviors
ComponentMod.patchFetch()
const webpackRequire = ComponentMod.__next_app__.require const webpackRequire = ComponentMod.__next_app__.require
if (webpackRequire?.m) { if (webpackRequire?.m) {
for (const id of Object.keys(webpackRequire.m)) { for (const id of Object.keys(webpackRequire.m)) {
......
import { waitFor, retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'
describe('app-fetch-deduping', () => {
const { next } = nextTestSetup({ files: __dirname })
it('should still properly cache fetches when the user has a custom fetch implementation', async () => {
const browser = await next.browser('/')
let currentValue: string | undefined
await retry(async () => {
const initialRandom = await browser.elementById('random').text()
expect(initialRandom).toMatch(/^0\.\d+$/)
await browser.refresh()
currentValue = await browser.elementById('random').text()
expect(currentValue).toBe(initialRandom)
})
// wait for the revalidation period
await waitFor(3000)
await retry(async () => {
await browser.refresh()
const finalValue = await browser.elementById('random').text()
expect(finalValue).not.toBe(currentValue)
})
})
})
export default function Layout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { cookies } from 'next/headers'
import { Suspense } from 'react'
const patchFetch = (originalFetch) => async (url, options) => {
const res = await originalFetch(url, options)
return res
}
const customFetch = patchFetch(fetch)
export default async function Page() {
const data = await customFetch(
'https://next-data-api-endpoint.vercel.app/api/random?revalidate-3',
{
next: {
revalidate: 3,
},
}
).then((res) => res.text())
return (
<>
<div id="random">{data}</div>
<Suspense fallback={<div>Loading...</div>}>
<DynamicThing />
</Suspense>
</>
)
}
async function DynamicThing() {
const cookieStore = await cookies()
const cookie = cookieStore.get('name')
return <div>{cookie}</div>
}
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