This commit is contained in:
Kismet Hasanaj
2026-05-02 20:07:02 +02:00
parent ce8672e283
commit 34dc9aec52
9428 changed files with 1733330 additions and 0 deletions
@@ -0,0 +1,320 @@
import { CachedRouteKind, IncrementalCacheKind } from '../../response-cache';
import path from '../../../shared/lib/isomorphic/path';
import { NEXT_CACHE_TAGS_HEADER, NEXT_DATA_SUFFIX, NEXT_META_SUFFIX, RSC_SEGMENT_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX, RSC_SUFFIX } from '../../../lib/constants';
import { areTagsExpired, tagsManifest } from './tags-manifest.external';
import { MultiFileWriter } from '../../../lib/multi-file-writer';
import { getMemoryCache } from './memory-cache.external';
export default class FileSystemCache {
static #_ = this.debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE;
constructor(ctx){
this.fs = ctx.fs;
this.flushToDisk = ctx.flushToDisk;
this.serverDistDir = ctx.serverDistDir;
this.revalidatedTags = ctx.revalidatedTags;
if (ctx.maxMemoryCacheSize) {
if (!FileSystemCache.memoryCache) {
if (FileSystemCache.debug) {
console.log('FileSystemCache: using memory store for fetch cache');
}
FileSystemCache.memoryCache = getMemoryCache(ctx.maxMemoryCacheSize);
} else if (FileSystemCache.debug) {
console.log('FileSystemCache: memory store already initialized');
}
} else if (FileSystemCache.debug) {
console.log('FileSystemCache: not using memory store for fetch cache');
}
}
resetRequestCache() {}
async revalidateTag(tags, durations) {
tags = typeof tags === 'string' ? [
tags
] : tags;
if (FileSystemCache.debug) {
console.log('FileSystemCache: revalidateTag', tags, durations);
}
if (tags.length === 0) {
return;
}
const now = Date.now();
for (const tag of tags){
const existingEntry = tagsManifest.get(tag) || {};
if (durations) {
// Use provided durations directly
const updates = {
...existingEntry
};
// mark as stale immediately
updates.stale = now;
if (durations.expire !== undefined) {
updates.expired = now + durations.expire * 1000 // Convert seconds to ms
;
}
tagsManifest.set(tag, updates);
} else {
// Update expired field for immediate expiration (default behavior when no durations provided)
tagsManifest.set(tag, {
...existingEntry,
expired: now
});
}
}
}
async get(...args) {
var _FileSystemCache_memoryCache, _data_value, _data_value1, _data_value2, _data_value3;
const [key, ctx] = args;
const { kind } = ctx;
let data = (_FileSystemCache_memoryCache = FileSystemCache.memoryCache) == null ? void 0 : _FileSystemCache_memoryCache.get(key);
if (FileSystemCache.debug) {
if (kind === IncrementalCacheKind.FETCH) {
console.log('FileSystemCache: get', key, ctx.tags, kind, !!data);
} else {
console.log('FileSystemCache: get', key, kind, !!data);
}
}
// let's check the disk for seed data
if (!data && process.env.NEXT_RUNTIME !== 'edge') {
try {
if (kind === IncrementalCacheKind.APP_ROUTE) {
const filePath = this.getFilePath(`${key}.body`, IncrementalCacheKind.APP_ROUTE);
const fileData = await this.fs.readFile(filePath);
const { mtime } = await this.fs.stat(filePath);
const meta = JSON.parse(await this.fs.readFile(filePath.replace(/\.body$/, NEXT_META_SUFFIX), 'utf8'));
data = {
lastModified: mtime.getTime(),
value: {
kind: CachedRouteKind.APP_ROUTE,
body: fileData,
headers: meta.headers,
status: meta.status
}
};
} else {
const filePath = this.getFilePath(kind === IncrementalCacheKind.FETCH ? key : `${key}.html`, kind);
const fileData = await this.fs.readFile(filePath, 'utf8');
const { mtime } = await this.fs.stat(filePath);
if (kind === IncrementalCacheKind.FETCH) {
var _data_value4;
const { tags, fetchIdx, fetchUrl } = ctx;
if (!this.flushToDisk) return null;
const lastModified = mtime.getTime();
const parsedData = JSON.parse(fileData);
data = {
lastModified,
value: parsedData
};
if (((_data_value4 = data.value) == null ? void 0 : _data_value4.kind) === CachedRouteKind.FETCH) {
var _data_value5;
const storedTags = (_data_value5 = data.value) == null ? void 0 : _data_value5.tags;
// update stored tags if a new one is being added
// TODO: remove this when we can send the tags
// via header on GET same as SET
if (!(tags == null ? void 0 : tags.every((tag)=>storedTags == null ? void 0 : storedTags.includes(tag)))) {
if (FileSystemCache.debug) {
console.log('FileSystemCache: tags vs storedTags mismatch', tags, storedTags);
}
await this.set(key, data.value, {
fetchCache: true,
tags,
fetchIdx,
fetchUrl
});
}
}
} else if (kind === IncrementalCacheKind.APP_PAGE) {
// We try to load the metadata file, but if it fails, we don't
// error. We also don't load it if this is a fallback.
let meta;
try {
meta = JSON.parse(await this.fs.readFile(filePath.replace(/\.html$/, NEXT_META_SUFFIX), 'utf8'));
} catch {}
let maybeSegmentData;
if (meta == null ? void 0 : meta.segmentPaths) {
// Collect all the segment data for this page.
// TODO: To optimize file system reads, we should consider creating
// separate cache entries for each segment, rather than storing them
// all on the page's entry. Though the behavior is
// identical regardless.
const segmentData = new Map();
maybeSegmentData = segmentData;
const segmentsDir = key + RSC_SEGMENTS_DIR_SUFFIX;
await Promise.all(meta.segmentPaths.map(async (segmentPath)=>{
const segmentDataFilePath = this.getFilePath(segmentsDir + segmentPath + RSC_SEGMENT_SUFFIX, IncrementalCacheKind.APP_PAGE);
try {
segmentData.set(segmentPath, await this.fs.readFile(segmentDataFilePath));
} catch {
// This shouldn't happen, but if for some reason we fail to
// load a segment from the filesystem, treat it the same as if
// the segment is dynamic and does not have a prefetch.
}
}));
}
let rscData;
if (!ctx.isFallback && (!ctx.isRoutePPREnabled || (meta == null ? void 0 : meta.postponed) == null)) {
rscData = await this.fs.readFile(this.getFilePath(`${key}${RSC_SUFFIX}`, IncrementalCacheKind.APP_PAGE));
}
data = {
lastModified: mtime.getTime(),
value: {
kind: CachedRouteKind.APP_PAGE,
html: fileData,
rscData,
postponed: meta == null ? void 0 : meta.postponed,
headers: meta == null ? void 0 : meta.headers,
status: meta == null ? void 0 : meta.status,
segmentData: maybeSegmentData
}
};
} else if (kind === IncrementalCacheKind.PAGES) {
let meta;
let pageData = {};
if (!ctx.isFallback) {
pageData = JSON.parse(await this.fs.readFile(this.getFilePath(`${key}${NEXT_DATA_SUFFIX}`, IncrementalCacheKind.PAGES), 'utf8'));
}
data = {
lastModified: mtime.getTime(),
value: {
kind: CachedRouteKind.PAGES,
html: fileData,
pageData,
headers: meta == null ? void 0 : meta.headers,
status: meta == null ? void 0 : meta.status
}
};
} else {
throw Object.defineProperty(new Error(`Invariant: Unexpected route kind ${kind} in file system cache.`), "__NEXT_ERROR_CODE", {
value: "E445",
enumerable: false,
configurable: true
});
}
}
if (data) {
var _FileSystemCache_memoryCache1;
(_FileSystemCache_memoryCache1 = FileSystemCache.memoryCache) == null ? void 0 : _FileSystemCache_memoryCache1.set(key, data);
}
} catch {
return null;
}
}
if ((data == null ? void 0 : (_data_value = data.value) == null ? void 0 : _data_value.kind) === CachedRouteKind.APP_PAGE || (data == null ? void 0 : (_data_value1 = data.value) == null ? void 0 : _data_value1.kind) === CachedRouteKind.APP_ROUTE || (data == null ? void 0 : (_data_value2 = data.value) == null ? void 0 : _data_value2.kind) === CachedRouteKind.PAGES) {
var _data_value_headers;
const tagsHeader = (_data_value_headers = data.value.headers) == null ? void 0 : _data_value_headers[NEXT_CACHE_TAGS_HEADER];
if (typeof tagsHeader === 'string') {
const cacheTags = tagsHeader.split(',');
// we trigger a blocking validation if an ISR page
// had a tag revalidated, if we want to be a background
// revalidation instead we return data.lastModified = -1
if (cacheTags.length > 0 && areTagsExpired(cacheTags, data.lastModified)) {
if (FileSystemCache.debug) {
console.log('FileSystemCache: expired tags', cacheTags);
}
return null;
}
}
} else if ((data == null ? void 0 : (_data_value3 = data.value) == null ? void 0 : _data_value3.kind) === CachedRouteKind.FETCH) {
const combinedTags = ctx.kind === IncrementalCacheKind.FETCH ? [
...ctx.tags || [],
...ctx.softTags || []
] : [];
// When revalidate tag is called we don't return stale data so it's
// updated right away.
if (combinedTags.some((tag)=>this.revalidatedTags.includes(tag))) {
if (FileSystemCache.debug) {
console.log('FileSystemCache: was revalidated', combinedTags);
}
return null;
}
if (areTagsExpired(combinedTags, data.lastModified)) {
if (FileSystemCache.debug) {
console.log('FileSystemCache: expired tags', combinedTags);
}
return null;
}
}
return data ?? null;
}
async set(key, data, ctx) {
var _FileSystemCache_memoryCache;
(_FileSystemCache_memoryCache = FileSystemCache.memoryCache) == null ? void 0 : _FileSystemCache_memoryCache.set(key, {
value: data,
lastModified: Date.now()
});
if (FileSystemCache.debug) {
console.log('FileSystemCache: set', key);
}
if (!this.flushToDisk || !data) return;
// Create a new writer that will prepare to write all the files to disk
// after their containing directory is created.
const writer = new MultiFileWriter(this.fs);
if (data.kind === CachedRouteKind.APP_ROUTE) {
const filePath = this.getFilePath(`${key}.body`, IncrementalCacheKind.APP_ROUTE);
writer.append(filePath, data.body);
const meta = {
headers: data.headers,
status: data.status,
postponed: undefined,
segmentPaths: undefined,
prefetchHints: undefined
};
writer.append(filePath.replace(/\.body$/, NEXT_META_SUFFIX), JSON.stringify(meta, null, 2));
} else if (data.kind === CachedRouteKind.PAGES || data.kind === CachedRouteKind.APP_PAGE) {
const isAppPath = data.kind === CachedRouteKind.APP_PAGE;
const htmlPath = this.getFilePath(`${key}.html`, isAppPath ? IncrementalCacheKind.APP_PAGE : IncrementalCacheKind.PAGES);
writer.append(htmlPath, data.html);
// Fallbacks don't generate a data file.
if (!ctx.fetchCache && !ctx.isFallback && !ctx.isRoutePPREnabled) {
writer.append(this.getFilePath(`${key}${isAppPath ? RSC_SUFFIX : NEXT_DATA_SUFFIX}`, isAppPath ? IncrementalCacheKind.APP_PAGE : IncrementalCacheKind.PAGES), isAppPath ? data.rscData : JSON.stringify(data.pageData));
}
if ((data == null ? void 0 : data.kind) === CachedRouteKind.APP_PAGE) {
let segmentPaths;
if (data.segmentData) {
segmentPaths = [];
const segmentsDir = htmlPath.replace(/\.html$/, RSC_SEGMENTS_DIR_SUFFIX);
for (const [segmentPath, buffer] of data.segmentData){
segmentPaths.push(segmentPath);
const segmentDataFilePath = segmentsDir + segmentPath + RSC_SEGMENT_SUFFIX;
writer.append(segmentDataFilePath, buffer);
}
}
const meta = {
headers: data.headers,
status: data.status,
postponed: data.postponed,
segmentPaths,
prefetchHints: undefined
};
writer.append(htmlPath.replace(/\.html$/, NEXT_META_SUFFIX), JSON.stringify(meta));
}
} else if (data.kind === CachedRouteKind.FETCH) {
const filePath = this.getFilePath(key, IncrementalCacheKind.FETCH);
writer.append(filePath, JSON.stringify({
...data,
tags: ctx.fetchCache ? ctx.tags : []
}));
}
// Wait for all FS operations to complete.
await writer.wait();
}
getFilePath(pathname, kind) {
switch(kind){
case IncrementalCacheKind.FETCH:
// we store in .next/cache/fetch-cache so it can be persisted
// across deploys
return path.join(this.serverDistDir, '..', 'cache', 'fetch-cache', pathname);
case IncrementalCacheKind.PAGES:
return path.join(this.serverDistDir, 'pages', pathname);
case IncrementalCacheKind.IMAGE:
case IncrementalCacheKind.APP_PAGE:
case IncrementalCacheKind.APP_ROUTE:
return path.join(this.serverDistDir, 'app', pathname);
default:
throw Object.defineProperty(new Error(`Unexpected file path kind: ${kind}`), "__NEXT_ERROR_CODE", {
value: "E479",
enumerable: false,
configurable: true
});
}
}
}
//# sourceMappingURL=file-system-cache.js.map
File diff suppressed because one or more lines are too long
+474
View File
@@ -0,0 +1,474 @@
import { IncrementalCacheKind, CachedRouteKind } from '../../response-cache';
import FileSystemCache from './file-system-cache';
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path';
import { CACHE_ONE_YEAR_SECONDS, NEXT_CACHE_TAGS_HEADER, PRERENDER_REVALIDATE_HEADER } from '../../../lib/constants';
import { toRoute } from '../to-route';
import { SharedCacheControls } from './shared-cache-controls.external';
import { getPrerenderResumeDataCache, getRenderResumeDataCache, workUnitAsyncStorage } from '../../app-render/work-unit-async-storage.external';
import { InvariantError } from '../../../shared/lib/invariant-error';
import { getPreviouslyRevalidatedTags } from '../../server-utils';
import { workAsyncStorage } from '../../app-render/work-async-storage.external';
import { DetachedPromise } from '../../../lib/detached-promise';
import { areTagsExpired, areTagsStale } from './tags-manifest.external';
export class CacheHandler {
// eslint-disable-next-line
constructor(_ctx){}
async get(_cacheKey, _ctx) {
return {};
}
async set(_cacheKey, _data, _ctx) {}
async revalidateTag(_tags, _durations) {}
resetRequestCache() {}
}
export class IncrementalCache {
static #_ = this.debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE;
constructor({ fs, dev, flushToDisk, minimalMode, serverDistDir, requestHeaders, maxMemoryCacheSize, getPrerenderManifest, fetchCacheKeyPrefix, CurCacheHandler, allowedRevalidateHeaderKeys }){
var _this_prerenderManifest_preview, _this_prerenderManifest;
this.locks = new Map();
this.hasCustomCacheHandler = Boolean(CurCacheHandler);
const cacheHandlersSymbol = Symbol.for('@next/cache-handlers');
const _globalThis = globalThis;
if (!CurCacheHandler) {
// if we have a global cache handler available leverage it
const globalCacheHandler = _globalThis[cacheHandlersSymbol];
if (globalCacheHandler == null ? void 0 : globalCacheHandler.FetchCache) {
CurCacheHandler = globalCacheHandler.FetchCache;
if (IncrementalCache.debug) {
console.log('IncrementalCache: using global FetchCache cache handler');
}
} else {
if (fs && serverDistDir) {
if (IncrementalCache.debug) {
console.log('IncrementalCache: using filesystem cache handler');
}
CurCacheHandler = FileSystemCache;
}
}
} else if (IncrementalCache.debug) {
console.log('IncrementalCache: using custom cache handler', CurCacheHandler.name);
}
if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
// Allow cache size to be overridden for testing purposes
maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10);
}
this.dev = dev;
this.disableForTestmode = process.env.NEXT_PRIVATE_TEST_PROXY === 'true';
// 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;
this.requestHeaders = requestHeaders;
this.allowedRevalidateHeaderKeys = allowedRevalidateHeaderKeys;
this.prerenderManifest = getPrerenderManifest();
this.cacheControls = new SharedCacheControls(this.prerenderManifest);
this.fetchCacheKeyPrefix = fetchCacheKeyPrefix;
let revalidatedTags = [];
if (requestHeaders[PRERENDER_REVALIDATE_HEADER] === ((_this_prerenderManifest = this.prerenderManifest) == null ? void 0 : (_this_prerenderManifest_preview = _this_prerenderManifest.preview) == null ? void 0 : _this_prerenderManifest_preview.previewModeId)) {
this.isOnDemandRevalidate = true;
}
if (minimalMode) {
var _this_prerenderManifest_preview1, _this_prerenderManifest1;
revalidatedTags = this.revalidatedTags = getPreviouslyRevalidatedTags(requestHeaders, (_this_prerenderManifest1 = this.prerenderManifest) == null ? void 0 : (_this_prerenderManifest_preview1 = _this_prerenderManifest1.preview) == null ? void 0 : _this_prerenderManifest_preview1.previewModeId);
}
if (CurCacheHandler) {
this.cacheHandler = new CurCacheHandler({
dev,
fs,
flushToDisk,
serverDistDir,
revalidatedTags,
maxMemoryCacheSize,
_requestHeaders: requestHeaders,
fetchCacheKeyPrefix
});
}
}
calculateRevalidate(pathname, fromTime, dev, isFallback) {
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
if (dev) return Math.floor(performance.timeOrigin + performance.now() - 1000);
const cacheControl = this.cacheControls.get(toRoute(pathname));
// if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second unless it's a fallback request.
const initialRevalidateSeconds = cacheControl ? cacheControl.revalidate : isFallback ? false : 1;
const revalidateAfter = typeof initialRevalidateSeconds === 'number' ? initialRevalidateSeconds * 1000 + fromTime : initialRevalidateSeconds;
return revalidateAfter;
}
_getPathname(pathname, fetchCache) {
return fetchCache ? pathname : normalizePagePath(pathname);
}
resetRequestCache() {
var _this_cacheHandler_resetRequestCache, _this_cacheHandler;
(_this_cacheHandler = this.cacheHandler) == null ? void 0 : (_this_cacheHandler_resetRequestCache = _this_cacheHandler.resetRequestCache) == null ? void 0 : _this_cacheHandler_resetRequestCache.call(_this_cacheHandler);
}
async lock(cacheKey) {
// Wait for any existing lock on this cache key to be released
// This implements a simple queue-based locking mechanism
while(true){
const lock = this.locks.get(cacheKey);
if (IncrementalCache.debug) {
console.log('IncrementalCache: lock get', cacheKey, !!lock);
}
// If no lock exists, we can proceed to acquire it
if (!lock) break;
// Wait for the existing lock to be released before trying again
await lock;
}
// Create a new detached promise that will represent this lock
// The resolve function (unlock) will be returned to the caller
const { resolve, promise } = new DetachedPromise();
if (IncrementalCache.debug) {
console.log('IncrementalCache: successfully locked', cacheKey);
}
// Store the lock promise in the locks map
this.locks.set(cacheKey, promise);
return ()=>{
// Resolve the promise to release the lock.
resolve();
// Remove the lock from the map once it's released so that future gets
// can acquire the lock.
this.locks.delete(cacheKey);
};
}
async revalidateTag(tags, durations) {
var _this_cacheHandler;
return (_this_cacheHandler = this.cacheHandler) == null ? void 0 : _this_cacheHandler.revalidateTag(tags, durations);
}
// x-ref: https://github.com/facebook/react/blob/2655c9354d8e1c54ba888444220f63e836925caa/packages/react/src/ReactFetch.js#L23
async generateCacheKey(url, init = {}) {
// this should be bumped anytime a fix is made to cache entries
// that should bust the cache
const MAIN_KEY_PREFIX = 'v3';
const bodyChunks = [];
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (init.body) {
// handle Uint8Array body
if (init.body instanceof Uint8Array) {
bodyChunks.push(decoder.decode(init.body));
init._ogBody = init.body;
} else if (typeof init.body.getReader === 'function') {
const readableBody = init.body;
const chunks = [];
try {
await readableBody.pipeTo(new WritableStream({
write (chunk) {
if (typeof chunk === 'string') {
chunks.push(encoder.encode(chunk));
bodyChunks.push(chunk);
} else {
chunks.push(chunk);
bodyChunks.push(decoder.decode(chunk, {
stream: true
}));
}
}
}));
// Flush the decoder.
bodyChunks.push(decoder.decode());
// Create a new buffer with all the chunks.
const length = chunks.reduce((total, arr)=>total + arr.length, 0);
const arrayBuffer = new Uint8Array(length);
// Push each of the chunks into the new array buffer.
let offset = 0;
for (const chunk of chunks){
arrayBuffer.set(chunk, offset);
offset += chunk.length;
}
;
init._ogBody = arrayBuffer;
} catch (err) {
console.error('Problem reading body', err);
}
} else if (typeof init.body.keys === 'function') {
const formData = init.body;
init._ogBody = init.body;
for (const key of new Set([
...formData.keys()
])){
const values = formData.getAll(key);
bodyChunks.push(`${key}=${(await Promise.all(values.map(async (val)=>{
if (typeof val === 'string') {
return val;
} else {
return await val.text();
}
}))).join(',')}`);
}
// handle blob body
} else if (typeof init.body.arrayBuffer === 'function') {
const blob = init.body;
const arrayBuffer = await blob.arrayBuffer();
bodyChunks.push(await blob.text());
init._ogBody = new Blob([
arrayBuffer
], {
type: blob.type
});
} else if (typeof init.body === 'string') {
bodyChunks.push(init.body);
init._ogBody = init.body;
}
}
const headers = typeof (init.headers || {}).keys === 'function' ? Object.fromEntries(init.headers) : Object.assign({}, init.headers);
// w3c trace context headers can break request caching and deduplication
// so we remove them from the cache key
if ('traceparent' in headers) delete headers['traceparent'];
if ('tracestate' in headers) delete headers['tracestate'];
const cacheString = JSON.stringify([
MAIN_KEY_PREFIX,
this.fetchCacheKeyPrefix || '',
url,
init.method,
headers,
init.mode,
init.redirect,
init.credentials,
init.referrer,
init.referrerPolicy,
init.integrity,
init.cache,
bodyChunks
]);
if (process.env.NEXT_RUNTIME === 'edge') {
function bufferToHex(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), (b)=>b.toString(16).padStart(2, '0')).join('');
}
const buffer = encoder.encode(cacheString);
return bufferToHex(await crypto.subtle.digest('SHA-256', buffer));
} else {
const crypto1 = require('crypto');
return crypto1.createHash('sha256').update(cacheString).digest('hex');
}
}
async get(cacheKey, ctx) {
var _this_cacheHandler, _cacheData_value;
// Unlike other caches if we have a resume data cache, we use it even if
// testmode would normally disable it or if requestHeaders say 'no-cache'.
if (ctx.kind === IncrementalCacheKind.FETCH) {
const workUnitStore = workUnitAsyncStorage.getStore();
const resumeDataCache = workUnitStore ? getRenderResumeDataCache(workUnitStore) : null;
if (resumeDataCache) {
const memoryCacheData = resumeDataCache.fetch.get(cacheKey);
if ((memoryCacheData == null ? void 0 : memoryCacheData.kind) === CachedRouteKind.FETCH) {
// Check if any tags were recently revalidated before returning RDC entry.
// When a server action calls updateTag(), the re-render should see fresh
// data instead of stale RDC data.
const workStore = workAsyncStorage.getStore();
const combinedTags = [
...ctx.tags || [],
...ctx.softTags || []
];
const hasRevalidatedTag = combinedTags.some((tag)=>{
var _this_revalidatedTags, _workStore_pendingRevalidatedTags;
return ((_this_revalidatedTags = this.revalidatedTags) == null ? void 0 : _this_revalidatedTags.includes(tag)) || (workStore == null ? void 0 : (_workStore_pendingRevalidatedTags = workStore.pendingRevalidatedTags) == null ? void 0 : _workStore_pendingRevalidatedTags.some((item)=>item.tag === tag));
});
if (hasRevalidatedTag) {
if (IncrementalCache.debug) {
console.log('IncrementalCache: rdc:revalidated-tag', cacheKey);
}
// Fall through to cacheHandler lookup
} else {
if (IncrementalCache.debug) {
console.log('IncrementalCache: rdc:hit', cacheKey);
}
return {
isStale: false,
value: memoryCacheData
};
}
} else if (IncrementalCache.debug) {
console.log('IncrementalCache: rdc:miss', cacheKey);
}
} else {
if (IncrementalCache.debug) {
console.log('IncrementalCache: rdc:no-resume-data');
}
}
}
// we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging
if (this.disableForTestmode || this.dev && (ctx.kind !== IncrementalCacheKind.FETCH || this.requestHeaders['cache-control'] === 'no-cache')) {
return null;
}
cacheKey = this._getPathname(cacheKey, ctx.kind === IncrementalCacheKind.FETCH);
const cacheData = await ((_this_cacheHandler = this.cacheHandler) == null ? void 0 : _this_cacheHandler.get(cacheKey, ctx));
if (ctx.kind === IncrementalCacheKind.FETCH) {
var _cacheData_value1;
if (!cacheData) {
return null;
}
if (((_cacheData_value1 = cacheData.value) == null ? void 0 : _cacheData_value1.kind) !== CachedRouteKind.FETCH) {
var _cacheData_value2;
throw Object.defineProperty(new InvariantError(`Expected cached value for cache key ${JSON.stringify(cacheKey)} to be a "FETCH" kind, got ${JSON.stringify((_cacheData_value2 = cacheData.value) == null ? void 0 : _cacheData_value2.kind)} instead.`), "__NEXT_ERROR_CODE", {
value: "E653",
enumerable: false,
configurable: true
});
}
const workStore = workAsyncStorage.getStore();
const combinedTags = [
...ctx.tags || [],
...ctx.softTags || []
];
// if a tag was revalidated we don't return stale data
if (combinedTags.some((tag)=>{
var _this_revalidatedTags, _workStore_pendingRevalidatedTags;
return ((_this_revalidatedTags = this.revalidatedTags) == null ? void 0 : _this_revalidatedTags.includes(tag)) || (workStore == null ? void 0 : (_workStore_pendingRevalidatedTags = workStore.pendingRevalidatedTags) == null ? void 0 : _workStore_pendingRevalidatedTags.some((item)=>item.tag === tag));
})) {
if (IncrementalCache.debug) {
console.log('IncrementalCache: expired tag', cacheKey);
}
return null;
}
// As we're able to get the cache entry for this fetch, and the prerender
// resume data cache (RDC) is available, it must have been populated by a
// previous fetch, but was not yet present in the in-memory cache. This
// could be the case when performing multiple renders in parallel during
// build time where we de-duplicate the fetch calls.
//
// We add it to the RDC so that the next fetch call will be able to use it
// and it won't have to reach into the fetch cache implementation.
const workUnitStore = workUnitAsyncStorage.getStore();
if (workUnitStore) {
const prerenderResumeDataCache = getPrerenderResumeDataCache(workUnitStore);
if (prerenderResumeDataCache) {
if (IncrementalCache.debug) {
console.log('IncrementalCache: rdc:set', cacheKey);
}
prerenderResumeDataCache.fetch.set(cacheKey, cacheData.value);
}
}
const revalidate = ctx.revalidate || cacheData.value.revalidate;
const age = (performance.timeOrigin + performance.now() - (cacheData.lastModified || 0)) / 1000;
let isStale = age > revalidate;
const data = cacheData.value.data;
if (areTagsExpired(combinedTags, cacheData.lastModified)) {
return null;
} else if (areTagsStale(combinedTags, cacheData.lastModified)) {
isStale = true;
}
return {
isStale,
value: {
kind: CachedRouteKind.FETCH,
data,
revalidate
}
};
} else if ((cacheData == null ? void 0 : (_cacheData_value = cacheData.value) == null ? void 0 : _cacheData_value.kind) === CachedRouteKind.FETCH) {
throw Object.defineProperty(new InvariantError(`Expected cached value for cache key ${JSON.stringify(cacheKey)} not to be a ${JSON.stringify(ctx.kind)} kind, got "FETCH" instead.`), "__NEXT_ERROR_CODE", {
value: "E652",
enumerable: false,
configurable: true
});
}
let entry = null;
const { isFallback } = ctx;
const cacheControl = this.cacheControls.get(toRoute(cacheKey));
let isStale;
let revalidateAfter;
if ((cacheData == null ? void 0 : cacheData.lastModified) === -1) {
isStale = -1;
revalidateAfter = -1 * CACHE_ONE_YEAR_SECONDS * 1000;
} else {
var _cacheData_value3, _cacheData_value4;
const now = performance.timeOrigin + performance.now();
const lastModified = (cacheData == null ? void 0 : cacheData.lastModified) || now;
revalidateAfter = this.calculateRevalidate(cacheKey, lastModified, this.dev ?? false, ctx.isFallback);
isStale = revalidateAfter !== false && revalidateAfter < now ? true : undefined;
// If the stale time couldn't be determined based on the revalidation
// time, we check if the tags are expired or stale.
if (isStale === undefined && ((cacheData == null ? void 0 : (_cacheData_value3 = cacheData.value) == null ? void 0 : _cacheData_value3.kind) === CachedRouteKind.APP_PAGE || (cacheData == null ? void 0 : (_cacheData_value4 = cacheData.value) == null ? void 0 : _cacheData_value4.kind) === CachedRouteKind.APP_ROUTE)) {
var _cacheData_value_headers;
const tagsHeader = (_cacheData_value_headers = cacheData.value.headers) == null ? void 0 : _cacheData_value_headers[NEXT_CACHE_TAGS_HEADER];
if (typeof tagsHeader === 'string') {
const cacheTags = tagsHeader.split(',');
if (cacheTags.length > 0) {
if (areTagsExpired(cacheTags, lastModified)) {
isStale = -1;
} else if (areTagsStale(cacheTags, lastModified)) {
isStale = true;
}
}
}
}
}
if (cacheData) {
entry = {
isStale,
cacheControl,
revalidateAfter,
value: cacheData.value,
isFallback
};
}
if (!cacheData && this.prerenderManifest.notFoundRoutes.includes(cacheKey)) {
// for the first hit after starting the server the cache
// may not have a way to save notFound: true so if
// the prerender-manifest marks this as notFound then we
// return that entry and trigger a cache set to give it a
// chance to update in-memory entries
entry = {
isStale,
value: null,
cacheControl,
revalidateAfter,
isFallback
};
this.set(cacheKey, entry.value, {
...ctx,
cacheControl
});
}
return entry;
}
async set(pathname, data, ctx) {
// Even if we otherwise disable caching for testMode or if no fetchCache is
// configured we still always stash results in the resume data cache if one
// exists. This is because this is a transient in memory cache that
// populates caches ahead of a dynamic render in dev mode to allow the RSC
// debug info to have the right environment associated to it.
if ((data == null ? void 0 : data.kind) === CachedRouteKind.FETCH) {
const workUnitStore = workUnitAsyncStorage.getStore();
const prerenderResumeDataCache = workUnitStore ? getPrerenderResumeDataCache(workUnitStore) : null;
if (prerenderResumeDataCache) {
if (IncrementalCache.debug) {
console.log('IncrementalCache: rdc:set', pathname);
}
prerenderResumeDataCache.fetch.set(pathname, data);
}
}
if (this.disableForTestmode || this.dev && !ctx.fetchCache) return;
pathname = this._getPathname(pathname, ctx.fetchCache);
// FetchCache has upper limit of 2MB per-entry currently
const itemSize = JSON.stringify(data).length;
if (ctx.fetchCache && itemSize > 2 * 1024 * 1024 && // We ignore the size limit when custom cache handler is being used, as it
// might not have this limit
!this.hasCustomCacheHandler && // We also ignore the size limit when it's an implicit build-time-only
// caching that the user isn't even aware of.
!ctx.isImplicitBuildTimeCache) {
const warningText = `Failed to set Next.js data cache for ${ctx.fetchUrl || pathname}, items over 2MB can not be cached (${itemSize} bytes)`;
if (this.dev) {
throw Object.defineProperty(new Error(warningText), "__NEXT_ERROR_CODE", {
value: "E1003",
enumerable: false,
configurable: true
});
}
console.warn(warningText);
return;
}
try {
var _this_cacheHandler;
if (!ctx.fetchCache && ctx.cacheControl) {
this.cacheControls.set(toRoute(pathname), ctx.cacheControl);
}
await ((_this_cacheHandler = this.cacheHandler) == null ? void 0 : _this_cacheHandler.set(pathname, data, ctx));
} catch (error) {
console.warn('Failed to update prerender cache for', pathname, error);
}
}
}
//# sourceMappingURL=index.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,47 @@
import { CachedRouteKind } from '../../response-cache/types';
import { LRUCache } from '../lru-cache';
let memoryCache;
function getBufferSize(buffer) {
return (buffer == null ? void 0 : buffer.length) || 0;
}
function getSegmentDataSize(segmentData) {
if (!segmentData) {
return 0;
}
let size = 0;
for (const [segmentPath, buffer] of segmentData){
size += segmentPath.length + getBufferSize(buffer);
}
return size;
}
export function getMemoryCache(maxMemoryCacheSize) {
if (!memoryCache) {
memoryCache = new LRUCache(maxMemoryCacheSize, function length({ value }) {
var _JSON_stringify;
if (!value) {
return 25;
} else if (value.kind === CachedRouteKind.REDIRECT) {
return JSON.stringify(value.props).length;
} else if (value.kind === CachedRouteKind.IMAGE) {
throw Object.defineProperty(new Error('invariant image should not be incremental-cache'), "__NEXT_ERROR_CODE", {
value: "E501",
enumerable: false,
configurable: true
});
} else if (value.kind === CachedRouteKind.FETCH) {
return JSON.stringify(value.data || '').length;
} else if (value.kind === CachedRouteKind.APP_ROUTE) {
return value.body.length;
}
// rough estimate of size of cache value
if (value.kind === CachedRouteKind.APP_PAGE) {
var _value_postponed;
return Math.max(1, value.html.length + getBufferSize(value.rscData) + (((_value_postponed = value.postponed) == null ? void 0 : _value_postponed.length) || 0) + getSegmentDataSize(value.segmentData));
}
return value.html.length + (((_JSON_stringify = JSON.stringify(value.pageData)) == null ? void 0 : _JSON_stringify.length) || 0);
});
}
return memoryCache;
}
//# sourceMappingURL=memory-cache.external.js.map
@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/server/lib/incremental-cache/memory-cache.external.ts"],"sourcesContent":["import type { CacheHandlerValue } from '.'\nimport { CachedRouteKind } from '../../response-cache/types'\nimport { LRUCache } from '../lru-cache'\n\nlet memoryCache: LRUCache<CacheHandlerValue> | undefined\n\nfunction getBufferSize(buffer: Buffer | undefined) {\n return buffer?.length || 0\n}\n\nfunction getSegmentDataSize(segmentData: Map<string, Buffer> | undefined) {\n if (!segmentData) {\n return 0\n }\n\n let size = 0\n\n for (const [segmentPath, buffer] of segmentData) {\n size += segmentPath.length + getBufferSize(buffer)\n }\n\n return size\n}\n\nexport function getMemoryCache(maxMemoryCacheSize: number) {\n if (!memoryCache) {\n memoryCache = new LRUCache(maxMemoryCacheSize, function length({ value }) {\n if (!value) {\n return 25\n } else if (value.kind === CachedRouteKind.REDIRECT) {\n return JSON.stringify(value.props).length\n } else if (value.kind === CachedRouteKind.IMAGE) {\n throw new Error('invariant image should not be incremental-cache')\n } else if (value.kind === CachedRouteKind.FETCH) {\n return JSON.stringify(value.data || '').length\n } else if (value.kind === CachedRouteKind.APP_ROUTE) {\n return value.body.length\n }\n // rough estimate of size of cache value\n if (value.kind === CachedRouteKind.APP_PAGE) {\n return Math.max(\n 1,\n value.html.length +\n getBufferSize(value.rscData) +\n (value.postponed?.length || 0) +\n getSegmentDataSize(value.segmentData)\n )\n }\n\n return value.html.length + (JSON.stringify(value.pageData)?.length || 0)\n })\n }\n\n return memoryCache\n}\n"],"names":["CachedRouteKind","LRUCache","memoryCache","getBufferSize","buffer","length","getSegmentDataSize","segmentData","size","segmentPath","getMemoryCache","maxMemoryCacheSize","value","JSON","kind","REDIRECT","stringify","props","IMAGE","Error","FETCH","data","APP_ROUTE","body","APP_PAGE","Math","max","html","rscData","postponed","pageData"],"mappings":"AACA,SAASA,eAAe,QAAQ,6BAA4B;AAC5D,SAASC,QAAQ,QAAQ,eAAc;AAEvC,IAAIC;AAEJ,SAASC,cAAcC,MAA0B;IAC/C,OAAOA,CAAAA,0BAAAA,OAAQC,MAAM,KAAI;AAC3B;AAEA,SAASC,mBAAmBC,WAA4C;IACtE,IAAI,CAACA,aAAa;QAChB,OAAO;IACT;IAEA,IAAIC,OAAO;IAEX,KAAK,MAAM,CAACC,aAAaL,OAAO,IAAIG,YAAa;QAC/CC,QAAQC,YAAYJ,MAAM,GAAGF,cAAcC;IAC7C;IAEA,OAAOI;AACT;AAEA,OAAO,SAASE,eAAeC,kBAA0B;IACvD,IAAI,CAACT,aAAa;QAChBA,cAAc,IAAID,SAASU,oBAAoB,SAASN,OAAO,EAAEO,KAAK,EAAE;gBAuB1CC;YAtB5B,IAAI,CAACD,OAAO;gBACV,OAAO;YACT,OAAO,IAAIA,MAAME,IAAI,KAAKd,gBAAgBe,QAAQ,EAAE;gBAClD,OAAOF,KAAKG,SAAS,CAACJ,MAAMK,KAAK,EAAEZ,MAAM;YAC3C,OAAO,IAAIO,MAAME,IAAI,KAAKd,gBAAgBkB,KAAK,EAAE;gBAC/C,MAAM,qBAA4D,CAA5D,IAAIC,MAAM,oDAAV,qBAAA;2BAAA;gCAAA;kCAAA;gBAA2D;YACnE,OAAO,IAAIP,MAAME,IAAI,KAAKd,gBAAgBoB,KAAK,EAAE;gBAC/C,OAAOP,KAAKG,SAAS,CAACJ,MAAMS,IAAI,IAAI,IAAIhB,MAAM;YAChD,OAAO,IAAIO,MAAME,IAAI,KAAKd,gBAAgBsB,SAAS,EAAE;gBACnD,OAAOV,MAAMW,IAAI,CAAClB,MAAM;YAC1B;YACA,wCAAwC;YACxC,IAAIO,MAAME,IAAI,KAAKd,gBAAgBwB,QAAQ,EAAE;oBAKtCZ;gBAJL,OAAOa,KAAKC,GAAG,CACb,GACAd,MAAMe,IAAI,CAACtB,MAAM,GACfF,cAAcS,MAAMgB,OAAO,IAC1BhB,CAAAA,EAAAA,mBAAAA,MAAMiB,SAAS,qBAAfjB,iBAAiBP,MAAM,KAAI,CAAA,IAC5BC,mBAAmBM,MAAML,WAAW;YAE1C;YAEA,OAAOK,MAAMe,IAAI,CAACtB,MAAM,GAAIQ,CAAAA,EAAAA,kBAAAA,KAAKG,SAAS,CAACJ,MAAMkB,QAAQ,sBAA7BjB,gBAAgCR,MAAM,KAAI,CAAA;QACxE;IACF;IAEA,OAAOH;AACT","ignoreList":[0]}
@@ -0,0 +1,67 @@
/**
* A shared cache of cache controls for routes. This cache is used so we don't
* have to modify the prerender manifest when we want to update the cache
* control for a route.
*/ export class SharedCacheControls {
static #_ = /**
* The in-memory cache of cache lives for routes. This cache is populated when
* the cache is updated with new cache lives.
*/ this.cacheControls = new Map();
constructor(/**
* The prerender manifest that contains the initial cache controls for
* routes.
*/ prerenderManifest){
this.prerenderManifest = prerenderManifest;
}
/**
* Try to get the cache control value for a route. This will first try to get
* the value from the in-memory cache. If the value is not present in the
* in-memory cache, it will be sourced from the prerender manifest.
*
* @param route the route to get the cache control for
* @returns the cache control for the route, or undefined if the values
* are not present in the in-memory cache or the prerender manifest
*/ get(route) {
// This is a copy on write cache that is updated when the cache is updated.
// If the cache is never written to, then the values will be sourced from
// the prerender manifest.
let cacheControl = SharedCacheControls.cacheControls.get(route);
if (cacheControl) return cacheControl;
let prerenderData = this.prerenderManifest.routes[route];
if (prerenderData) {
const { initialRevalidateSeconds, initialExpireSeconds } = prerenderData;
if (typeof initialRevalidateSeconds !== 'undefined') {
return {
revalidate: initialRevalidateSeconds,
expire: initialExpireSeconds
};
}
}
const dynamicPrerenderData = this.prerenderManifest.dynamicRoutes[route];
if (dynamicPrerenderData) {
const { fallbackRevalidate, fallbackExpire } = dynamicPrerenderData;
if (typeof fallbackRevalidate !== 'undefined') {
return {
revalidate: fallbackRevalidate,
expire: fallbackExpire
};
}
}
return undefined;
}
/**
* Set the cache control for a route.
*
* @param route the route to set the cache control for
* @param cacheControl the cache control for the route
*/ set(route, cacheControl) {
SharedCacheControls.cacheControls.set(route, cacheControl);
}
/**
* Clear the in-memory cache of cache controls for routes.
*/ clear() {
SharedCacheControls.cacheControls.clear();
}
}
//# sourceMappingURL=shared-cache-controls.external.js.map
@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/server/lib/incremental-cache/shared-cache-controls.external.ts"],"sourcesContent":["import type { PrerenderManifest } from '../../../build'\nimport type { DeepReadonly } from '../../../shared/lib/deep-readonly'\nimport type { CacheControl } from '../cache-control'\n\n/**\n * A shared cache of cache controls for routes. This cache is used so we don't\n * have to modify the prerender manifest when we want to update the cache\n * control for a route.\n */\nexport class SharedCacheControls {\n /**\n * The in-memory cache of cache lives for routes. This cache is populated when\n * the cache is updated with new cache lives.\n */\n private static readonly cacheControls = new Map<string, CacheControl>()\n\n constructor(\n /**\n * The prerender manifest that contains the initial cache controls for\n * routes.\n */\n private readonly prerenderManifest: DeepReadonly<\n Pick<PrerenderManifest, 'routes' | 'dynamicRoutes'>\n >\n ) {}\n\n /**\n * Try to get the cache control value for a route. This will first try to get\n * the value from the in-memory cache. If the value is not present in the\n * in-memory cache, it will be sourced from the prerender manifest.\n *\n * @param route the route to get the cache control for\n * @returns the cache control for the route, or undefined if the values\n * are not present in the in-memory cache or the prerender manifest\n */\n public get(route: string): CacheControl | undefined {\n // This is a copy on write cache that is updated when the cache is updated.\n // If the cache is never written to, then the values will be sourced from\n // the prerender manifest.\n let cacheControl = SharedCacheControls.cacheControls.get(route)\n if (cacheControl) return cacheControl\n\n let prerenderData = this.prerenderManifest.routes[route]\n\n if (prerenderData) {\n const { initialRevalidateSeconds, initialExpireSeconds } = prerenderData\n\n if (typeof initialRevalidateSeconds !== 'undefined') {\n return {\n revalidate: initialRevalidateSeconds,\n expire: initialExpireSeconds,\n }\n }\n }\n\n const dynamicPrerenderData = this.prerenderManifest.dynamicRoutes[route]\n\n if (dynamicPrerenderData) {\n const { fallbackRevalidate, fallbackExpire } = dynamicPrerenderData\n\n if (typeof fallbackRevalidate !== 'undefined') {\n return { revalidate: fallbackRevalidate, expire: fallbackExpire }\n }\n }\n\n return undefined\n }\n\n /**\n * Set the cache control for a route.\n *\n * @param route the route to set the cache control for\n * @param cacheControl the cache control for the route\n */\n public set(route: string, cacheControl: CacheControl) {\n SharedCacheControls.cacheControls.set(route, cacheControl)\n }\n\n /**\n * Clear the in-memory cache of cache controls for routes.\n */\n public clear() {\n SharedCacheControls.cacheControls.clear()\n }\n}\n"],"names":["SharedCacheControls","cacheControls","Map","constructor","prerenderManifest","get","route","cacheControl","prerenderData","routes","initialRevalidateSeconds","initialExpireSeconds","revalidate","expire","dynamicPrerenderData","dynamicRoutes","fallbackRevalidate","fallbackExpire","undefined","set","clear"],"mappings":"AAIA;;;;CAIC,GACD,OAAO,MAAMA;gBACX;;;GAGC,QACuBC,gBAAgB,IAAIC;IAE5CC,YACE;;;KAGC,GACD,AAAiBC,iBAEhB,CACD;aAHiBA,oBAAAA;IAGhB;IAEH;;;;;;;;GAQC,GACD,AAAOC,IAAIC,KAAa,EAA4B;QAClD,2EAA2E;QAC3E,yEAAyE;QACzE,0BAA0B;QAC1B,IAAIC,eAAeP,oBAAoBC,aAAa,CAACI,GAAG,CAACC;QACzD,IAAIC,cAAc,OAAOA;QAEzB,IAAIC,gBAAgB,IAAI,CAACJ,iBAAiB,CAACK,MAAM,CAACH,MAAM;QAExD,IAAIE,eAAe;YACjB,MAAM,EAAEE,wBAAwB,EAAEC,oBAAoB,EAAE,GAAGH;YAE3D,IAAI,OAAOE,6BAA6B,aAAa;gBACnD,OAAO;oBACLE,YAAYF;oBACZG,QAAQF;gBACV;YACF;QACF;QAEA,MAAMG,uBAAuB,IAAI,CAACV,iBAAiB,CAACW,aAAa,CAACT,MAAM;QAExE,IAAIQ,sBAAsB;YACxB,MAAM,EAAEE,kBAAkB,EAAEC,cAAc,EAAE,GAAGH;YAE/C,IAAI,OAAOE,uBAAuB,aAAa;gBAC7C,OAAO;oBAAEJ,YAAYI;oBAAoBH,QAAQI;gBAAe;YAClE;QACF;QAEA,OAAOC;IACT;IAEA;;;;;GAKC,GACD,AAAOC,IAAIb,KAAa,EAAEC,YAA0B,EAAE;QACpDP,oBAAoBC,aAAa,CAACkB,GAAG,CAACb,OAAOC;IAC/C;IAEA;;GAEC,GACD,AAAOa,QAAQ;QACbpB,oBAAoBC,aAAa,CAACmB,KAAK;IACzC;AACF","ignoreList":[0]}
@@ -0,0 +1,31 @@
// We share the tags manifest between the "use cache" handlers and the previous
// file-system cache.
export const tagsManifest = new Map();
export const areTagsExpired = (tags, timestamp)=>{
for (const tag of tags){
const entry = tagsManifest.get(tag);
const expiredAt = entry == null ? void 0 : entry.expired;
if (typeof expiredAt === 'number') {
const now = Date.now();
// For immediate expiration (expiredAt <= now) and tag was invalidated after entry was created
// OR for future expiration that has now passed (expiredAt > timestamp && expiredAt <= now)
const isImmediatelyExpired = expiredAt <= now && expiredAt > timestamp;
if (isImmediatelyExpired) {
return true;
}
}
}
return false;
};
export const areTagsStale = (tags, timestamp)=>{
for (const tag of tags){
const entry = tagsManifest.get(tag);
const staleAt = (entry == null ? void 0 : entry.stale) ?? 0;
if (typeof staleAt === 'number' && staleAt > timestamp) {
return true;
}
}
return false;
};
//# sourceMappingURL=tags-manifest.external.js.map
@@ -0,0 +1 @@
{"version":3,"sources":["../../../../../src/server/lib/incremental-cache/tags-manifest.external.ts"],"sourcesContent":["import type { Timestamp } from '../cache-handlers/types'\n\nexport interface TagManifestEntry {\n stale?: number\n expired?: number\n}\n\n// We share the tags manifest between the \"use cache\" handlers and the previous\n// file-system cache.\nexport const tagsManifest = new Map<string, TagManifestEntry>()\n\nexport const areTagsExpired = (tags: string[], timestamp: Timestamp) => {\n for (const tag of tags) {\n const entry = tagsManifest.get(tag)\n const expiredAt = entry?.expired\n\n if (typeof expiredAt === 'number') {\n const now = Date.now()\n // For immediate expiration (expiredAt <= now) and tag was invalidated after entry was created\n // OR for future expiration that has now passed (expiredAt > timestamp && expiredAt <= now)\n const isImmediatelyExpired = expiredAt <= now && expiredAt > timestamp\n\n if (isImmediatelyExpired) {\n return true\n }\n }\n }\n\n return false\n}\n\nexport const areTagsStale = (tags: string[], timestamp: Timestamp) => {\n for (const tag of tags) {\n const entry = tagsManifest.get(tag)\n const staleAt = entry?.stale ?? 0\n\n if (typeof staleAt === 'number' && staleAt > timestamp) {\n return true\n }\n }\n\n return false\n}\n"],"names":["tagsManifest","Map","areTagsExpired","tags","timestamp","tag","entry","get","expiredAt","expired","now","Date","isImmediatelyExpired","areTagsStale","staleAt","stale"],"mappings":"AAOA,+EAA+E;AAC/E,qBAAqB;AACrB,OAAO,MAAMA,eAAe,IAAIC,MAA+B;AAE/D,OAAO,MAAMC,iBAAiB,CAACC,MAAgBC;IAC7C,KAAK,MAAMC,OAAOF,KAAM;QACtB,MAAMG,QAAQN,aAAaO,GAAG,CAACF;QAC/B,MAAMG,YAAYF,yBAAAA,MAAOG,OAAO;QAEhC,IAAI,OAAOD,cAAc,UAAU;YACjC,MAAME,MAAMC,KAAKD,GAAG;YACpB,8FAA8F;YAC9F,2FAA2F;YAC3F,MAAME,uBAAuBJ,aAAaE,OAAOF,YAAYJ;YAE7D,IAAIQ,sBAAsB;gBACxB,OAAO;YACT;QACF;IACF;IAEA,OAAO;AACT,EAAC;AAED,OAAO,MAAMC,eAAe,CAACV,MAAgBC;IAC3C,KAAK,MAAMC,OAAOF,KAAM;QACtB,MAAMG,QAAQN,aAAaO,GAAG,CAACF;QAC/B,MAAMS,UAAUR,CAAAA,yBAAAA,MAAOS,KAAK,KAAI;QAEhC,IAAI,OAAOD,YAAY,YAAYA,UAAUV,WAAW;YACtD,OAAO;QACT;IACF;IAEA,OAAO;AACT,EAAC","ignoreList":[0]}