forked from caching-tools/next-shared-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-handler.ts
488 lines (403 loc) · 16.6 KB
/
cache-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import path from 'node:path';
import fs, { promises as fsPromises } from 'node:fs';
import type {
CacheHandler,
CacheHandlerValue,
CacheHandlerParametersSet,
CacheHandlerParametersGet,
FileSystemCacheContext,
CachedFetchValue,
CacheHandlerParametersRevalidateTag,
RouteMetadata,
NonNullableRouteMetadata,
} from '@neshca/next-types';
import { LRUCache } from 'lru-cache';
export type TagsManifest = {
version: 1;
items: Record<string, { revalidatedAt: number }>;
};
export type Cache<T = CacheHandlerValue> = {
get: (key: string) => Promise<CacheHandlerValue | null | undefined>;
set: (key: string, value: T, ttl?: number) => Promise<void>;
getTagsManifest: () => Promise<TagsManifest>;
revalidateTag: (tag: string, revalidatedAt: number) => Promise<void>;
};
type ReadCacheFromDisk = 'yes' | 'no';
type WriteCacheFromDisk = 'yes' | 'no';
type CacheDiskAccessMode = `read-${ReadCacheFromDisk}/write-${WriteCacheFromDisk}`;
type CacheConfigDefaultCache = {
diskAccessMode?: CacheDiskAccessMode;
cache?: Cache;
defaultLruCacheOptions?: never;
};
type CacheConfigWithDefaultCache = {
diskAccessMode?: CacheDiskAccessMode;
cache?: never;
defaultLruCacheOptions?: {
max?: number;
maxSize?: number;
};
};
export type CacheConfig = CacheConfigDefaultCache | CacheConfigWithDefaultCache;
export type CacheCreationContext = {
serverDistDir?: string;
dev?: boolean;
};
export class IncrementalCache implements CacheHandler {
private static diskAccessMode: CacheDiskAccessMode = 'read-yes/write-yes';
private static cache: Cache;
private static tagsManifestPath?: string;
private static serverDistDir?: string;
private static configDefiner: (cacheCreationContext: CacheCreationContext) => CacheConfig | undefined = () =>
undefined;
public static onCreation(
onCreationCallback: (cacheCreationContext: CacheCreationContext) => CacheConfig | undefined,
): void {
this.configDefiner = onCreationCallback;
}
private static init(cacheCreationContext: CacheCreationContext): void {
this.configure(this.configDefiner(cacheCreationContext));
}
private static configure({
diskAccessMode = 'read-yes/write-yes',
cache,
defaultLruCacheOptions,
}: CacheConfig = {}): void {
this.diskAccessMode = diskAccessMode;
if (this.serverDistDir && diskAccessMode === 'read-yes/write-yes') {
this.tagsManifestPath = path.join(this.serverDistDir, '..', 'cache', 'fetch-cache', 'tags-manifest.json');
}
if (cache) {
this.cache = cache;
return;
}
// if no cache is provided, we use a default LRU cache
const lruCache = new LRUCache<string, CacheHandlerValue>({
max: defaultLruCacheOptions?.max ?? 1000,
maxSize: defaultLruCacheOptions?.maxSize ?? 1024 * 1024 * 500, // 500MB
// Credits to Next.js for the following code
sizeCalculation: ({ value }) => {
if (!value) {
return 25;
} else if (value.kind === 'REDIRECT') {
return JSON.stringify(value.props).length;
} else if (value.kind === 'IMAGE') {
throw new Error('invariant image should not be incremental-cache');
} else if (value.kind === 'FETCH') {
return JSON.stringify(value.data || '').length;
} else if (value.kind === 'ROUTE') {
return value.body.length;
}
// rough estimate of size of cache value
return value.html.length + (JSON.stringify(value.pageData)?.length || 0);
},
});
let tagsManifest: TagsManifest = { items: {}, version: 1 };
if (this.tagsManifestPath) {
try {
const tagsManifestData = fs.readFileSync(this.tagsManifestPath, 'utf-8');
if (tagsManifestData) {
tagsManifest = JSON.parse(tagsManifestData) as TagsManifest;
}
} catch (err) {
// file doesn't exist. Use default tagsManifest
}
}
const defaultCache: Cache = {
get(key) {
return Promise.resolve(lruCache.get(key));
},
set(key, value) {
lruCache.set(key, value);
return Promise.resolve();
},
getTagsManifest() {
return Promise.resolve(tagsManifest);
},
revalidateTag(tag, revalidatedAt) {
tagsManifest.items[tag] = { revalidatedAt };
return Promise.resolve();
},
};
this.cache = defaultCache;
}
revalidatedTags: FileSystemCacheContext['revalidatedTags'];
appDir: FileSystemCacheContext['_appDir'];
serverDistDir: FileSystemCacheContext['serverDistDir'];
public constructor(context: FileSystemCacheContext) {
this.revalidatedTags = context.revalidatedTags;
this.appDir = context._appDir;
this.serverDistDir = context.serverDistDir;
if (!context.dev && !IncrementalCache.cache) {
IncrementalCache.serverDistDir = this.serverDistDir;
IncrementalCache.init({ dev: context.dev, serverDistDir: this.serverDistDir });
}
}
public async get(...args: CacheHandlerParametersGet): Promise<CacheHandlerValue | null> {
const [cacheKey, ctx = {}] = args;
const { tags = [], softTags = [], fetchCache } = ctx;
let cachedData: CacheHandlerValue | null = null;
try {
cachedData = (await IncrementalCache.cache.get(cacheKey)) ?? null;
} catch (error) {
return null;
}
if (!cachedData && IncrementalCache.diskAccessMode.includes('read-yes')) {
try {
const { filePath } = await this.getFsPath({
pathname: `${cacheKey}.body`,
appDir: true,
});
const fileData = await fsPromises.readFile(filePath);
const { mtime } = await fsPromises.stat(filePath);
const metaFilePath = filePath.replace(/\.body$/, '.meta');
const metaFileData = await fsPromises.readFile(metaFilePath, 'utf-8');
const meta: NonNullableRouteMetadata = JSON.parse(metaFileData) as NonNullableRouteMetadata;
const cacheEntry: CacheHandlerValue = {
lastModified: mtime.getTime(),
value: {
kind: 'ROUTE',
body: fileData,
headers: meta.headers,
status: meta.status,
},
};
return cacheEntry;
} catch (_) {
// no .meta data for the related key
}
try {
const { filePath: htmlFilePath, isAppPath: isHtmlFileInAppPath } = await this.getFsPath({
pathname: fetchCache ? cacheKey : `${cacheKey}.html`,
fetchCache,
});
const htmlFileData = await fsPromises.readFile(htmlFilePath, 'utf-8');
const { mtime } = await fsPromises.stat(htmlFilePath);
if (fetchCache) {
const lastModified = mtime.getTime();
const parsedData = JSON.parse(htmlFileData) as CachedFetchValue;
cachedData = {
lastModified,
value: parsedData,
};
if (cachedData.value?.kind === 'FETCH') {
const storedTags = cachedData.value?.data?.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?.every((tag) => storedTags?.includes(tag))) {
await this.set(cacheKey, cachedData.value, { tags });
}
}
} else {
const { filePath } = await this.getFsPath({
pathname: isHtmlFileInAppPath ? `${cacheKey}.rsc` : `${cacheKey}.json`,
appDir: isHtmlFileInAppPath,
});
const fileData = await fsPromises.readFile(filePath, 'utf-8');
const pageData = isHtmlFileInAppPath ? fileData : (JSON.parse(fileData) as object);
let meta: RouteMetadata | undefined;
if (isHtmlFileInAppPath) {
try {
const metaFilePath = htmlFilePath.replace(/\.html$/, '.meta');
const metaFileData = await fsPromises.readFile(metaFilePath, 'utf-8');
meta = JSON.parse(metaFileData) as RouteMetadata;
} catch {
// no .meta data for the related key
}
}
cachedData = {
lastModified: mtime.getTime(),
value: {
kind: 'PAGE',
html: htmlFileData,
pageData,
postponed: meta?.postponed,
headers: meta?.headers,
status: meta?.status,
},
};
}
if (cachedData) {
await IncrementalCache.cache.set(cacheKey, cachedData);
}
} catch (_) {
// unable to get data from disk
}
}
if (!cachedData) {
return null;
}
// credits to Next.js for the following code
if (cachedData.value?.kind === 'PAGE') {
let cacheTags: undefined | string[];
const tagsHeader = cachedData.value.headers?.['x-next-cache-tags'];
if (typeof tagsHeader === 'string') {
cacheTags = tagsHeader.split(',');
}
const tagsManifest = await IncrementalCache.cache.getTagsManifest();
if (cacheTags?.length) {
const isStale = cacheTags.some((tag) => {
const revalidatedAt = tagsManifest.items[tag]?.revalidatedAt;
return revalidatedAt && revalidatedAt >= (cachedData?.lastModified || Date.now());
});
// we trigger a blocking validation if an ISR page
// had a tag revalidated, if we want to be a background
// revalidation instead we return cachedData.lastModified = -1
if (isStale) {
return null;
}
}
}
if (cachedData.value?.kind === 'FETCH') {
const combinedTags = [...tags, ...softTags];
const tagsManifest = await IncrementalCache.cache.getTagsManifest();
const wasRevalidated = combinedTags.some((tag: string) => {
if (this.revalidatedTags.includes(tag)) {
return true;
}
const revalidatedAt = tagsManifest.items[tag]?.revalidatedAt;
return revalidatedAt && revalidatedAt >= (cachedData?.lastModified || Date.now());
});
// When revalidate tag is called we don't return
// stale cachedData so it's updated right away
if (wasRevalidated) {
return null;
}
}
return cachedData;
}
public async set(...args: CacheHandlerParametersSet): Promise<void> {
const [cacheKey, data, ctx] = args;
let ttl: number | undefined;
const { revalidate } = ctx;
if (IncrementalCache.diskAccessMode === 'read-no/write-no' && typeof revalidate === 'number') {
ttl = revalidate;
}
await IncrementalCache.cache.set(
cacheKey,
{
value: data,
lastModified: Date.now(),
},
ttl,
);
if (!data || IncrementalCache.diskAccessMode.includes('write-no')) {
return;
}
// credits to Next.js for the following code
if (data.kind === 'ROUTE') {
const { filePath } = await this.getFsPath({
pathname: `${cacheKey}.body`,
appDir: true,
});
const meta: RouteMetadata = {
headers: data.headers,
status: data.status,
postponed: undefined,
};
await fsPromises.mkdir(path.dirname(filePath), { recursive: true });
await fsPromises.writeFile(filePath, data.body);
await fsPromises.writeFile(filePath.replace(/\.body$/, '.meta'), JSON.stringify(meta, null, 2));
return;
}
if (data.kind === 'PAGE') {
const isAppPath = typeof data.pageData === 'string';
const { filePath: htmlPath } = await this.getFsPath({
pathname: `${cacheKey}.html`,
appDir: isAppPath,
});
await fsPromises.mkdir(path.dirname(htmlPath), { recursive: true });
await fsPromises.writeFile(htmlPath, data.html);
await fsPromises.writeFile(
(
await this.getFsPath({
pathname: `${cacheKey}.${isAppPath ? 'rsc' : 'json'}`,
appDir: isAppPath,
})
).filePath,
isAppPath ? JSON.stringify(data.pageData) : JSON.stringify(data.pageData),
);
if (data.headers || data.status) {
const meta: RouteMetadata = {
headers: data.headers,
status: data.status,
postponed: data.postponed,
};
await fsPromises.writeFile(htmlPath.replace(/\.html$/, '.meta'), JSON.stringify(meta));
}
return;
}
if (data.kind === 'FETCH') {
const { filePath } = await this.getFsPath({
pathname: cacheKey,
fetchCache: true,
});
await fsPromises.mkdir(path.dirname(filePath), { recursive: true });
await fsPromises.writeFile(
filePath,
JSON.stringify({
...data,
tags: ctx.tags,
}),
);
}
}
public async revalidateTag(...args: CacheHandlerParametersRevalidateTag): Promise<void> {
const [tag] = args;
await IncrementalCache.cache.revalidateTag(tag, Date.now());
if (!IncrementalCache.tagsManifestPath || IncrementalCache.diskAccessMode.includes('write-no')) {
return;
}
const tagsManifest = await IncrementalCache.cache.getTagsManifest();
try {
await fsPromises.mkdir(path.dirname(IncrementalCache.tagsManifestPath), { recursive: true });
await fsPromises.writeFile(IncrementalCache.tagsManifestPath, JSON.stringify(tagsManifest || {}));
} catch (err) {
// eslint-disable-next-line no-console -- Next.js logs it so we do too
console.warn('Failed to update tags manifest.', err);
}
}
// credits to Next.js for the following code
private async getFsPath({
pathname,
appDir,
fetchCache,
}: {
pathname: string;
appDir?: boolean;
fetchCache?: boolean;
}): Promise<{
filePath: string;
isAppPath: boolean;
}> {
if (fetchCache) {
// we store in .next/cache/fetch-cache so it can be persisted
// across deploys
return {
filePath: path.join(this.serverDistDir, '..', 'cache', 'fetch-cache', pathname),
isAppPath: false,
};
}
const isAppPath = false;
const filePath = path.join(this.serverDistDir, 'pages', pathname);
if (!this.appDir || appDir === false) {
return {
filePath,
isAppPath,
};
}
try {
await fsPromises.stat(filePath);
return {
filePath,
isAppPath,
};
} catch (err) {
return {
filePath: path.join(this.serverDistDir, 'app', pathname),
isAppPath: true,
};
}
}
}