Skip to content

Commit db1ea37

Browse files
committed
Disable automatic fetch caching
1 parent 6f9c472 commit db1ea37

File tree

2 files changed

+4
-158
lines changed

2 files changed

+4
-158
lines changed

packages/next/src/server/app-render/dynamic-rendering.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,6 @@ export function Postpone({
172172
postponeWithTracking(prerenderState, reason, pathname)
173173
}
174174

175-
// @TODO refactor patch-fetch and this function to better model dynamic semantics. Currently this implementation
176-
// is too explicit about postponing if we are in a prerender and patch-fetch contains a lot of logic for determining
177-
// what makes the fetch "dynamic". It also doesn't handle Non PPR cases so it is isn't as consistent with the other
178-
// dynamic-rendering methods.
179-
export function trackDynamicFetch(
180-
store: StaticGenerationStore,
181-
expression: string
182-
) {
183-
// If we aren't in a prerender, or we're in an unstable cache callback, we
184-
// don't need to postpone.
185-
if (!store.prerenderState || store.isUnstableCacheCallback) return
186-
187-
postponeWithTracking(store.prerenderState, expression, store.urlPathname)
188-
}
189-
190175
function postponeWithTracking(
191176
prerenderState: PrerenderState,
192177
expression: string,

packages/next/src/server/lib/patch-fetch.ts

Lines changed: 4 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ import {
1313
NEXT_CACHE_TAG_MAX_LENGTH,
1414
} from '../../lib/constants'
1515
import * as Log from '../../build/output/log'
16-
import { trackDynamicFetch } from '../app-render/dynamic-rendering'
1716
import type { FetchMetric } from '../base-http'
1817

19-
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
20-
2118
type Fetcher = typeof fetch
2219

2320
type PatchedFetcher = Fetcher & {
@@ -215,10 +212,7 @@ interface PatchableModule {
215212

216213
function createPatchedFetcher(
217214
originFetch: Fetcher,
218-
{
219-
serverHooks: { DynamicServerError },
220-
staticGenerationAsyncStorage,
221-
}: PatchableModule
215+
{ staticGenerationAsyncStorage }: PatchableModule
222216
): PatchedFetcher {
223217
// Create the patched fetch function. We don't set the type here, as it's
224218
// verified as the return value of this function.
@@ -354,35 +348,11 @@ function createPatchedFetcher(
354348
curRevalidate = 0
355349
}
356350

357-
if (_cache === 'no-cache' || _cache === 'no-store') {
358-
cacheReason = `cache: ${_cache}`
359-
}
360-
361351
revalidate = validateRevalidate(
362352
curRevalidate,
363353
staticGenerationStore.urlPathname
364354
)
365355

366-
const _headers = getRequestMeta('headers')
367-
const initHeaders: Headers =
368-
typeof _headers?.get === 'function'
369-
? _headers
370-
: new Headers(_headers || {})
371-
372-
const hasUnCacheableHeader =
373-
initHeaders.get('authorization') || initHeaders.get('cookie')
374-
375-
const isUnCacheableMethod = !['get', 'head'].includes(
376-
getRequestMeta('method')?.toLowerCase() || 'get'
377-
)
378-
379-
// if there are authorized headers or a POST method and
380-
// dynamic data usage was present above the tree we bail
381-
// e.g. if cookies() is used before an authed/POST fetch
382-
const autoNoCache =
383-
(hasUnCacheableHeader || isUnCacheableMethod) &&
384-
staticGenerationStore.revalidate === 0
385-
386356
switch (fetchCacheMode) {
387357
case 'force-no-store': {
388358
cacheReason = 'fetchCache = force-no-store'
@@ -424,55 +394,20 @@ function createPatchedFetcher(
424394
}
425395

426396
if (typeof revalidate === 'undefined') {
427-
if (fetchCacheMode === 'default-cache') {
428-
revalidate = false
429-
cacheReason = 'fetchCache = default-cache'
430-
} else if (autoNoCache) {
431-
revalidate = 0
432-
cacheReason = 'auto no cache'
433-
} else if (fetchCacheMode === 'default-no-store') {
397+
if (fetchCacheMode === 'default-no-store') {
434398
revalidate = 0
435399
cacheReason = 'fetchCache = default-no-store'
436400
} else if (isUsingNoStore) {
437401
revalidate = 0
438402
cacheReason = 'noStore call'
439403
} else {
440-
cacheReason = 'auto cache'
441-
revalidate =
442-
typeof staticGenerationStore.revalidate === 'boolean' ||
443-
typeof staticGenerationStore.revalidate === 'undefined'
444-
? false
445-
: staticGenerationStore.revalidate
404+
revalidate = 0
405+
cacheReason = 'default no cache'
446406
}
447407
} else if (!cacheReason) {
448408
cacheReason = `revalidate: ${revalidate}`
449409
}
450410

451-
if (
452-
// when force static is configured we don't bail from
453-
// `revalidate: 0` values
454-
!(staticGenerationStore.forceStatic && revalidate === 0) &&
455-
// we don't consider autoNoCache to switch to dynamic during
456-
// revalidate although if it occurs during build we do
457-
!autoNoCache &&
458-
// If the revalidate value isn't currently set or the value is less
459-
// than the current revalidate value, we should update the revalidate
460-
// value.
461-
(typeof staticGenerationStore.revalidate === 'undefined' ||
462-
(typeof revalidate === 'number' &&
463-
(staticGenerationStore.revalidate === false ||
464-
(typeof staticGenerationStore.revalidate === 'number' &&
465-
revalidate < staticGenerationStore.revalidate))))
466-
) {
467-
// If we were setting the revalidate value to 0, we should try to
468-
// postpone instead first.
469-
if (revalidate === 0) {
470-
trackDynamicFetch(staticGenerationStore, 'revalidate: 0')
471-
}
472-
473-
staticGenerationStore.revalidate = revalidate
474-
}
475-
476411
const isCacheableRevalidate =
477412
(typeof revalidate === 'number' && revalidate > 0) ||
478413
revalidate === false
@@ -673,80 +608,6 @@ function createPatchedFetcher(
673608
}
674609
}
675610

676-
if (
677-
staticGenerationStore.isStaticGeneration &&
678-
init &&
679-
typeof init === 'object'
680-
) {
681-
const { cache } = init
682-
683-
// Delete `cache` property as Cloudflare Workers will throw an error
684-
if (isEdgeRuntime) delete init.cache
685-
686-
if (!staticGenerationStore.forceStatic && cache === 'no-store') {
687-
const dynamicUsageReason = `no-store fetch ${input}${
688-
staticGenerationStore.urlPathname
689-
? ` ${staticGenerationStore.urlPathname}`
690-
: ''
691-
}`
692-
693-
// If enabled, we should bail out of static generation.
694-
trackDynamicFetch(staticGenerationStore, dynamicUsageReason)
695-
696-
// If partial prerendering is not enabled, then we should throw an
697-
// error to indicate that this fetch is dynamic.
698-
if (!staticGenerationStore.prerenderState) {
699-
// PPR is not enabled, or React postpone is not available, we
700-
// should set the revalidate to 0.
701-
staticGenerationStore.revalidate = 0
702-
703-
const err = new DynamicServerError(dynamicUsageReason)
704-
staticGenerationStore.dynamicUsageErr = err
705-
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
706-
throw err
707-
}
708-
}
709-
710-
const hasNextConfig = 'next' in init
711-
const { next = {} } = init
712-
if (
713-
typeof next.revalidate === 'number' &&
714-
(typeof staticGenerationStore.revalidate === 'undefined' ||
715-
(typeof staticGenerationStore.revalidate === 'number' &&
716-
next.revalidate < staticGenerationStore.revalidate))
717-
) {
718-
if (
719-
!staticGenerationStore.forceDynamic &&
720-
!staticGenerationStore.forceStatic &&
721-
next.revalidate === 0
722-
) {
723-
const dynamicUsageReason = `revalidate: 0 fetch ${input}${
724-
staticGenerationStore.urlPathname
725-
? ` ${staticGenerationStore.urlPathname}`
726-
: ''
727-
}`
728-
729-
// If enabled, we should bail out of static generation.
730-
trackDynamicFetch(staticGenerationStore, dynamicUsageReason)
731-
732-
// If partial prerendering is not enabled, then we should throw an
733-
// error to indicate that this fetch is dynamic.
734-
if (!staticGenerationStore.prerenderState) {
735-
const err = new DynamicServerError(dynamicUsageReason)
736-
staticGenerationStore.dynamicUsageErr = err
737-
staticGenerationStore.dynamicUsageDescription =
738-
dynamicUsageReason
739-
throw err
740-
}
741-
}
742-
743-
if (!staticGenerationStore.forceStatic || next.revalidate !== 0) {
744-
staticGenerationStore.revalidate = next.revalidate
745-
}
746-
}
747-
if (hasNextConfig) delete init.next
748-
}
749-
750611
// if we are revalidating the whole page via time or on-demand and
751612
// the fetch cache entry is stale we should still de-dupe the
752613
// origin hit if it's a cache-able entry

0 commit comments

Comments
 (0)