feat(blog): add file-based blog with dynamic slugs, MDX content and layout shell

- Introduced blog routing using Next.js App Router
- Implemented dynamic [slug] pages for blog posts
- Added MDX-based content loading via lib/posts
- Integrated shared TopBar layout with navigation
- Established clear content, lib and component separation
This commit is contained in:
PascalSchattenburg
2026-01-22 14:14:15 +01:00
parent b717952234
commit d147843c76
10412 changed files with 2475583 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from '.';
import type { CacheFs } from '../../../shared/lib/utils';
import { type IncrementalCacheValue, type SetIncrementalFetchCacheContext, type SetIncrementalResponseCacheContext } from '../../response-cache';
type FileSystemCacheContext = Omit<CacheHandlerContext, 'fs' | 'serverDistDir'> & {
fs: CacheFs;
serverDistDir: string;
};
export default class FileSystemCache implements CacheHandler {
private fs;
private flushToDisk?;
private serverDistDir;
private revalidatedTags;
private static debug;
private static memoryCache;
constructor(ctx: FileSystemCacheContext);
resetRequestCache(): void;
revalidateTag(tags: string | string[], durations?: {
expire?: number;
}): Promise<void>;
get(...args: Parameters<CacheHandler['get']>): Promise<CacheHandlerValue | null>;
set(key: string, data: IncrementalCacheValue | null, ctx: SetIncrementalFetchCacheContext | SetIncrementalResponseCacheContext): Promise<void>;
private getFilePath;
}
export {};

View File

