File size: 42,395 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 |
import type {
WorkAsyncStorage,
WorkStore,
} from '../app-render/work-async-storage.external'
import { AppRenderSpan, NextNodeServerSpan } from './trace/constants'
import { getTracer, SpanKind } from './trace/tracer'
import {
CACHE_ONE_YEAR,
INFINITE_CACHE,
NEXT_CACHE_TAG_MAX_ITEMS,
NEXT_CACHE_TAG_MAX_LENGTH,
} from '../../lib/constants'
import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering'
import { makeHangingPromise } from '../dynamic-rendering-utils'
import type { FetchMetric } from '../base-http'
import { createDedupeFetch } from './dedupe-fetch'
import {
getCacheSignal,
type RevalidateStore,
type WorkUnitAsyncStorage,
} from '../app-render/work-unit-async-storage.external'
import {
CachedRouteKind,
IncrementalCacheKind,
type CachedFetchData,
type ServerComponentsHmrCache,
type SetIncrementalFetchCacheContext,
} from '../response-cache'
import { waitAtLeastOneReactRenderTask } from '../../lib/scheduler'
import { cloneResponse } from './clone-response'
import type { IncrementalCache } from './incremental-cache'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
type Fetcher = typeof fetch
type PatchedFetcher = Fetcher & {
readonly __nextPatched: true
readonly __nextGetStaticStore: () => WorkAsyncStorage
readonly _nextOriginalFetch: Fetcher
}
export const NEXT_PATCH_SYMBOL = Symbol.for('next-patch')
function isFetchPatched() {
return (globalThis as Record<symbol, unknown>)[NEXT_PATCH_SYMBOL] === true
}
export function validateRevalidate(
revalidateVal: unknown,
route: string
): undefined | number {
try {
let normalizedRevalidate: number | undefined = undefined
if (revalidateVal === false) {
normalizedRevalidate = INFINITE_CACHE
} else if (
typeof revalidateVal === 'number' &&
!isNaN(revalidateVal) &&
revalidateVal > -1
) {
normalizedRevalidate = revalidateVal
} else if (typeof revalidateVal !== 'undefined') {
throw new Error(
`Invalid revalidate value "${revalidateVal}" on "${route}", must be a non-negative number or false`
)
}
return normalizedRevalidate
} catch (err: any) {
// handle client component error from attempting to check revalidate value
if (err instanceof Error && err.message.includes('Invalid revalidate')) {
throw err
}
return undefined
}
}
export function validateTags(tags: any[], description: string) {
const validTags: string[] = []
const invalidTags: Array<{
tag: any
reason: string
}> = []
for (let i = 0; i < tags.length; i++) {
const tag = tags[i]
if (typeof tag !== 'string') {
invalidTags.push({ tag, reason: 'invalid type, must be a string' })
} else if (tag.length > NEXT_CACHE_TAG_MAX_LENGTH) {
invalidTags.push({
tag,
reason: `exceeded max length of ${NEXT_CACHE_TAG_MAX_LENGTH}`,
})
} else {
validTags.push(tag)
}
if (validTags.length > NEXT_CACHE_TAG_MAX_ITEMS) {
console.warn(
`Warning: exceeded max tag count for ${description}, dropped tags:`,
tags.slice(i).join(', ')
)
break
}
}
if (invalidTags.length > 0) {
console.warn(`Warning: invalid tags passed to ${description}: `)
for (const { tag, reason } of invalidTags) {
console.log(`tag: "${tag}" ${reason}`)
}
}
return validTags
}
function trackFetchMetric(
workStore: WorkStore,
ctx: Omit<FetchMetric, 'end' | 'idx'>
) {
// If the static generation store is not available, we can't track the fetch
if (!workStore) return
if (workStore.requestEndedState?.ended) return
const isDebugBuild =
(!!process.env.NEXT_DEBUG_BUILD ||
process.env.NEXT_SSG_FETCH_METRICS === '1') &&
workStore.isStaticGeneration
const isDevelopment = process.env.NODE_ENV === 'development'
if (
// The only time we want to track fetch metrics outside of development is when
// we are performing a static generation & we are in debug mode.
!isDebugBuild &&
!isDevelopment
) {
return
}
workStore.fetchMetrics ??= []
workStore.fetchMetrics.push({
...ctx,
end: performance.timeOrigin + performance.now(),
idx: workStore.nextFetchId || 0,
})
}
async function createCachedPrerenderResponse(
res: Response,
cacheKey: string,
incrementalCacheContext: SetIncrementalFetchCacheContext | undefined,
incrementalCache: IncrementalCache,
revalidate: number,
handleUnlock: () => Promise<void> | void
): Promise<Response> {
// We are prerendering at build time or revalidate time with cacheComponents so we
// need to buffer the response so we can guarantee it can be read in a
// microtask.
const bodyBuffer = await res.arrayBuffer()
const fetchedData = {
headers: Object.fromEntries(res.headers.entries()),
body: Buffer.from(bodyBuffer).toString('base64'),
status: res.status,
url: res.url,
}
// We can skip setting the serverComponentsHmrCache because we aren't in dev
// mode.
if (incrementalCacheContext) {
await incrementalCache.set(
cacheKey,
{ kind: CachedRouteKind.FETCH, data: fetchedData, revalidate },
incrementalCacheContext
)
}
await handleUnlock()
// We return a new Response to the caller.
return new Response(bodyBuffer, {
headers: res.headers,
status: res.status,
statusText: res.statusText,
})
}
async function createCachedDynamicResponse(
workStore: WorkStore,
res: Response,
cacheKey: string,
incrementalCacheContext: SetIncrementalFetchCacheContext | undefined,
incrementalCache: IncrementalCache,
serverComponentsHmrCache: ServerComponentsHmrCache | undefined,
revalidate: number,
input: RequestInfo | URL,
handleUnlock: () => Promise<void> | void
): Promise<Response> {
// We're cloning the response using this utility because there exists a bug in
// the undici library around response cloning. See the following pull request
// for more details: https://github.com/vercel/next.js/pull/73274
const [cloned1, cloned2] = cloneResponse(res)
// We are dynamically rendering including dev mode. We want to return the
// response to the caller as soon as possible because it might stream over a
// very long time.
const cacheSetPromise = cloned1
.arrayBuffer()
.then(async (arrayBuffer) => {
const bodyBuffer = Buffer.from(arrayBuffer)
const fetchedData = {
headers: Object.fromEntries(cloned1.headers.entries()),
body: bodyBuffer.toString('base64'),
status: cloned1.status,
url: cloned1.url,
}
serverComponentsHmrCache?.set(cacheKey, fetchedData)
if (incrementalCacheContext) {
await incrementalCache.set(
cacheKey,
{ kind: CachedRouteKind.FETCH, data: fetchedData, revalidate },
incrementalCacheContext
)
}
})
.catch((error) => console.warn(`Failed to set fetch cache`, input, error))
.finally(handleUnlock)
const pendingRevalidateKey = `cache-set-${cacheKey}`
workStore.pendingRevalidates ??= {}
if (pendingRevalidateKey in workStore.pendingRevalidates) {
// there is already a pending revalidate entry that we need to await to
// avoid race conditions
await workStore.pendingRevalidates[pendingRevalidateKey]
}
workStore.pendingRevalidates[pendingRevalidateKey] = cacheSetPromise.finally(
() => {
// If the pending revalidate is not present in the store, then we have
// nothing to delete.
if (!workStore.pendingRevalidates?.[pendingRevalidateKey]) {
return
}
delete workStore.pendingRevalidates[pendingRevalidateKey]
}
)
return cloned2
}
interface PatchableModule {
workAsyncStorage: WorkAsyncStorage
workUnitAsyncStorage: WorkUnitAsyncStorage
}
export function createPatchedFetcher(
originFetch: Fetcher,
{ workAsyncStorage, workUnitAsyncStorage }: PatchableModule
): PatchedFetcher {
// Create the patched fetch function.
const patched = async function fetch(
input: RequestInfo | URL,
init: RequestInit | undefined
): Promise<Response> {
let url: URL | undefined
try {
url = new URL(input instanceof Request ? input.url : input)
url.username = ''
url.password = ''
} catch {
// Error caused by malformed URL should be handled by native fetch
url = undefined
}
const fetchUrl = url?.href ?? ''
const method = init?.method?.toUpperCase() || 'GET'
// Do create a new span trace for internal fetches in the
// non-verbose mode.
const isInternal = (init?.next as any)?.internal === true
const hideSpan = process.env.NEXT_OTEL_FETCH_DISABLED === '1'
// We don't track fetch metrics for internal fetches
// so it's not critical that we have a start time, as it won't be recorded.
// This is to workaround a flaky issue where performance APIs might
// not be available and will require follow-up investigation.
const fetchStart: number | undefined = isInternal
? undefined
: performance.timeOrigin + performance.now()
const workStore = workAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorage.getStore()
// During static generation we track cache reads so we can reason about when they fill
let cacheSignal = workUnitStore ? getCacheSignal(workUnitStore) : null
if (cacheSignal) {
cacheSignal.beginRead()
}
const result = getTracer().trace(
isInternal ? NextNodeServerSpan.internalFetch : AppRenderSpan.fetch,
{
hideSpan,
kind: SpanKind.CLIENT,
spanName: ['fetch', method, fetchUrl].filter(Boolean).join(' '),
attributes: {
'http.url': fetchUrl,
'http.method': method,
'net.peer.name': url?.hostname,
'net.peer.port': url?.port || undefined,
},
},
async () => {
// If this is an internal fetch, we should not do any special treatment.
if (isInternal) {
return originFetch(input, init)
}
// If the workStore is not available, we can't do any
// special treatment of fetch, therefore fallback to the original
// fetch implementation.
if (!workStore) {
return originFetch(input, init)
}
// We should also fallback to the original fetch implementation if we
// are in draft mode, it does not constitute a static generation.
if (workStore.isDraftMode) {
return originFetch(input, init)
}
const isRequestInput =
input &&
typeof input === 'object' &&
typeof (input as Request).method === 'string'
const getRequestMeta = (field: string) => {
// If request input is present but init is not, retrieve from input first.
const value = (init as any)?.[field]
return value || (isRequestInput ? (input as any)[field] : null)
}
let finalRevalidate: number | undefined = undefined
const getNextField = (field: 'revalidate' | 'tags') => {
return typeof init?.next?.[field] !== 'undefined'
? init?.next?.[field]
: isRequestInput
? (input as any).next?.[field]
: undefined
}
// RequestInit doesn't keep extra fields e.g. next so it's
// only available if init is used separate
const originalFetchRevalidate = getNextField('revalidate')
let currentFetchRevalidate = originalFetchRevalidate
const tags: string[] = validateTags(
getNextField('tags') || [],
`fetch ${input.toString()}`
)
let revalidateStore: RevalidateStore | undefined
if (workUnitStore) {
switch (workUnitStore.type) {
case 'prerender':
// TODO: Stop accumulating tags in client prerender. (fallthrough)
case 'prerender-client':
case 'prerender-ppr':
case 'prerender-legacy':
case 'cache':
case 'private-cache':
revalidateStore = workUnitStore
break
case 'request':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
if (revalidateStore) {
if (Array.isArray(tags)) {
// Collect tags onto parent caches or parent prerenders.
const collectedTags =
revalidateStore.tags ?? (revalidateStore.tags = [])
for (const tag of tags) {
if (!collectedTags.includes(tag)) {
collectedTags.push(tag)
}
}
}
}
const implicitTags = workUnitStore?.implicitTags
let pageFetchCacheMode = workStore.fetchCache
if (workUnitStore) {
switch (workUnitStore.type) {
case 'unstable-cache':
// Inside unstable-cache we treat it the same as force-no-store on
// the page.
pageFetchCacheMode = 'force-no-store'
break
case 'prerender':
case 'prerender-client':
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
break
default:
workUnitStore satisfies never
}
}
const isUsingNoStore = !!workStore.isUnstableNoStore
let currentFetchCacheConfig = getRequestMeta('cache')
let cacheReason = ''
let cacheWarning: string | undefined
if (
typeof currentFetchCacheConfig === 'string' &&
typeof currentFetchRevalidate !== 'undefined'
) {
// If the revalidate value conflicts with the cache value, we should warn the user and unset the conflicting values.
const isConflictingRevalidate =
// revalidate: 0 and cache: force-cache
(currentFetchCacheConfig === 'force-cache' &&
currentFetchRevalidate === 0) ||
// revalidate: >0 or revalidate: false and cache: no-store
(currentFetchCacheConfig === 'no-store' &&
(currentFetchRevalidate > 0 || currentFetchRevalidate === false))
if (isConflictingRevalidate) {
cacheWarning = `Specified "cache: ${currentFetchCacheConfig}" and "revalidate: ${currentFetchRevalidate}", only one should be specified.`
currentFetchCacheConfig = undefined
currentFetchRevalidate = undefined
}
}
const hasExplicitFetchCacheOptOut =
// fetch config itself signals not to cache
currentFetchCacheConfig === 'no-cache' ||
currentFetchCacheConfig === 'no-store' ||
// the fetch isn't explicitly caching and the segment level cache config signals not to cache
// note: `pageFetchCacheMode` is also set by being in an unstable_cache context.
pageFetchCacheMode === 'force-no-store' ||
pageFetchCacheMode === 'only-no-store'
// If no explicit fetch cache mode is set, but dynamic = `force-dynamic` is set,
// we shouldn't consider caching the fetch. This is because the `dynamic` cache
// is considered a "top-level" cache mode, whereas something like `fetchCache` is more
// fine-grained. Top-level modes are responsible for setting reasonable defaults for the
// other configurations.
const noFetchConfigAndForceDynamic =
!pageFetchCacheMode &&
!currentFetchCacheConfig &&
!currentFetchRevalidate &&
workStore.forceDynamic
if (
// force-cache was specified without a revalidate value. We set the revalidate value to false
// which will signal the cache to not revalidate
currentFetchCacheConfig === 'force-cache' &&
typeof currentFetchRevalidate === 'undefined'
) {
currentFetchRevalidate = false
} else if (
hasExplicitFetchCacheOptOut ||
noFetchConfigAndForceDynamic
) {
currentFetchRevalidate = 0
}
if (
currentFetchCacheConfig === 'no-cache' ||
currentFetchCacheConfig === 'no-store'
) {
cacheReason = `cache: ${currentFetchCacheConfig}`
}
finalRevalidate = validateRevalidate(
currentFetchRevalidate,
workStore.route
)
const _headers = getRequestMeta('headers')
const initHeaders: Headers =
typeof _headers?.get === 'function'
? _headers
: new Headers(_headers || {})
const hasUnCacheableHeader =
initHeaders.get('authorization') || initHeaders.get('cookie')
const isUnCacheableMethod = !['get', 'head'].includes(
getRequestMeta('method')?.toLowerCase() || 'get'
)
/**
* We automatically disable fetch caching under the following conditions:
* - Fetch cache configs are not set. Specifically:
* - A page fetch cache mode is not set (export const fetchCache=...)
* - A fetch cache mode is not set in the fetch call (fetch(url, { cache: ... }))
* or the fetch cache mode is set to 'default'
* - A fetch revalidate value is not set in the fetch call (fetch(url, { revalidate: ... }))
* - OR the fetch comes after a configuration that triggered dynamic rendering (e.g., reading cookies())
* and the fetch was considered uncacheable (e.g., POST method or has authorization headers)
*/
const hasNoExplicitCacheConfig =
// eslint-disable-next-line eqeqeq
pageFetchCacheMode == undefined &&
// eslint-disable-next-line eqeqeq
(currentFetchCacheConfig == undefined ||
// when considering whether to opt into the default "no-cache" fetch semantics,
// a "default" cache config should be treated the same as no cache config
currentFetchCacheConfig === 'default') &&
// eslint-disable-next-line eqeqeq
currentFetchRevalidate == undefined
let autoNoCache = Boolean(
(hasUnCacheableHeader || isUnCacheableMethod) &&
revalidateStore?.revalidate === 0
)
let isImplicitBuildTimeCache = false
if (!autoNoCache && hasNoExplicitCacheConfig) {
// We don't enable automatic no-cache behavior during build-time
// prerendering so that we can still leverage the fetch cache between
// export workers.
if (workStore.isBuildTimePrerendering) {
isImplicitBuildTimeCache = true
} else {
autoNoCache = true
}
}
// If we have no cache config, and we're in Dynamic I/O prerendering,
// it'll be a dynamic call. We don't have to issue that dynamic call.
if (hasNoExplicitCacheConfig && workUnitStore !== undefined) {
switch (workUnitStore.type) {
case 'prerender':
// While we don't want to do caching in the client scope we know the
// fetch will be dynamic for cacheComponents so we may as well avoid the
// call here. (fallthrough)
case 'prerender-client':
if (cacheSignal) {
cacheSignal.endRead()
cacheSignal = null
}
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
switch (pageFetchCacheMode) {
case 'force-no-store': {
cacheReason = 'fetchCache = force-no-store'
break
}
case 'only-no-store': {
if (
currentFetchCacheConfig === 'force-cache' ||
(typeof finalRevalidate !== 'undefined' && finalRevalidate > 0)
) {
throw new Error(
`cache: 'force-cache' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-no-store'`
)
}
cacheReason = 'fetchCache = only-no-store'
break
}
case 'only-cache': {
if (currentFetchCacheConfig === 'no-store') {
throw new Error(
`cache: 'no-store' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-cache'`
)
}
break
}
case 'force-cache': {
if (
typeof currentFetchRevalidate === 'undefined' ||
currentFetchRevalidate === 0
) {
cacheReason = 'fetchCache = force-cache'
finalRevalidate = INFINITE_CACHE
}
break
}
case 'default-cache':
case 'default-no-store':
case 'auto':
case undefined:
// sometimes we won't match the above cases. the reason we don't move
// everything to this switch is the use of autoNoCache which is not a fetchCacheMode
// I suspect this could be unified with fetchCacheMode however in which case we could
// simplify the switch case and ensure we have an exhaustive switch handling all modes
break
default:
pageFetchCacheMode satisfies never
}
if (typeof finalRevalidate === 'undefined') {
if (pageFetchCacheMode === 'default-cache' && !isUsingNoStore) {
finalRevalidate = INFINITE_CACHE
cacheReason = 'fetchCache = default-cache'
} else if (pageFetchCacheMode === 'default-no-store') {
finalRevalidate = 0
cacheReason = 'fetchCache = default-no-store'
} else if (isUsingNoStore) {
finalRevalidate = 0
cacheReason = 'noStore call'
} else if (autoNoCache) {
finalRevalidate = 0
cacheReason = 'auto no cache'
} else {
// TODO: should we consider this case an invariant?
cacheReason = 'auto cache'
finalRevalidate = revalidateStore
? revalidateStore.revalidate
: INFINITE_CACHE
}
} else if (!cacheReason) {
cacheReason = `revalidate: ${finalRevalidate}`
}
if (
// when force static is configured we don't bail from
// `revalidate: 0` values
!(workStore.forceStatic && finalRevalidate === 0) &&
// we don't consider autoNoCache to switch to dynamic for ISR
!autoNoCache &&
// If the revalidate value isn't currently set or the value is less
// than the current revalidate value, we should update the revalidate
// value.
revalidateStore &&
finalRevalidate < revalidateStore.revalidate
) {
// If we were setting the revalidate value to 0, we should try to
// postpone instead first.
if (finalRevalidate === 0) {
if (workUnitStore) {
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-client':
if (cacheSignal) {
cacheSignal.endRead()
cacheSignal = null
}
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
markCurrentScopeAsDynamic(
workStore,
workUnitStore,
`revalidate: 0 fetch ${input} ${workStore.route}`
)
}
// We only want to set the revalidate store's revalidate time if it
// was explicitly set for the fetch call, i.e.
// originalFetchRevalidate.
if (revalidateStore && originalFetchRevalidate === finalRevalidate) {
revalidateStore.revalidate = finalRevalidate
}
}
const isCacheableRevalidate =
typeof finalRevalidate === 'number' && finalRevalidate > 0
let cacheKey: string | undefined
const { incrementalCache } = workStore
let isHmrRefresh = false
let serverComponentsHmrCache: ServerComponentsHmrCache | undefined
if (workUnitStore) {
switch (workUnitStore.type) {
case 'request':
case 'cache':
case 'private-cache':
isHmrRefresh = workUnitStore.isHmrRefresh ?? false
serverComponentsHmrCache = workUnitStore.serverComponentsHmrCache
break
case 'prerender':
case 'prerender-client':
case 'prerender-ppr':
case 'prerender-legacy':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
if (
incrementalCache &&
(isCacheableRevalidate || serverComponentsHmrCache)
) {
try {
cacheKey = await incrementalCache.generateCacheKey(
fetchUrl,
isRequestInput ? (input as RequestInit) : init
)
} catch (err) {
console.error(`Failed to generate cache key for`, input)
}
}
const fetchIdx = workStore.nextFetchId ?? 1
workStore.nextFetchId = fetchIdx + 1
let handleUnlock: () => Promise<void> | void = () => {}
const doOriginalFetch = async (
isStale?: boolean,
cacheReasonOverride?: string
) => {
const requestInputFields = [
'cache',
'credentials',
'headers',
'integrity',
'keepalive',
'method',
'mode',
'redirect',
'referrer',
'referrerPolicy',
'window',
'duplex',
// don't pass through signal when revalidating
...(isStale ? [] : ['signal']),
]
if (isRequestInput) {
const reqInput: Request = input as any
const reqOptions: RequestInit = {
body: (reqInput as any)._ogBody || reqInput.body,
}
for (const field of requestInputFields) {
// @ts-expect-error custom fields
reqOptions[field] = reqInput[field]
}
input = new Request(reqInput.url, reqOptions)
} else if (init) {
const { _ogBody, body, signal, ...otherInput } =
init as RequestInit & { _ogBody?: any }
init = {
...otherInput,
body: _ogBody || body,
signal: isStale ? undefined : signal,
}
}
// add metadata to init without editing the original
const clonedInit = {
...init,
next: { ...init?.next, fetchType: 'origin', fetchIdx },
}
return originFetch(input, clonedInit)
.then(async (res) => {
if (!isStale && fetchStart) {
trackFetchMetric(workStore, {
start: fetchStart,
url: fetchUrl,
cacheReason: cacheReasonOverride || cacheReason,
cacheStatus:
finalRevalidate === 0 || cacheReasonOverride
? 'skip'
: 'miss',
cacheWarning,
status: res.status,
method: clonedInit.method || 'GET',
})
}
if (
res.status === 200 &&
incrementalCache &&
cacheKey &&
(isCacheableRevalidate || serverComponentsHmrCache)
) {
const normalizedRevalidate =
finalRevalidate >= INFINITE_CACHE
? CACHE_ONE_YEAR
: finalRevalidate
const incrementalCacheConfig:
| SetIncrementalFetchCacheContext
| undefined = isCacheableRevalidate
? {
fetchCache: true,
fetchUrl,
fetchIdx,
tags,
isImplicitBuildTimeCache,
}
: undefined
switch (workUnitStore?.type) {
case 'prerender':
case 'prerender-client':
return createCachedPrerenderResponse(
res,
cacheKey,
incrementalCacheConfig,
incrementalCache,
normalizedRevalidate,
handleUnlock
)
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
case undefined:
return createCachedDynamicResponse(
workStore,
res,
cacheKey,
incrementalCacheConfig,
incrementalCache,
serverComponentsHmrCache,
normalizedRevalidate,
input,
handleUnlock
)
default:
workUnitStore satisfies never
}
}
// we had response that we determined shouldn't be cached so we return it
// and don't cache it. This also needs to unlock the cache lock we acquired.
await handleUnlock()
return res
})
.catch((error) => {
handleUnlock()
throw error
})
}
let cacheReasonOverride
let isForegroundRevalidate = false
let isHmrRefreshCache = false
if (cacheKey && incrementalCache) {
let cachedFetchData: CachedFetchData | undefined
if (isHmrRefresh && serverComponentsHmrCache) {
cachedFetchData = serverComponentsHmrCache.get(cacheKey)
isHmrRefreshCache = true
}
if (isCacheableRevalidate && !cachedFetchData) {
handleUnlock = await incrementalCache.lock(cacheKey)
const entry = workStore.isOnDemandRevalidate
? null
: await incrementalCache.get(cacheKey, {
kind: IncrementalCacheKind.FETCH,
revalidate: finalRevalidate,
fetchUrl,
fetchIdx,
tags,
softTags: implicitTags?.tags,
})
if (hasNoExplicitCacheConfig && workUnitStore) {
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-client':
// We sometimes use the cache to dedupe fetches that do not
// specify a cache configuration. In these cases we want to
// make sure we still exclude them from prerenders if
// cacheComponents is on so we introduce an artificial task boundary
// here.
await waitAtLeastOneReactRenderTask()
break
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
if (entry) {
await handleUnlock()
} else {
// in dev, incremental cache response will be null in case the browser adds `cache-control: no-cache` in the request headers
cacheReasonOverride = 'cache-control: no-cache (hard refresh)'
}
if (entry?.value && entry.value.kind === CachedRouteKind.FETCH) {
// when stale and is revalidating we wait for fresh data
// so the revalidated entry has the updated data
if (workStore.isRevalidate && entry.isStale) {
isForegroundRevalidate = true
} else {
if (entry.isStale) {
workStore.pendingRevalidates ??= {}
if (!workStore.pendingRevalidates[cacheKey]) {
const pendingRevalidate = doOriginalFetch(true)
.then(async (response) => ({
body: await response.arrayBuffer(),
headers: response.headers,
status: response.status,
statusText: response.statusText,
}))
.finally(() => {
workStore.pendingRevalidates ??= {}
delete workStore.pendingRevalidates[cacheKey || '']
})
// Attach the empty catch here so we don't get a "unhandled
// promise rejection" warning.
pendingRevalidate.catch(console.error)
workStore.pendingRevalidates[cacheKey] = pendingRevalidate
}
}
cachedFetchData = entry.value.data
}
}
}
if (cachedFetchData) {
if (fetchStart) {
trackFetchMetric(workStore, {
start: fetchStart,
url: fetchUrl,
cacheReason,
cacheStatus: isHmrRefreshCache ? 'hmr' : 'hit',
cacheWarning,
status: cachedFetchData.status || 200,
method: init?.method || 'GET',
})
}
const response = new Response(
Buffer.from(cachedFetchData.body, 'base64'),
{
headers: cachedFetchData.headers,
status: cachedFetchData.status,
}
)
Object.defineProperty(response, 'url', {
value: cachedFetchData.url,
})
return response
}
}
if (workStore.isStaticGeneration && init && typeof init === 'object') {
const { cache } = init
// Delete `cache` property as Cloudflare Workers will throw an error
if (isEdgeRuntime) delete init.cache
if (cache === 'no-store') {
// If enabled, we should bail out of static generation.
if (workUnitStore) {
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-client':
if (cacheSignal) {
cacheSignal.endRead()
cacheSignal = null
}
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
markCurrentScopeAsDynamic(
workStore,
workUnitStore,
`no-store fetch ${input} ${workStore.route}`
)
}
const hasNextConfig = 'next' in init
const { next = {} } = init
if (
typeof next.revalidate === 'number' &&
revalidateStore &&
next.revalidate < revalidateStore.revalidate
) {
if (next.revalidate === 0) {
// If enabled, we should bail out of static generation.
if (workUnitStore) {
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-client':
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
case 'prerender-legacy':
case 'prerender-ppr':
break
default:
workUnitStore satisfies never
}
}
markCurrentScopeAsDynamic(
workStore,
workUnitStore,
`revalidate: 0 fetch ${input} ${workStore.route}`
)
}
if (!workStore.forceStatic || next.revalidate !== 0) {
revalidateStore.revalidate = next.revalidate
}
}
if (hasNextConfig) delete init.next
}
// if we are revalidating the whole page via time or on-demand and
// the fetch cache entry is stale we should still de-dupe the
// origin hit if it's a cache-able entry
if (cacheKey && isForegroundRevalidate) {
const pendingRevalidateKey = cacheKey
workStore.pendingRevalidates ??= {}
let pendingRevalidate =
workStore.pendingRevalidates[pendingRevalidateKey]
if (pendingRevalidate) {
const revalidatedResult: {
body: ArrayBuffer
headers: Headers
status: number
statusText: string
} = await pendingRevalidate
return new Response(revalidatedResult.body, {
headers: revalidatedResult.headers,
status: revalidatedResult.status,
statusText: revalidatedResult.statusText,
})
}
// We used to just resolve the Response and clone it however for
// static generation with cacheComponents we need the response to be able to
// be resolved in a microtask and cloning the response will never have
// a body that can resolve in a microtask in node (as observed through
// experimentation) So instead we await the body and then when it is
// available we construct manually cloned Response objects with the
// body as an ArrayBuffer. This will be resolvable in a microtask
// making it compatible with cacheComponents.
const pendingResponse = doOriginalFetch(true, cacheReasonOverride)
// We're cloning the response using this utility because there
// exists a bug in the undici library around response cloning.
// See the following pull request for more details:
// https://github.com/vercel/next.js/pull/73274
.then(cloneResponse)
pendingRevalidate = pendingResponse
.then(async (responses) => {
const response = responses[0]
return {
body: await response.arrayBuffer(),
headers: response.headers,
status: response.status,
statusText: response.statusText,
}
})
.finally(() => {
// If the pending revalidate is not present in the store, then
// we have nothing to delete.
if (!workStore.pendingRevalidates?.[pendingRevalidateKey]) {
return
}
delete workStore.pendingRevalidates[pendingRevalidateKey]
})
// Attach the empty catch here so we don't get a "unhandled promise
// rejection" warning
pendingRevalidate.catch(() => {})
workStore.pendingRevalidates[pendingRevalidateKey] = pendingRevalidate
return pendingResponse.then((responses) => responses[1])
} else {
return doOriginalFetch(false, cacheReasonOverride)
}
}
)
if (cacheSignal) {
try {
return await result
} finally {
if (cacheSignal) {
cacheSignal.endRead()
}
}
}
return result
}
// Attach the necessary properties to the patched fetch function.
// We don't use this to determine if the fetch function has been patched,
// but for external consumers to determine if the fetch function has been
// patched.
patched.__nextPatched = true as const
patched.__nextGetStaticStore = () => workAsyncStorage
patched._nextOriginalFetch = originFetch
;(globalThis as Record<symbol, unknown>)[NEXT_PATCH_SYMBOL] = true
// Assign the function name also as a name property, so that it's preserved
// even when mangling is enabled.
Object.defineProperty(patched, 'name', { value: 'fetch', writable: false })
return patched
}
// we patch fetch to collect cache information used for
// determining if a page is static or not
export function patchFetch(options: PatchableModule) {
// If we've already patched fetch, we should not patch it again.
if (isFetchPatched()) return
// Grab the original fetch function. We'll attach this so we can use it in
// the patched fetch function.
const original = createDedupeFetch(globalThis.fetch)
// Set the global fetch to the patched fetch.
globalThis.fetch = createPatchedFetcher(original, options)
}
|