added fonts

This commit is contained in:
guus committed 2025-07-02 11:28:41 +02:00
1 parent 4134e3a234
commit 142d2986b6
9079 files changed
+1498421 -2

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
import { Batcher } from '../../lib/batcher';
import { scheduleOnNextTick } from '../../lib/scheduler';
import { fromResponseCacheEntry, routeKindToIncrementalCacheKind, toResponseCacheEntry } from './utils';
export * from './types';
export default class ResponseCache {
constructor(minimalMode){
this.batcher = Batcher.create({
// Ensure on-demand revalidate doesn't block normal requests, it should be
// safe to run an on-demand revalidate for the same key as a normal request.
cacheKeyFn: ({ key, isOnDemandRevalidate })=>`${key}-${isOnDemandRevalidate ? '1' : '0'}`,
// We wait to do any async work until after we've added our promise to
// `pendingResponses` to ensure that any any other calls will reuse the
// same promise until we've fully finished our work.
schedulerFn: scheduleOnNextTick
});
// this is a hack to avoid Webpack knowing this is equal to this.minimalMode
// because we replace this.minimalMode to true in production bundles.
const minimalModeKey = 'minimalMode';
this[minimalModeKey] = minimalMode;
}
async get(key, responseGenerator, context) {
// If there is no key for the cache, we can't possibly look this up in the
// cache so just return the result of the response generator.
if (!key) {
return responseGenerator({
hasResolved: false,
previousCacheEntry: null
});
}
const { incrementalCache, isOnDemandRevalidate = false, isFallback = false, isRoutePPREnabled = false } = context;
const response = await this.batcher.batch({
key,
isOnDemandRevalidate
}, async (cacheKey, resolve)=>{
var _this_previousCacheItem;
// We keep the previous cache entry around to leverage when the
// incremental cache is disabled in minimal mode.
if (this.minimalMode && ((_this_previousCacheItem = this.previousCacheItem) == null ? void 0 : _this_previousCacheItem.key) === cacheKey && this.previousCacheItem.expiresAt > Date.now()) {
return this.previousCacheItem.entry;
}
// Coerce the kindHint into a given kind for the incremental cache.
const kind = routeKindToIncrementalCacheKind(context.routeKind);
let resolved = false;
let cachedResponse = null;
try {
cachedResponse = !this.minimalMode ? await incrementalCache.get(key, {
kind,
isRoutePPREnabled: context.isRoutePPREnabled,
isFallback
}) : null;
if (cachedResponse && !isOnDemandRevalidate) {
resolve(cachedResponse);
resolved = true;
if (!cachedResponse.isStale || context.isPrefetch) {
// The cached value is still valid, so we don't need
// to update it yet.
return null;
}
}
const cacheEntry = await responseGenerator({
hasResolved: resolved,
previousCacheEntry: cachedResponse,
isRevalidating: true
});
// If the cache entry couldn't be generated, we don't want to cache
// the result.
if (!cacheEntry) {
// Unset the previous cache item if it was set.
if (this.minimalMode) this.previousCacheItem = undefined;
return null;
}
const resolveValue = await fromResponseCacheEntry({
...cacheEntry,
isMiss: !cachedResponse
});
if (!resolveValue) {
// Unset the previous cache item if it was set.
if (this.minimalMode) this.previousCacheItem = undefined;
return null;
}
// For on-demand revalidate wait to resolve until cache is set.
// Otherwise resolve now.
if (!isOnDemandRevalidate && !resolved) {
resolve(resolveValue);
resolved = true;
}
// We want to persist the result only if it has a cache control value
// defined.
if (resolveValue.cacheControl) {
if (this.minimalMode) {
this.previousCacheItem = {
key: cacheKey,
entry: resolveValue,
expiresAt: Date.now() + 1000
};
} else {
await incrementalCache.set(key, resolveValue.value, {
cacheControl: resolveValue.cacheControl,
isRoutePPREnabled,
isFallback
});
}
}
return resolveValue;
} catch (err) {
// When a path is erroring we automatically re-set the existing cache
// with new revalidate and expire times to prevent non-stop retrying.
if (cachedResponse == null ? void 0 : cachedResponse.cacheControl) {
const newRevalidate = Math.min(Math.max(cachedResponse.cacheControl.revalidate || 3, 3), 30);
const newExpire = cachedResponse.cacheControl.expire === undefined ? undefined : Math.max(newRevalidate + 3, cachedResponse.cacheControl.expire);
await incrementalCache.set(key, cachedResponse.value, {
cacheControl: {
revalidate: newRevalidate,
expire: newExpire
},
isRoutePPREnabled,
isFallback
});
}
// While revalidating in the background we can't reject as we already
// resolved the cache entry so log the error here.
if (resolved) {
console.error(err);
return null;
}
// We haven't resolved yet, so let's throw to indicate an error.
throw err;
}
});
return toResponseCacheEntry(response);
}
}
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/response-cache/index.ts"],"sourcesContent":["import type {\n ResponseCacheEntry,\n ResponseGenerator,\n ResponseCacheBase,\n IncrementalResponseCacheEntry,\n IncrementalResponseCache,\n} from './types'\n\nimport { Batcher } from '../../lib/batcher'\nimport { scheduleOnNextTick } from '../../lib/scheduler'\nimport {\n fromResponseCacheEntry,\n routeKindToIncrementalCacheKind,\n toResponseCacheEntry,\n} from './utils'\nimport type { RouteKind } from '../route-kind'\n\nexport * from './types'\n\nexport default class ResponseCache implements ResponseCacheBase {\n private readonly batcher = Batcher.create<\n { key: string; isOnDemandRevalidate: boolean },\n IncrementalResponseCacheEntry | null,\n string\n >({\n // Ensure on-demand revalidate doesn't block normal requests, it should be\n // safe to run an on-demand revalidate for the same key as a normal request.\n cacheKeyFn: ({ key, isOnDemandRevalidate }) =>\n `${key}-${isOnDemandRevalidate ? '1' : '0'}`,\n // We wait to do any async work until after we've added our promise to\n // `pendingResponses` to ensure that any any other calls will reuse the\n // same promise until we've fully finished our work.\n schedulerFn: scheduleOnNextTick,\n })\n\n private previousCacheItem?: {\n key: string\n entry: IncrementalResponseCacheEntry | null\n expiresAt: number\n }\n\n private minimalMode?: boolean\n\n constructor(minimalMode: boolean) {\n // this is a hack to avoid Webpack knowing this is equal to this.minimalMode\n // because we replace this.minimalMode to true in production bundles.\n const minimalModeKey = 'minimalMode'\n this[minimalModeKey] = minimalMode\n }\n\n public async get(\n key: string | null,\n responseGenerator: ResponseGenerator,\n context: {\n routeKind: RouteKind\n isOnDemandRevalidate?: boolean\n isPrefetch?: boolean\n incrementalCache: IncrementalResponseCache\n isRoutePPREnabled?: boolean\n isFallback?: boolean\n }\n ): Promise<ResponseCacheEntry | null> {\n // If there is no key for the cache, we can't possibly look this up in the\n // cache so just return the result of the response generator.\n if (!key) {\n return responseGenerator({ hasResolved: false, previousCacheEntry: null })\n }\n\n const {\n incrementalCache,\n isOnDemandRevalidate = false,\n isFallback = false,\n isRoutePPREnabled = false,\n } = context\n\n const response = await this.batcher.batch(\n { key, isOnDemandRevalidate },\n async (cacheKey, resolve) => {\n // We keep the previous cache entry around to leverage when the\n // incremental cache is disabled in minimal mode.\n if (\n this.minimalMode &&\n this.previousCacheItem?.key === cacheKey &&\n this.previousCacheItem.expiresAt > Date.now()\n ) {\n return this.previousCacheItem.entry\n }\n\n // Coerce the kindHint into a given kind for the incremental cache.\n const kind = routeKindToIncrementalCacheKind(context.routeKind)\n\n let resolved = false\n let cachedResponse: IncrementalResponseCacheEntry | null = null\n try {\n cachedResponse = !this.minimalMode\n ? await incrementalCache.get(key, {\n kind,\n isRoutePPREnabled: context.isRoutePPREnabled,\n isFallback,\n })\n : null\n\n if (cachedResponse && !isOnDemandRevalidate) {\n resolve(cachedResponse)\n resolved = true\n\n if (!cachedResponse.isStale || context.isPrefetch) {\n // The cached value is still valid, so we don't need\n // to update it yet.\n return null\n }\n }\n\n const cacheEntry = await responseGenerator({\n hasResolved: resolved,\n previousCacheEntry: cachedResponse,\n isRevalidating: true,\n })\n\n // If the cache entry couldn't be generated, we don't want to cache\n // the result.\n if (!cacheEntry) {\n // Unset the previous cache item if it was set.\n if (this.minimalMode) this.previousCacheItem = undefined\n return null\n }\n\n const resolveValue = await fromResponseCacheEntry({\n ...cacheEntry,\n isMiss: !cachedResponse,\n })\n if (!resolveValue) {\n // Unset the previous cache item if it was set.\n if (this.minimalMode) this.previousCacheItem = undefined\n return null\n }\n\n // For on-demand revalidate wait to resolve until cache is set.\n // Otherwise resolve now.\n if (!isOnDemandRevalidate && !resolved) {\n resolve(resolveValue)\n resolved = true\n }\n\n // We want to persLine truncated
+19
View File
@@ -0,0 +1,19 @@
export var CachedRouteKind = /*#__PURE__*/ function(CachedRouteKind) {
CachedRouteKind["APP_PAGE"] = "APP_PAGE";
CachedRouteKind["APP_ROUTE"] = "APP_ROUTE";
CachedRouteKind["PAGES"] = "PAGES";
CachedRouteKind["FETCH"] = "FETCH";
CachedRouteKind["REDIRECT"] = "REDIRECT";
CachedRouteKind["IMAGE"] = "IMAGE";
return CachedRouteKind;
}({});
export var IncrementalCacheKind = /*#__PURE__*/ function(IncrementalCacheKind) {
IncrementalCacheKind["APP_PAGE"] = "APP_PAGE";
IncrementalCacheKind["APP_ROUTE"] = "APP_ROUTE";
IncrementalCacheKind["PAGES"] = "PAGES";
IncrementalCacheKind["FETCH"] = "FETCH";
IncrementalCacheKind["IMAGE"] = "IMAGE";
return IncrementalCacheKind;
}({});
//# sourceMappingURL=types.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/response-cache/types.ts"],"sourcesContent":["import type { OutgoingHttpHeaders } from 'http'\nimport type RenderResult from '../render-result'\nimport type { CacheControl, Revalidate } from '../lib/cache-control'\nimport type { RouteKind } from '../route-kind'\n\nexport interface ResponseCacheBase {\n get(\n key: string | null,\n responseGenerator: ResponseGenerator,\n context: {\n isOnDemandRevalidate?: boolean\n isPrefetch?: boolean\n incrementalCache: IncrementalCache\n /**\n * This is a hint to the cache to help it determine what kind of route\n * this is so it knows where to look up the cache entry from. If not\n * provided it will test the filesystem to check.\n */\n routeKind: RouteKind\n\n /**\n * True if this is a fallback request.\n */\n isFallback?: boolean\n\n /**\n * True if the route is enabled for PPR.\n */\n isRoutePPREnabled?: boolean\n }\n ): Promise<ResponseCacheEntry | null>\n}\n\n// The server components HMR cache might store other data as well in the future,\n// at which point this should be refactored to a discriminated union type.\nexport interface ServerComponentsHmrCache {\n get(key: string): CachedFetchData | undefined\n set(key: string, data: CachedFetchData): void\n}\n\nexport type CachedFetchData = {\n headers: Record<string, string>\n body: string\n url: string\n status?: number\n}\n\nexport const enum CachedRouteKind {\n APP_PAGE = 'APP_PAGE',\n APP_ROUTE = 'APP_ROUTE',\n PAGES = 'PAGES',\n FETCH = 'FETCH',\n REDIRECT = 'REDIRECT',\n IMAGE = 'IMAGE',\n}\n\nexport interface CachedFetchValue {\n kind: CachedRouteKind.FETCH\n data: CachedFetchData\n // tags are only present with file-system-cache\n // fetch cache stores tags outside of cache entry\n tags?: string[]\n revalidate: number\n}\n\nexport interface CachedRedirectValue {\n kind: CachedRouteKind.REDIRECT\n props: Object\n}\n\nexport interface CachedAppPageValue {\n kind: CachedRouteKind.APP_PAGE\n // this needs to be a RenderResult so since renderResponse\n // expects that type instead of a string\n html: RenderResult\n rscData: Buffer | undefined\n status: number | undefined\n postponed: string | undefined\n headers: OutgoingHttpHeaders | undefined\n segmentData: Map<string, Buffer> | undefined\n}\n\nexport interface CachedPageValue {\n kind: CachedRouteKind.PAGES\n // this needs to be a RenderResult so since renderResponse\n // expects that type instead of a string\n html: RenderResult\n pageData: Object\n status: number | undefined\n headers: OutgoingHttpHeaders | undefined\n}\n\nexport interface CachedRouteValue {\n kind: CachedRouteKind.APP_ROUTE\n // this needs to be a RenderResult so since renderResponse\n // expects that type instead of a string\n body: Buffer\n status: number\n headers: OutgoingHttpHeaders\n}\n\nexport interface CachedImageValue {\n kind: CachedRouteKind.IMAGE\n etag: string\n upstreamEtag: string\n buffer: Buffer\n extension: string\n isMiss?: boolean\n isStale?: boolean\n}\n\nexport interface IncrementalCachedAppPageValue {\n kind: CachedRouteKind.APP_PAGE\n // this needs to be a string since the cache expects to store\n // the string value\n html: string\n rscData: Buffer | undefined\n headers: OutgoingHttpHeaders | undefined\n postponed: string | undefined\n status: number | undefined\n segmentData: Map<string, Buffer> | undefined\n}\n\nexport interface IncrementalCachedPageValue {\n kind: CachedRouteKind.PAGES\n // this needs to be a string since the cache expects to store\n // the string value\n html: string\n pageData: Object\n headers: OutgoingHttpHeaders | undefined\n status: number | undefined\n}\n\nexport interface IncrementalResponseCacheEntry {\n cacheControl?: CacheControl\n /**\n * timestamp in milliseconds to revalidate after\n */\n revalidateAfter?: Revalidate\n /**\n * `-1` here dictates a blocking revalidate should be used\n */\n isStale?: boolean | -1\n isMiss?: boolean\n isFallback: boolean | undefined\n value: Exclude<IncrementalCacheValue, CachedFetchValue> | null\n}\n\nexport interface IncrementalFetchCacheEntry {\n /**\n * `-1` here dictates a blocking revalidate should be used\n */\n isStale?: boolean | -1\n value: CachedFetchValue\n}\n\nexport type IncrementalCacheEntry =\n | IncrementalResponseCacheEntry\n | IncrementalFetchCacheEntry\n\nexport type IncrementalCacheValue =\n | CachedRedirectValue\n | IncrementalCachedPageValue\n | IncrementalCachedAppPageValue\n | CachedImageValue\n | CachedFetchValue\n | CachedRouteValue\n\nexport type ResponseCacheValue =\n | CachedRedirectValue\n | CachedPageValue\n | CachedAppPageValue\n | CachedImageValue\n | CachedRouteValue\n\nexport type ResponseCacheEntry = {\n cacheControl?: CacheControl\n value: ResponseCacheValue | null\n isStale?: boolean | -1\n isMiss?: boolean\n isFLine truncated
+69
View File
@@ -0,0 +1,69 @@
import { CachedRouteKind, IncrementalCacheKind } from './types';
import RenderResult from '../render-result';
import { RouteKind } from '../route-kind';
export async function fromResponseCacheEntry(cacheEntry) {
var _cacheEntry_value, _cacheEntry_value1;
return {
...cacheEntry,
value: ((_cacheEntry_value = cacheEntry.value) == null ? void 0 : _cacheEntry_value.kind) === CachedRouteKind.PAGES ? {
kind: CachedRouteKind.PAGES,
html: await cacheEntry.value.html.toUnchunkedString(true),
pageData: cacheEntry.value.pageData,
headers: cacheEntry.value.headers,
status: cacheEntry.value.status
} : ((_cacheEntry_value1 = cacheEntry.value) == null ? void 0 : _cacheEntry_value1.kind) === CachedRouteKind.APP_PAGE ? {
kind: CachedRouteKind.APP_PAGE,
html: await cacheEntry.value.html.toUnchunkedString(true),
postponed: cacheEntry.value.postponed,
rscData: cacheEntry.value.rscData,
headers: cacheEntry.value.headers,
status: cacheEntry.value.status,
segmentData: cacheEntry.value.segmentData
} : cacheEntry.value
};
}
export async function toResponseCacheEntry(response) {
var _response_value, _response_value1;
if (!response) return null;
return {
isMiss: response.isMiss,
isStale: response.isStale,
cacheControl: response.cacheControl,
isFallback: response.isFallback,
value: ((_response_value = response.value) == null ? void 0 : _response_value.kind) === CachedRouteKind.PAGES ? {
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(response.value.html),
pageData: response.value.pageData,
headers: response.value.headers,
status: response.value.status
} : ((_response_value1 = response.value) == null ? void 0 : _response_value1.kind) === CachedRouteKind.APP_PAGE ? {
kind: CachedRouteKind.APP_PAGE,
html: RenderResult.fromStatic(response.value.html),
rscData: response.value.rscData,
headers: response.value.headers,
status: response.value.status,
postponed: response.value.postponed,
segmentData: response.value.segmentData
} : response.value
};
}
export function routeKindToIncrementalCacheKind(routeKind) {
switch(routeKind){
case RouteKind.PAGES:
return IncrementalCacheKind.PAGES;
case RouteKind.APP_PAGE:
return IncrementalCacheKind.APP_PAGE;
case RouteKind.IMAGE:
return IncrementalCacheKind.IMAGE;
case RouteKind.APP_ROUTE:
return IncrementalCacheKind.APP_ROUTE;
default:
throw Object.defineProperty(new Error(`Unexpected route kind ${routeKind}`), "__NEXT_ERROR_CODE", {
value: "E64",
enumerable: false,
configurable: true
});
}
}
//# sourceMappingURL=utils.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/response-cache/utils.ts"],"sourcesContent":["import {\n CachedRouteKind,\n IncrementalCacheKind,\n type CachedAppPageValue,\n type CachedPageValue,\n type IncrementalResponseCacheEntry,\n type ResponseCacheEntry,\n} from './types'\n\nimport RenderResult from '../render-result'\nimport { RouteKind } from '../route-kind'\n\nexport async function fromResponseCacheEntry(\n cacheEntry: ResponseCacheEntry\n): Promise<IncrementalResponseCacheEntry> {\n return {\n ...cacheEntry,\n value:\n cacheEntry.value?.kind === CachedRouteKind.PAGES\n ? {\n kind: CachedRouteKind.PAGES,\n html: await cacheEntry.value.html.toUnchunkedString(true),\n pageData: cacheEntry.value.pageData,\n headers: cacheEntry.value.headers,\n status: cacheEntry.value.status,\n }\n : cacheEntry.value?.kind === CachedRouteKind.APP_PAGE\n ? {\n kind: CachedRouteKind.APP_PAGE,\n html: await cacheEntry.value.html.toUnchunkedString(true),\n postponed: cacheEntry.value.postponed,\n rscData: cacheEntry.value.rscData,\n headers: cacheEntry.value.headers,\n status: cacheEntry.value.status,\n segmentData: cacheEntry.value.segmentData,\n }\n : cacheEntry.value,\n }\n}\n\nexport async function toResponseCacheEntry(\n response: IncrementalResponseCacheEntry | null\n): Promise<ResponseCacheEntry | null> {\n if (!response) return null\n\n return {\n isMiss: response.isMiss,\n isStale: response.isStale,\n cacheControl: response.cacheControl,\n isFallback: response.isFallback,\n value:\n response.value?.kind === CachedRouteKind.PAGES\n ? ({\n kind: CachedRouteKind.PAGES,\n html: RenderResult.fromStatic(response.value.html),\n pageData: response.value.pageData,\n headers: response.value.headers,\n status: response.value.status,\n } satisfies CachedPageValue)\n : response.value?.kind === CachedRouteKind.APP_PAGE\n ? ({\n kind: CachedRouteKind.APP_PAGE,\n html: RenderResult.fromStatic(response.value.html),\n rscData: response.value.rscData,\n headers: response.value.headers,\n status: response.value.status,\n postponed: response.value.postponed,\n segmentData: response.value.segmentData,\n } satisfies CachedAppPageValue)\n : response.value,\n }\n}\n\nexport function routeKindToIncrementalCacheKind(\n routeKind: RouteKind\n): Exclude<IncrementalCacheKind, IncrementalCacheKind.FETCH> {\n switch (routeKind) {\n case RouteKind.PAGES:\n return IncrementalCacheKind.PAGES\n case RouteKind.APP_PAGE:\n return IncrementalCacheKind.APP_PAGE\n case RouteKind.IMAGE:\n return IncrementalCacheKind.IMAGE\n case RouteKind.APP_ROUTE:\n return IncrementalCacheKind.APP_ROUTE\n default:\n throw new Error(`Unexpected route kind ${routeKind}`)\n }\n}\n"],"names":["CachedRouteKind","IncrementalCacheKind","RenderResult","RouteKind","fromResponseCacheEntry","cacheEntry","value","kind","PAGES","html","toUnchunkedString","pageData","headers","status","APP_PAGE","postponed","rscData","segmentData","toResponseCacheEntry","response","isMiss","isStale","cacheControl","isFallback","fromStatic","routeKindToIncrementalCacheKind","routeKind","IMAGE","APP_ROUTE","Error"],"mappings":"AAAA,SACEA,eAAe,EACfC,oBAAoB,QAKf,UAAS;AAEhB,OAAOC,kBAAkB,mBAAkB;AAC3C,SAASC,SAAS,QAAQ,gBAAe;AAEzC,OAAO,eAAeC,uBACpBC,UAA8B;QAK1BA,mBAQIA;IAXR,OAAO;QACL,GAAGA,UAAU;QACbC,OACED,EAAAA,oBAAAA,WAAWC,KAAK,qBAAhBD,kBAAkBE,IAAI,MAAKP,gBAAgBQ,KAAK,GAC5C;YACED,MAAMP,gBAAgBQ,KAAK;YAC3BC,MAAM,MAAMJ,WAAWC,KAAK,CAACG,IAAI,CAACC,iBAAiB,CAAC;YACpDC,UAAUN,WAAWC,KAAK,CAACK,QAAQ;YACnCC,SAASP,WAAWC,KAAK,CAACM,OAAO;YACjCC,QAAQR,WAAWC,KAAK,CAACO,MAAM;QACjC,IACAR,EAAAA,qBAAAA,WAAWC,KAAK,qBAAhBD,mBAAkBE,IAAI,MAAKP,gBAAgBc,QAAQ,GACjD;YACEP,MAAMP,gBAAgBc,QAAQ;YAC9BL,MAAM,MAAMJ,WAAWC,KAAK,CAACG,IAAI,CAACC,iBAAiB,CAAC;YACpDK,WAAWV,WAAWC,KAAK,CAACS,SAAS;YACrCC,SAASX,WAAWC,KAAK,CAACU,OAAO;YACjCJ,SAASP,WAAWC,KAAK,CAACM,OAAO;YACjCC,QAAQR,WAAWC,KAAK,CAACO,MAAM;YAC/BI,aAAaZ,WAAWC,KAAK,CAACW,WAAW;QAC3C,IACAZ,WAAWC,KAAK;IAC1B;AACF;AAEA,OAAO,eAAeY,qBACpBC,QAA8C;QAU1CA,iBAQIA;IAhBR,IAAI,CAACA,UAAU,OAAO;IAEtB,OAAO;QACLC,QAAQD,SAASC,MAAM;QACvBC,SAASF,SAASE,OAAO;QACzBC,cAAcH,SAASG,YAAY;QACnCC,YAAYJ,SAASI,UAAU;QAC/BjB,OACEa,EAAAA,kBAAAA,SAASb,KAAK,qBAAda,gBAAgBZ,IAAI,MAAKP,gBAAgBQ,KAAK,GACzC;YACCD,MAAMP,gBAAgBQ,KAAK;YAC3BC,MAAMP,aAAasB,UAAU,CAACL,SAASb,KAAK,CAACG,IAAI;YACjDE,UAAUQ,SAASb,KAAK,CAACK,QAAQ;YACjCC,SAASO,SAASb,KAAK,CAACM,OAAO;YAC/BC,QAAQM,SAASb,KAAK,CAACO,MAAM;QAC/B,IACAM,EAAAA,mBAAAA,SAASb,KAAK,qBAAda,iBAAgBZ,IAAI,MAAKP,gBAAgBc,QAAQ,GAC9C;YACCP,MAAMP,gBAAgBc,QAAQ;YAC9BL,MAAMP,aAAasB,UAAU,CAACL,SAASb,KAAK,CAACG,IAAI;YACjDO,SAASG,SAASb,KLine truncated
+91
View File
@@ -0,0 +1,91 @@
import { DetachedPromise } from '../../lib/detached-promise';
/**
* In the web server, there is currently no incremental cache provided and we
* always SSR the page.
*/ export default class WebResponseCache {
constructor(minimalMode){
this.pendingResponses = new Map();
// this is a hack to avoid Webpack knowing this is equal to this.minimalMode
// because we replace this.minimalMode to true in production bundles.
Object.assign(this, {
minimalMode
});
}
get(key, responseGenerator, context) {
var _this_previousCacheItem;
// ensure on-demand revalidate doesn't block normal requests
const pendingResponseKey = key ? `${key}-${context.isOnDemandRevalidate ? '1' : '0'}` : null;
const pendingResponse = pendingResponseKey ? this.pendingResponses.get(pendingResponseKey) : null;
if (pendingResponse) {
return pendingResponse;
}
const { promise, resolve: resolver, reject: rejecter } = new DetachedPromise();
if (pendingResponseKey) {
this.pendingResponses.set(pendingResponseKey, promise);
}
let hasResolved = false;
const resolve = (cacheEntry)=>{
if (pendingResponseKey) {
// Ensure all reads from the cache get the latest value.
this.pendingResponses.set(pendingResponseKey, Promise.resolve(cacheEntry));
}
if (!hasResolved) {
hasResolved = true;
resolver(cacheEntry);
}
};
// we keep the previous cache entry around to leverage
// when the incremental cache is disabled in minimal mode
if (pendingResponseKey && this.minimalMode && ((_this_previousCacheItem = this.previousCacheItem) == null ? void 0 : _this_previousCacheItem.key) === pendingResponseKey && this.previousCacheItem.expiresAt > Date.now()) {
resolve(this.previousCacheItem.entry);
this.pendingResponses.delete(pendingResponseKey);
return promise;
}
// We wait to do any async work until after we've added our promise to
// `pendingResponses` to ensure that any any other calls will reuse the
// same promise until we've fully finished our work.
;
(async ()=>{
try {
const cacheEntry = await responseGenerator({
hasResolved
});
const resolveValue = cacheEntry === null ? null : {
...cacheEntry,
isMiss: true
};
// for on-demand revalidate wait to resolve until cache is set
if (!context.isOnDemandRevalidate) {
resolve(resolveValue);
}
if (key && cacheEntry && cacheEntry.cacheControl) {
this.previousCacheItem = {
key: pendingResponseKey || key,
entry: cacheEntry,
expiresAt: Date.now() + 1000
};
} else {
this.previousCacheItem = undefined;
}
if (context.isOnDemandRevalidate) {
resolve(resolveValue);
}
} catch (err) {
// while revalidating in the background we can't reject as
// we already resolved the cache entry so log the error here
if (hasResolved) {
console.error(err);
} else {
rejecter(err);
}
} finally{
if (pendingResponseKey) {
this.pendingResponses.delete(pendingResponseKey);
}
}
})();
return promise;
}
}
//# sourceMappingURL=web.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/response-cache/web.ts"],"sourcesContent":["import { DetachedPromise } from '../../lib/detached-promise'\nimport type { ResponseCacheEntry, ResponseGenerator } from './types'\n\n/**\n * In the web server, there is currently no incremental cache provided and we\n * always SSR the page.\n */\nexport default class WebResponseCache {\n pendingResponses: Map<string, Promise<ResponseCacheEntry | null>>\n previousCacheItem?: {\n key: string\n entry: ResponseCacheEntry | null\n expiresAt: number\n }\n minimalMode?: boolean\n\n constructor(minimalMode: boolean) {\n this.pendingResponses = new Map()\n // this is a hack to avoid Webpack knowing this is equal to this.minimalMode\n // because we replace this.minimalMode to true in production bundles.\n Object.assign(this, { minimalMode })\n }\n\n public get(\n key: string | null,\n responseGenerator: ResponseGenerator,\n context: {\n isOnDemandRevalidate?: boolean\n isPrefetch?: boolean\n incrementalCache: any\n }\n ): Promise<ResponseCacheEntry | null> {\n // ensure on-demand revalidate doesn't block normal requests\n const pendingResponseKey = key\n ? `${key}-${context.isOnDemandRevalidate ? '1' : '0'}`\n : null\n\n const pendingResponse = pendingResponseKey\n ? this.pendingResponses.get(pendingResponseKey)\n : null\n if (pendingResponse) {\n return pendingResponse\n }\n\n const {\n promise,\n resolve: resolver,\n reject: rejecter,\n } = new DetachedPromise<ResponseCacheEntry | null>()\n if (pendingResponseKey) {\n this.pendingResponses.set(pendingResponseKey, promise)\n }\n\n let hasResolved = false\n const resolve = (cacheEntry: ResponseCacheEntry | null) => {\n if (pendingResponseKey) {\n // Ensure all reads from the cache get the latest value.\n this.pendingResponses.set(\n pendingResponseKey,\n Promise.resolve(cacheEntry)\n )\n }\n if (!hasResolved) {\n hasResolved = true\n resolver(cacheEntry)\n }\n }\n\n // we keep the previous cache entry around to leverage\n // when the incremental cache is disabled in minimal mode\n if (\n pendingResponseKey &&\n this.minimalMode &&\n this.previousCacheItem?.key === pendingResponseKey &&\n this.previousCacheItem.expiresAt > Date.now()\n ) {\n resolve(this.previousCacheItem.entry)\n this.pendingResponses.delete(pendingResponseKey)\n return promise\n }\n\n // We wait to do any async work until after we've added our promise to\n // `pendingResponses` to ensure that any any other calls will reuse the\n // same promise until we've fully finished our work.\n ;(async () => {\n try {\n const cacheEntry = await responseGenerator({ hasResolved })\n const resolveValue =\n cacheEntry === null\n ? null\n : {\n ...cacheEntry,\n isMiss: true,\n }\n\n // for on-demand revalidate wait to resolve until cache is set\n if (!context.isOnDemandRevalidate) {\n resolve(resolveValue)\n }\n\n if (key && cacheEntry && cacheEntry.cacheControl) {\n this.previousCacheItem = {\n key: pendingResponseKey || key,\n entry: cacheEntry,\n expiresAt: Date.now() + 1000,\n }\n } else {\n this.previousCacheItem = undefined\n }\n\n if (context.isOnDemandRevalidate) {\n resolve(resolveValue)\n }\n } catch (err) {\n // while revalidating in the background we can't reject as\n // we already resolved the cache entry so log the error here\n if (hasResolved) {\n console.error(err)\n } else {\n rejecter(err as Error)\n }\n } finally {\n if (pendingResponseKey) {\n this.pendingResponses.delete(pendingResponseKey)\n }\n }\n })()\n return promise\n }\n}\n"],"names":["DetachedPromise","WebResponseCache","constructor","minimalMode","pendingResponses","Map","Object","assign","get","key","responseGenerator","context","pendingResponseKey","isOnDemandRevalidate","pendingResponse","promise","resolve","resolver","reject","rejecter","set","hasResolved","cacheEntry","Promise","previousCacheItem","expiresAt","Date","now","entry","delete","resolveValue","isMiss","cacheControl","undefined","err","console","error"],"mappings":"AAAA,SAASA,eAAe,QAAQ,6BAA4B;AAG5D;;;CAGC,GACD,eAAe,MAAMC;IASnBC,YAAYC,WAAoB,CAAE;QAChC,IAAI,CAACC,gBAAgB,GAAG,IAAIC;QAC5B,4EAA4E;QAC5E,qEAAqE;QACrEC,OAAOC,MAAM,CAAC,IAAI,EAAE;YAAEJ;QAAY;IACpC;IAEOK,IACLC,GAAkB,EAClBC,iBAAoC,EACpCC,OAIC,EACmC;YA0ClC;QAzCF,4DAA4D;QAC5D,MAAMC,qBAAqBH,MACvB,GAAGA,IAAI,CAAC,EAAEE,QAAQE,oBAAoB,GAAG,MAAM,KAAK,GACpD;QAEJ,MAAMC,kBAAkBF,qBACpB,IAAI,CAACR,gBAAgB,CAACI,GAAG,CAACI,sBAC1B;QACJ,IAAIE,iBAAiB;YACnB,OAAOALine truncated