@@ -0,0 +1,333 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return FileSystemCache;
}
});
const _responsecache = require("../../response-cache");
const _path = /*#__PURE__*/ _interop_require_default(require("../../../shared/lib/isomorphic/path"));
const _constants = require("../../../lib/constants");
const _tagsmanifestexternal = require("./tags-manifest.external");
const _multifilewriter = require("../../../lib/multi-file-writer");
const _memorycacheexternal = require("./memory-cache.external");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
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 = (0, _memorycacheexternal.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 = _tagsmanifestexternal.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
;
}
_tagsmanifestexternal.tagsManifest.set(tag, updates);
} else {
// Update expired field for immediate expiration (default behavior when no durations provided)
_tagsmanifestexternal.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 === _responsecache.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 === _responsecache.IncrementalCacheKind.APP_ROUTE) {
const filePath = this.getFilePath(`${key}.body`, _responsecache.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$/, _constants.NEXT_META_SUFFIX), 'utf8'));
data = {
lastModified: mtime.getTime(),
value: {
kind: _responsecache.CachedRouteKind.APP_ROUTE,
body: fileData,
headers: meta.headers,
status: meta.status
}
};
} else {
const filePath = this.getFilePath(kind === _responsecache.IncrementalCacheKind.FETCH ? key : `${key}.html`, kind);
const fileData = await this.fs.readFile(filePath, 'utf8');
const { mtime } = await this.fs.stat(filePath);
if (kind === _responsecache.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) === _responsecache.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 === _responsecache.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$/, _constants.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 + _constants.RSC_SEGMENTS_DIR_SUFFIX;
await Promise.all(meta.segmentPaths.map(async (segmentPath)=>{
const segmentDataFilePath = this.getFilePath(segmentsDir + segmentPath + _constants.RSC_SEGMENT_SUFFIX, _responsecache.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) {
rscData = await this.fs.readFile(this.getFilePath(`${key}${_constants.RSC_SUFFIX}`, _responsecache.IncrementalCacheKind.APP_PAGE));
}
data = {
lastModified: mtime.getTime(),
value: {
kind: _responsecache.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 === _responsecache.IncrementalCacheKind.PAGES) {
let meta;
let pageData = {};
if (!ctx.isFallback) {
pageData = JSON.parse(await this.fs.readFile(this.getFilePath(`${key}${_constants.NEXT_DATA_SUFFIX}`, _responsecache.IncrementalCacheKind.PAGES), 'utf8'));
}
data = {
lastModified: mtime.getTime(),
value: {
kind: _responsecache.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) === _responsecache.CachedRouteKind.APP_PAGE || (data == null ? void 0 : (_data_value1 = data.value) == null ? void 0 : _data_value1.kind) === _responsecache.CachedRouteKind.APP_ROUTE || (data == null ? void 0 : (_data_value2 = data.value) == null ? void 0 : _data_value2.kind) === _responsecache.CachedRouteKind.PAGES) {
var _data_value_headers;
const tagsHeader = (_data_value_headers = data.value.headers) == null ? void 0 : _data_value_headers[_constants.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 && (0, _tagsmanifestexternal.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) === _responsecache.CachedRouteKind.FETCH) {
const combinedTags = ctx.kind === _responsecache.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 ((0, _tagsmanifestexternal.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.MultiFileWriter(this.fs);
if (data.kind === _responsecache.CachedRouteKind.APP_ROUTE) {
const filePath = this.getFilePath(`${key}.body`, _responsecache.IncrementalCacheKind.APP_ROUTE);
writer.append(filePath, data.body);
const meta = {
headers: data.headers,
status: data.status,
postponed: undefined,
segmentPaths: undefined
};
writer.append(filePath.replace(/\.body$/, _constants.NEXT_META_SUFFIX), JSON.stringify(meta, null, 2));
} else if (data.kind === _responsecache.CachedRouteKind.PAGES || data.kind === _responsecache.CachedRouteKind.APP_PAGE) {
const isAppPath = data.kind === _responsecache.CachedRouteKind.APP_PAGE;
const htmlPath = this.getFilePath(`${key}.html`, isAppPath ? _responsecache.IncrementalCacheKind.APP_PAGE : _responsecache.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 ? _constants.RSC_SUFFIX : _constants.NEXT_DATA_SUFFIX}`, isAppPath ? _responsecache.IncrementalCacheKind.APP_PAGE : _responsecache.IncrementalCacheKind.PAGES), isAppPath ? data.rscData : JSON.stringify(data.pageData));
}
if ((data == null ? void 0 : data.kind) === _responsecache.CachedRouteKind.APP_PAGE) {
let segmentPaths;
if (data.segmentData) {
segmentPaths = [];
const segmentsDir = htmlPath.replace(/\.html$/, _constants.RSC_SEGMENTS_DIR_SUFFIX);
for (const [segmentPath, buffer] of data.segmentData){
segmentPaths.push(segmentPath);
const segmentDataFilePath = segmentsDir + segmentPath + _constants.RSC_SEGMENT_SUFFIX;
writer.append(segmentDataFilePath, buffer);
}
}
const meta = {
headers: data.headers,
status: data.status,
postponed: data.postponed,
segmentPaths
};
writer.append(htmlPath.replace(/\.html$/, _constants.NEXT_META_SUFFIX), JSON.stringify(meta));
}
} else if (data.kind === _responsecache.CachedRouteKind.FETCH) {
const filePath = this.getFilePath(key, _responsecache.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 _responsecache.IncrementalCacheKind.FETCH:
// we store in .next/cache/fetch-cache so it can be persisted
// across deploys
return _path.default.join(this.serverDistDir, '..', 'cache', 'fetch-cache', pathname);
case _responsecache.IncrementalCacheKind.PAGES:
return _path.default.join(this.serverDistDir, 'pages', pathname);
case _responsecache.IncrementalCacheKind.IMAGE:
case _responsecache.IncrementalCacheKind.APP_PAGE:
case _responsecache.IncrementalCacheKind.APP_ROUTE:
return _path.default.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

View File

@@ -0,0 +1,75 @@
import type { CacheFs } from '../../../shared/lib/utils';
import type { PrerenderManifest } from '../../../build';
import { type IncrementalCacheValue, type IncrementalCache as IncrementalCacheType, type IncrementalResponseCacheEntry, type IncrementalFetchCacheEntry, type GetIncrementalFetchCacheContext, type GetIncrementalResponseCacheContext, type CachedFetchValue, type SetIncrementalFetchCacheContext, type SetIncrementalResponseCacheContext } from '../../response-cache';
import type { DeepReadonly } from '../../../shared/lib/deep-readonly';
export interface CacheHandlerContext {
fs?: CacheFs;
dev?: boolean;
flushToDisk?: boolean;
serverDistDir?: string;
maxMemoryCacheSize?: number;
fetchCacheKeyPrefix?: string;
prerenderManifest?: PrerenderManifest;
revalidatedTags: string[];
_requestHeaders: IncrementalCache['requestHeaders'];
}
export interface CacheHandlerValue {
lastModified: number;
age?: number;
cacheState?: string;
value: IncrementalCacheValue | null;
}
export declare class CacheHandler {
constructor(_ctx: CacheHandlerContext);
get(_cacheKey: string, _ctx: GetIncrementalFetchCacheContext | GetIncrementalResponseCacheContext): Promise<CacheHandlerValue | null>;
set(_cacheKey: string, _data: IncrementalCacheValue | null, _ctx: SetIncrementalFetchCacheContext | SetIncrementalResponseCacheContext): Promise<void>;
revalidateTag(_tags: string | string[], _durations?: {
expire?: number;
}): Promise<void>;
resetRequestCache(): void;
}
export declare class IncrementalCache implements IncrementalCacheType {
readonly dev?: boolean;
readonly disableForTestmode?: boolean;
readonly cacheHandler?: CacheHandler;
readonly hasCustomCacheHandler: boolean;
readonly prerenderManifest: DeepReadonly<PrerenderManifest>;
readonly requestHeaders: Record<string, undefined | string | string[]>;
readonly allowedRevalidateHeaderKeys?: string[];
readonly minimalMode?: boolean;
readonly fetchCacheKeyPrefix?: string;
readonly isOnDemandRevalidate?: boolean;
readonly revalidatedTags?: readonly string[];
private static readonly debug;
private readonly locks;
/**
* The cache controls for routes. This will source the values from the
* prerender manifest until the in-memory cache is updated with new values.
*/
private readonly cacheControls;
constructor({ fs, dev, flushToDisk, minimalMode, serverDistDir, requestHeaders, maxMemoryCacheSize, getPrerenderManifest, fetchCacheKeyPrefix, CurCacheHandler, allowedRevalidateHeaderKeys, }: {
fs?: CacheFs;
dev: boolean;
minimalMode?: boolean;
serverDistDir?: string;
flushToDisk?: boolean;
allowedRevalidateHeaderKeys?: string[];
requestHeaders: IncrementalCache['requestHeaders'];
maxMemoryCacheSize?: number;
getPrerenderManifest: () => DeepReadonly<PrerenderManifest>;
fetchCacheKeyPrefix?: string;
CurCacheHandler?: typeof CacheHandler;
});
private calculateRevalidate;
_getPathname(pathname: string, fetchCache?: boolean): string;
resetRequestCache(): void;
lock(cacheKey: string): Promise<() => Promise<void> | void>;
revalidateTag(tags: string | string[], durations?: {
expire?: number;
}): Promise<void>;
generateCacheKey(url: string, init?: RequestInit | Request): Promise<string>;
get(cacheKey: string, ctx: GetIncrementalFetchCacheContext): Promise<IncrementalFetchCacheEntry | null>;
get(cacheKey: string, ctx: GetIncrementalResponseCacheContext): Promise<IncrementalResponseCacheEntry | null>;
set(pathname: string, data: CachedFetchValue | null, ctx: SetIncrementalFetchCacheContext): Promise<void>;
set(pathname: string, data: Exclude<IncrementalCacheValue, CachedFetchValue> | null, ctx: SetIncrementalResponseCacheContext): Promise<void>;
}

View File

@@ -0,0 +1,479 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
CacheHandler: null,
IncrementalCache: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
CacheHandler: function() {
return CacheHandler;
},
IncrementalCache: function() {
return IncrementalCache;
}
});
const _responsecache = require("../../response-cache");
const _filesystemcache = /*#__PURE__*/ _interop_require_default(require("./file-system-cache"));
const _normalizepagepath = require("../../../shared/lib/page-path/normalize-page-path");
const _constants = require("../../../lib/constants");
const _toroute = require("../to-route");
const _sharedcachecontrolsexternal = require("./shared-cache-controls.external");
const _workunitasyncstorageexternal = require("../../app-render/work-unit-async-storage.external");
const _invarianterror = require("../../../shared/lib/invariant-error");
const _serverutils = require("../../server-utils");
const _workasyncstorageexternal = require("../../app-render/work-async-storage.external");
const _detachedpromise = require("../../../lib/detached-promise");
const _tagsmanifestexternal = require("./tags-manifest.external");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
class CacheHandler {
// eslint-disable-next-line
constructor(_ctx){}
async get(_cacheKey, _ctx) {
return {};
}
async set(_cacheKey, _data, _ctx) {}
async revalidateTag(_tags, _durations) {}
resetRequestCache() {}
}
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.default;
}
}
} 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 _sharedcachecontrolsexternal.SharedCacheControls(this.prerenderManifest);
this.fetchCacheKeyPrefix = fetchCacheKeyPrefix;
let revalidatedTags = [];
if (requestHeaders[_constants.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 = (0, _serverutils.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((0, _toroute.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 : (0, _normalizepagepath.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.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 === _responsecache.IncrementalCacheKind.FETCH) {
const workUnitStore = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore();
const resumeDataCache = workUnitStore ? (0, _workunitasyncstorageexternal.getRenderResumeDataCache)(workUnitStore) : null;
if (resumeDataCache) {
const memoryCacheData = resumeDataCache.fetch.get(cacheKey);
if ((memoryCacheData == null ? void 0 : memoryCacheData.kind) === _responsecache.CachedRouteKind.FETCH) {
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 !== _responsecache.IncrementalCacheKind.FETCH || this.requestHeaders['cache-control'] === 'no-cache')) {
return null;
}
cacheKey = this._getPathname(cacheKey, ctx.kind === _responsecache.IncrementalCacheKind.FETCH);
const cacheData = await ((_this_cacheHandler = this.cacheHandler) == null ? void 0 : _this_cacheHandler.get(cacheKey, ctx));
if (ctx.kind === _responsecache.IncrementalCacheKind.FETCH) {
var _cacheData_value1;
if (!cacheData) {
return null;
}
if (((_cacheData_value1 = cacheData.value) == null ? void 0 : _cacheData_value1.kind) !== _responsecache.CachedRouteKind.FETCH) {
var _cacheData_value2;
throw Object.defineProperty(new _invarianterror.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 = _workasyncstorageexternal.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 = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore();
if (workUnitStore) {
const prerenderResumeDataCache = (0, _workunitasyncstorageexternal.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 ((0, _tagsmanifestexternal.areTagsExpired)(combinedTags, cacheData.lastModified)) {
return null;
} else if ((0, _tagsmanifestexternal.areTagsStale)(combinedTags, cacheData.lastModified)) {
isStale = true;
}
return {
isStale,
value: {
kind: _responsecache.CachedRouteKind.FETCH,
data,
revalidate
}
};
} else if ((cacheData == null ? void 0 : (_cacheData_value = cacheData.value) == null ? void 0 : _cacheData_value.kind) === _responsecache.CachedRouteKind.FETCH) {
throw Object.defineProperty(new _invarianterror.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 cacheControl = this.cacheControls.get((0, _toroute.toRoute)(cacheKey));
let isStale;
let revalidateAfter;
if ((cacheData == null ? void 0 : cacheData.lastModified) === -1) {
isStale = -1;
revalidateAfter = -1 * _constants.CACHE_ONE_YEAR;
} 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) === _responsecache.CachedRouteKind.APP_PAGE || (cacheData == null ? void 0 : (_cacheData_value4 = cacheData.value) == null ? void 0 : _cacheData_value4.kind) === _responsecache.CachedRouteKind.APP_ROUTE)) {
var _cacheData_value_headers;
const tagsHeader = (_cacheData_value_headers = cacheData.value.headers) == null ? void 0 : _cacheData_value_headers[_constants.NEXT_CACHE_TAGS_HEADER];
if (typeof tagsHeader === 'string') {
const cacheTags = tagsHeader.split(',');
if (cacheTags.length > 0) {
if ((0, _tagsmanifestexternal.areTagsExpired)(cacheTags, lastModified)) {
isStale = -1;
} else if ((0, _tagsmanifestexternal.areTagsStale)(cacheTags, lastModified)) {
isStale = true;
}
}
}
}
}
if (cacheData) {
entry = {
isStale,
cacheControl,
revalidateAfter,
value: cacheData.value
};
}
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
};
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) === _responsecache.CachedRouteKind.FETCH) {
const workUnitStore = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore();
const prerenderResumeDataCache = workUnitStore ? (0, _workunitasyncstorageexternal.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: "E394",
enumerable: false,
configurable: true
});
}
console.warn(warningText);
return;
}
try {
var _this_cacheHandler;
if (!ctx.fetchCache && ctx.cacheControl) {
this.cacheControls.set((0, _toroute.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

View File

@@ -0,0 +1,3 @@
import type { CacheHandlerValue } from '.';
import { LRUCache } from '../lru-cache';
export declare function getMemoryCache(maxMemoryCacheSize: number): LRUCache<CacheHandlerValue>;

View File

@@ -0,0 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "getMemoryCache", {
enumerable: true,
get: function() {
return getMemoryCache;
}
});
const _types = require("../../response-cache/types");
const _lrucache = require("../lru-cache");
let memoryCache;
function getMemoryCache(maxMemoryCacheSize) {
if (!memoryCache) {
memoryCache = new _lrucache.LRUCache(maxMemoryCacheSize, function length({ value }) {
var _JSON_stringify;
if (!value) {
return 25;
} else if (value.kind === _types.CachedRouteKind.REDIRECT) {
return JSON.stringify(value.props).length;
} else if (value.kind === _types.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 === _types.CachedRouteKind.FETCH) {
return JSON.stringify(value.data || '').length;
} else if (value.kind === _types.CachedRouteKind.APP_ROUTE) {
return value.body.length;
}
// rough estimate of size of cache value
return value.html.length + (((_JSON_stringify = JSON.stringify(value.kind === _types.CachedRouteKind.APP_PAGE ? value.rscData : value.pageData)) == null ? void 0 : _JSON_stringify.length) || 0);
});
}
return memoryCache;
}
//# sourceMappingURL=memory-cache.external.js.map

View File

@@ -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\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 return (\n value.html.length +\n (JSON.stringify(\n value.kind === CachedRouteKind.APP_PAGE\n ? value.rscData\n : value.pageData\n )?.length || 0)\n )\n })\n }\n\n return memoryCache\n}\n"],"names":["getMemoryCache","memoryCache","maxMemoryCacheSize","LRUCache","length","value","JSON","kind","CachedRouteKind","REDIRECT","stringify","props","IMAGE","Error","FETCH","data","APP_ROUTE","body","html","APP_PAGE","rscData","pageData"],"mappings":";;;;+BAMgBA;;;eAAAA;;;uBALgB;0BACP;AAEzB,IAAIC;AAEG,SAASD,eAAeE,kBAA0B;IACvD,IAAI,CAACD,aAAa;QAChBA,cAAc,IAAIE,kBAAQ,CAACD,oBAAoB,SAASE,OAAO,EAAEC,KAAK,EAAE;gBAenEC;YAdH,IAAI,CAACD,OAAO;gBACV,OAAO;YACT,OAAO,IAAIA,MAAME,IAAI,KAAKC,sBAAe,CAACC,QAAQ,EAAE;gBAClD,OAAOH,KAAKI,SAAS,CAACL,MAAMM,KAAK,EAAEP,MAAM;YAC3C,OAAO,IAAIC,MAAME,IAAI,KAAKC,sBAAe,CAACI,KAAK,EAAE;gBAC/C,MAAM,qBAA4D,CAA5D,IAAIC,MAAM,oDAAV,qBAAA;2BAAA;gCAAA;kCAAA;gBAA2D;YACnE,OAAO,IAAIR,MAAME,IAAI,KAAKC,sBAAe,CAACM,KAAK,EAAE;gBAC/C,OAAOR,KAAKI,SAAS,CAACL,MAAMU,IAAI,IAAI,IAAIX,MAAM;YAChD,OAAO,IAAIC,MAAME,IAAI,KAAKC,sBAAe,CAACQ,SAAS,EAAE;gBACnD,OAAOX,MAAMY,IAAI,CAACb,MAAM;YAC1B;YACA,wCAAwC;YACxC,OACEC,MAAMa,IAAI,CAACd,MAAM,GAChBE,CAAAA,EAAAA,kBAAAA,KAAKI,SAAS,CACbL,MAAME,IAAI,KAAKC,sBAAe,CAACW,QAAQ,GACnCd,MAAMe,OAAO,GACbf,MAAMgB,QAAQ,sBAHnBf,gBAIEF,MAAM,KAAI,CAAA;QAEjB;IACF;IAEA,OAAOH;AACT","ignoreList":[0]}

View File

@@ -0,0 +1,47 @@
import type { PrerenderManifest } from '../../../build';
import type { DeepReadonly } from '../../../shared/lib/deep-readonly';
import type { CacheControl } from '../cache-control';
/**
* 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 declare class SharedCacheControls {
/**
* The prerender manifest that contains the initial cache controls for
* routes.
*/
private readonly prerenderManifest;
/**
* The in-memory cache of cache lives for routes. This cache is populated when
* the cache is updated with new cache lives.
*/
private static readonly cacheControls;
constructor(
/**
* The prerender manifest that contains the initial cache controls for
* routes.
*/
prerenderManifest: DeepReadonly<Pick<PrerenderManifest, 'routes' | 'dynamicRoutes'>>);
/**
* 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: string): CacheControl | 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: string, cacheControl: CacheControl): void;
/**
* Clear the in-memory cache of cache controls for routes.
*/
clear(): void;
}

View File

@@ -0,0 +1,73 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "SharedCacheControls", {
enumerable: true,
get: function() {
return SharedCacheControls;
}
});
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

View File

@@ -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":";;;;+BASaA;;;eAAAA;;;AAAN,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]}

View File

@@ -0,0 +1,8 @@
import type { Timestamp } from '../cache-handlers/types';
export interface TagManifestEntry {
stale?: number;
expired?: number;
}
export declare const tagsManifest: Map<string, TagManifestEntry>;
export declare const areTagsExpired: (tags: string[], timestamp: Timestamp) => boolean;
export declare const areTagsStale: (tags: string[], timestamp: Timestamp) => boolean;

View File

@@ -0,0 +1,55 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
areTagsExpired: null,
areTagsStale: null,
tagsManifest: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
areTagsExpired: function() {
return areTagsExpired;
},
areTagsStale: function() {
return areTagsStale;
},
tagsManifest: function() {
return tagsManifest;
}
});
const tagsManifest = new Map();
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;
};
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

View File

@@ -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":["areTagsExpired","areTagsStale","tagsManifest","Map","tags","timestamp","tag","entry","get","expiredAt","expired","now","Date","isImmediatelyExpired","staleAt","stale"],"mappings":";;;;;;;;;;;;;;;;IAWaA,cAAc;eAAdA;;IAoBAC,YAAY;eAAZA;;IAtBAC,YAAY;eAAZA;;;AAAN,MAAMA,eAAe,IAAIC;AAEzB,MAAMH,iBAAiB,CAACI,MAAgBC;IAC7C,KAAK,MAAMC,OAAOF,KAAM;QACtB,MAAMG,QAAQL,aAAaM,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;AAEO,MAAMZ,eAAe,CAACG,MAAgBC;IAC3C,KAAK,MAAMC,OAAOF,KAAM;QACtB,MAAMG,QAAQL,aAAaM,GAAG,CAACF;QAC/B,MAAMQ,UAAUP,CAAAA,yBAAAA,MAAOQ,KAAK,KAAI;QAEhC,IAAI,OAAOD,YAAY,YAAYA,UAAUT,WAAW;YACtD,OAAO;QACT;IACF;IAEA,OAAO;AACT","ignoreList":[0]}