mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
57baeec90d
This adds support for caching `fetch` responses in server components across HMR refresh requests. The two main benefits are faster responses for those requests, and reduced costs for billed API calls during local development. **Implementation notes:** - The feature is guarded by the new experimental flag `serverComponentsHmrCache`. - The server components HMR cache is intentionally independent from the incremental cache. - Fetched responses are written to the cache after every original fetch call, regardless of the cache settings (specifically including `no-store`). - Cached responses are read from the cache only for HMR refresh requests, potentially also short-cutting the incremental cache. - The HMR refresh requests are marked by the client with the newly introduced `Next-HMR-Refresh` header. - I shied away from further extending `renderOpts`. The alternative of adding another parameter to `renderToHTMLOrFlight` might not necessarily be better though. - This includes a refactoring to steer away from the "fast refresh" wording, since this is a separate (but related) [React feature](https://github.com/facebook/react/issues/16604#issuecomment-528663101) (also build on top of HMR). x-ref: #48481
8 lines
244 B
TypeScript
8 lines
244 B
TypeScript
export async function fetchRandomValue(key: string) {
|
|
const url = new URL('https://next-data-api-endpoint.vercel.app/api/random')
|
|
url.searchParams.set('key', key)
|
|
const res = await fetch(url, { cache: 'no-store' })
|
|
|
|
return res.text()
|
|
}
|