@enonic/nextjs-adapter 2.0.0-B3 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/.eslint-childrc.js +17 -0
  2. package/.eslintrc.js +15 -0
  3. package/.github/dependabot.yml +20 -0
  4. package/.github/pull-merger.yml +1 -0
  5. package/.github/workflows/release.yml +39 -0
  6. package/LICENSE +201 -0
  7. package/RELEASE.md +75 -0
  8. package/package.json +8 -7
  9. package/server/.eslintrc.js +11 -0
  10. package/server/ClearableFileCache.ts +410 -0
  11. package/server/cache.ts +50 -0
  12. package/server/index.ts +74 -0
  13. package/server/tsconfig.json +23 -0
  14. package/setversion.js +8 -0
  15. package/src/.eslintrc.js +11 -0
  16. package/src/ComponentRegistry.ts +189 -0
  17. package/{UrlProcessor.js → src/UrlProcessor.ts} +54 -36
  18. package/{baseMappings.js → src/baseMappings.ts} +13 -3
  19. package/{guillotine/fetchContent.js → src/guillotine/fetchContent.ts} +496 -243
  20. package/src/guillotine/getMetaData.ts +246 -0
  21. package/{index.d.ts → src/index.ts} +1 -1
  22. package/src/tsconfig.json +20 -0
  23. package/{utils.js → src/utils.ts} +160 -70
  24. package/{views/BaseComponent.jsx → src/views/BaseComponent.tsx} +73 -39
  25. package/{views/BaseContent.jsx → src/views/BaseContent.tsx} +24 -17
  26. package/src/views/BaseLayout.tsx +45 -0
  27. package/{views/BaseMacro.jsx → src/views/BaseMacro.tsx} +45 -29
  28. package/src/views/BasePage.tsx +56 -0
  29. package/src/views/BasePart.tsx +50 -0
  30. package/{views/Empty.jsx → src/views/Empty.tsx} +2 -0
  31. package/src/views/Fragment.tsx +45 -0
  32. package/src/views/MainView.tsx +21 -0
  33. package/src/views/Region.tsx +89 -0
  34. package/{views/RichTextView.jsx → src/views/RichTextView.tsx} +31 -10
  35. package/{views/SingleComponent.jsx → src/views/SingleComponent.tsx} +8 -2
  36. package/src/views/StaticContent.tsx +40 -0
  37. package/src/views/Text.tsx +15 -0
  38. package/src/views/macros/DefaultMacro.tsx +9 -0
  39. package/src/views/macros/DisableMacro.tsx +8 -0
  40. package/ComponentRegistry.d.ts +0 -64
  41. package/ComponentRegistry.js +0 -121
  42. package/UrlProcessor.d.ts +0 -25
  43. package/baseMappings.d.ts +0 -1
  44. package/guillotine/fetchContent.d.ts +0 -48
  45. package/guillotine/getMetaData.d.ts +0 -94
  46. package/guillotine/getMetaData.js +0 -128
  47. package/index.js +0 -5
  48. package/query/Shortcut.d.ts +0 -1
  49. package/server/ClearableFileCache.d.ts +0 -31
  50. package/server/ClearableFileCache.js +0 -472
  51. package/server/cache.d.ts +0 -2
  52. package/server/cache.js +0 -129
  53. package/server/index.d.ts +0 -5
  54. package/server/index.js +0 -132
  55. package/utils.d.ts +0 -114
  56. package/views/BaseComponent.d.ts +0 -25
  57. package/views/BaseContent.d.ts +0 -4
  58. package/views/BaseLayout.d.ts +0 -17
  59. package/views/BaseLayout.jsx +0 -24
  60. package/views/BaseMacro.d.ts +0 -16
  61. package/views/BasePage.d.ts +0 -19
  62. package/views/BasePage.jsx +0 -34
  63. package/views/BasePart.d.ts +0 -19
  64. package/views/BasePart.jsx +0 -24
  65. package/views/Empty.d.ts +0 -3
  66. package/views/Fragment.d.ts +0 -10
  67. package/views/Fragment.jsx +0 -30
  68. package/views/MainView.d.ts +0 -4
  69. package/views/MainView.jsx +0 -14
  70. package/views/Region.d.ts +0 -20
  71. package/views/Region.jsx +0 -52
  72. package/views/RichTextView.d.ts +0 -16
  73. package/views/SingleComponent.d.ts +0 -5
  74. package/views/StaticContent.d.ts +0 -15
  75. package/views/StaticContent.jsx +0 -36
  76. package/views/Text.d.ts +0 -9
  77. package/views/Text.jsx +0 -4
  78. package/views/macros/DefaultMacro.d.ts +0 -4
  79. package/views/macros/DefaultMacro.jsx +0 -3
  80. package/views/macros/DisableMacro.d.ts +0 -3
  81. package/views/macros/DisableMacro.jsx +0 -2
  82. /package/{query/Shortcut.js → src/query/Shortcut.ts} +0 -0
@@ -1,128 +0,0 @@
1
- import { sanitizeGraphqlName } from '../utils';
2
- import { ComponentRegistry } from '../ComponentRegistry';
3
- const macroConfigQuery = () => {
4
- return configQuery(ComponentRegistry.getMacros(), false, false);
5
- };
6
- const partConfigQuery = () => {
7
- return configQuery(ComponentRegistry.getParts());
8
- };
9
- const layoutConfigQuery = () => {
10
- return configQuery(ComponentRegistry.getLayouts());
11
- };
12
- const pageConfigQuery = () => {
13
- return configQuery(ComponentRegistry.getPages());
14
- };
15
- const configQuery = (list, includeAppName = true, canUseConfigAsJson = true) => {
16
- const hasQueryList = (list === null || list === void 0 ? void 0 : list.filter(([key, def]) => def.configQuery)) || [];
17
- if (hasQueryList.length === 0) {
18
- return canUseConfigAsJson ? 'configAsJson' : '';
19
- }
20
- const configsByApp = {};
21
- hasQueryList
22
- .forEach((entry) => {
23
- const nameParts = entry[0].split(':');
24
- if (nameParts.length === 2) {
25
- const sanitizedAppName = sanitizeGraphqlName(nameParts[0]);
26
- let existingConfigs = configsByApp[sanitizedAppName];
27
- if (!existingConfigs) {
28
- existingConfigs = [];
29
- configsByApp[sanitizedAppName] = existingConfigs;
30
- }
31
- existingConfigs.push(`${sanitizeGraphqlName(nameParts[1])}${entry[1].configQuery}`);
32
- }
33
- });
34
- // Still query for configAsJson if at least one item has no configQuery defined
35
- const configAsJsonQuery = canUseConfigAsJson && hasQueryList.length < list.length ? 'configAsJson' : '';
36
- if (!includeAppName) {
37
- return `${configAsJsonQuery}
38
- config {
39
- ${Object.values(configsByApp).reduce((arr, curr) => arr.concat(curr), []).join('\n')}
40
- }`;
41
- }
42
- else {
43
- return `${configAsJsonQuery}
44
- config {
45
- ${Object.entries(configsByApp).map(entry => entry[0] + '{\n' + entry[1].join('\n') + '\n}')}
46
- }`;
47
- }
48
- };
49
- /*
50
- IMPORTANT: make sure to transform your queries into functions too when using this function inside them !!!
51
- */
52
- export const richTextQuery = (fieldName) => {
53
- return `${fieldName}(processHtml:{type:absolute, imageWidths:[400, 800, 1200], imageSizes:"(max-width: 400px) 400px, (max-width: 800px) 800px, 1200px"}) {
54
- processedHtml
55
- macros {
56
- ref
57
- name
58
- descriptor
59
- ${macroConfigQuery()}
60
- }
61
- links {
62
- ref
63
- media {
64
- content {
65
- _id
66
- }
67
- }
68
- }
69
- images {
70
- ref
71
- image {
72
- _id
73
- }
74
- }
75
- }`;
76
- };
77
- const componentsQuery = () => `
78
- type
79
- path
80
- page {
81
- descriptor
82
- ${pageConfigQuery()}
83
- template {
84
- _path
85
- }
86
- }
87
- layout {
88
- descriptor
89
- ${layoutConfigQuery()}
90
- }
91
- text {
92
- ${richTextQuery('value')}
93
- }
94
- part {
95
- descriptor
96
- ${partConfigQuery()}
97
- }
98
- image {
99
- caption
100
- image {
101
- imageUrl (type:absolute, scale: "width-768")
102
- }
103
- }
104
- `;
105
- // THIS QUERY DOES NOT SUPPORT NESTED FRAGMENTS
106
- export const pageFragmentQuery = () => `
107
- components(resolveFragment: false, resolveTemplate: true) {
108
- fragment {
109
- id
110
- fragment {
111
- components {
112
- ${componentsQuery()}
113
- }
114
- }
115
- }
116
- ${componentsQuery()}
117
- }`;
118
- export function getMetaQuery(pageFragment) {
119
- return `query($path:ID!){
120
- guillotine {
121
- get(key:$path) {
122
- _path
123
- type
124
- ${pageFragment || ''}
125
- }
126
- }
127
- }`;
128
- }
package/index.js DELETED
@@ -1,5 +0,0 @@
1
- export * from './utils';
2
- export * from './guillotine/fetchContent';
3
- export * from './guillotine/getMetaData';
4
- export * from './ComponentRegistry';
5
- export * from './UrlProcessor';
@@ -1 +0,0 @@
1
- export declare const getShortcutQuery = "\n query($path:ID!){\n guillotine {\n get(key:$path) {\n ... on base_Shortcut {\n data {\n target {\n pageUrl(type: absolute)\n }\n }\n }\n }\n }\n }";
@@ -1,31 +0,0 @@
1
- import { CacheHandler, CacheHandlerContext, CacheHandlerValue } from 'next/dist/server/lib/incremental-cache';
2
- import { CacheFs } from 'next/dist/shared/lib/utils';
3
- type FileSystemCacheContext = Omit<CacheHandlerContext, 'fs' | 'serverDistDir'> & {
4
- fs: CacheFs;
5
- serverDistDir: string;
6
- };
7
- export default class FileSystemCache implements CacheHandler {
8
- private fs;
9
- private flushToDisk?;
10
- private serverDistDir;
11
- private appDir;
12
- private tagsManifestPath?;
13
- private revalidatedTags;
14
- constructor(ctx: FileSystemCacheContext);
15
- clear(): void;
16
- delete(path: string): boolean | undefined;
17
- private loadTagsManifest;
18
- setTags(key: string, tags: string[]): Promise<void>;
19
- revalidateTag(tag: string): Promise<void>;
20
- get(key: string, fetchCache?: boolean): Promise<CacheHandlerValue | null>;
21
- set(key: string, data: CacheHandlerValue['value']): Promise<void>;
22
- getFsPath({ pathname, appDir, fetchCache, }: {
23
- pathname: string;
24
- appDir?: boolean;
25
- fetchCache?: boolean;
26
- }): Promise<{
27
- filePath: string;
28
- isAppPath: boolean;
29
- }>;
30
- }
31
- export {};
@@ -1,472 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- var path_1 = __importDefault(require("path"));
43
- var lru_cache_1 = require("lru-cache");
44
- var utils_1 = require("next/dist/server/lib/incremental-cache/utils");
45
- var memoryCache;
46
- var tagsManifest;
47
- var FileSystemCache = /** @class */ (function () {
48
- function FileSystemCache(ctx) {
49
- this.fs = ctx.fs;
50
- this.flushToDisk = ctx.flushToDisk;
51
- this.serverDistDir = ctx.serverDistDir;
52
- this.appDir = !!ctx._appDir;
53
- this.revalidatedTags = ctx.revalidatedTags;
54
- if (ctx.maxMemoryCacheSize && !memoryCache) {
55
- memoryCache = new lru_cache_1.LRUCache({
56
- max: ctx.maxMemoryCacheSize,
57
- maxSize: 10 * ctx.maxMemoryCacheSize,
58
- sizeCalculation: function (_a) {
59
- var _b;
60
- var value = _a.value;
61
- if (!value) {
62
- return 25;
63
- }
64
- else if (value.kind === 'REDIRECT') {
65
- return JSON.stringify(value.props).length;
66
- }
67
- else if (value.kind === 'IMAGE') {
68
- throw new Error('invariant image should not be incremental-cache');
69
- }
70
- else if (value.kind === 'FETCH') {
71
- return JSON.stringify(value.data || '').length;
72
- }
73
- else if (value.kind === 'ROUTE') {
74
- return value.body.length;
75
- }
76
- // rough estimate of size of cache value
77
- return (value.html.length + (((_b = JSON.stringify(value.pageData)) === null || _b === void 0 ? void 0 : _b.length) || 0));
78
- },
79
- });
80
- }
81
- if (this.serverDistDir && this.fs) {
82
- this.tagsManifestPath = path_1.default.join(this.serverDistDir, '..', 'cache', 'fetch-cache', 'tags-manifest.json');
83
- this.loadTagsManifest();
84
- }
85
- }
86
- FileSystemCache.prototype.clear = function () {
87
- memoryCache === null || memoryCache === void 0 ? void 0 : memoryCache.clear();
88
- };
89
- FileSystemCache.prototype.delete = function (path) {
90
- return memoryCache === null || memoryCache === void 0 ? void 0 : memoryCache.delete(path);
91
- };
92
- FileSystemCache.prototype.loadTagsManifest = function () {
93
- if (!this.tagsManifestPath || !this.fs || tagsManifest)
94
- return;
95
- try {
96
- tagsManifest = JSON.parse(this.fs.readFileSync(this.tagsManifestPath).toString('utf8'));
97
- }
98
- catch (err) {
99
- tagsManifest = { version: 1, items: {} };
100
- }
101
- };
102
- FileSystemCache.prototype.setTags = function (key, tags) {
103
- return __awaiter(this, void 0, void 0, function () {
104
- var _i, tags_1, tag, data, err_1;
105
- return __generator(this, function (_a) {
106
- switch (_a.label) {
107
- case 0:
108
- this.loadTagsManifest();
109
- if (!tagsManifest || !this.tagsManifestPath) {
110
- return [2 /*return*/];
111
- }
112
- for (_i = 0, tags_1 = tags; _i < tags_1.length; _i++) {
113
- tag = tags_1[_i];
114
- data = tagsManifest.items[tag] || { keys: [] };
115
- if (!data.keys.includes(key)) {
116
- data.keys.push(key);
117
- }
118
- tagsManifest.items[tag] = data;
119
- }
120
- _a.label = 1;
121
- case 1:
122
- _a.trys.push([1, 4, , 5]);
123
- return [4 /*yield*/, this.fs.mkdir(path_1.default.dirname(this.tagsManifestPath))];
124
- case 2:
125
- _a.sent();
126
- return [4 /*yield*/, this.fs.writeFile(this.tagsManifestPath, JSON.stringify(tagsManifest || {}))];
127
- case 3:
128
- _a.sent();
129
- return [3 /*break*/, 5];
130
- case 4:
131
- err_1 = _a.sent();
132
- console.warn('Failed to update tags manifest.', err_1);
133
- return [3 /*break*/, 5];
134
- case 5: return [2 /*return*/];
135
- }
136
- });
137
- });
138
- };
139
- FileSystemCache.prototype.revalidateTag = function (tag) {
140
- return __awaiter(this, void 0, void 0, function () {
141
- var data, err_2;
142
- return __generator(this, function (_a) {
143
- switch (_a.label) {
144
- case 0:
145
- // we need to ensure the tagsManifest is refreshed
146
- // since separate workers can be updating it at the same
147
- // time and we can't flush out of sync data
148
- this.loadTagsManifest();
149
- if (!tagsManifest || !this.tagsManifestPath) {
150
- return [2 /*return*/];
151
- }
152
- data = tagsManifest.items[tag] || { keys: [] };
153
- data.revalidatedAt = Date.now();
154
- tagsManifest.items[tag] = data;
155
- _a.label = 1;
156
- case 1:
157
- _a.trys.push([1, 4, , 5]);
158
- return [4 /*yield*/, this.fs.mkdir(path_1.default.dirname(this.tagsManifestPath))];
159
- case 2:
160
- _a.sent();
161
- return [4 /*yield*/, this.fs.writeFile(this.tagsManifestPath, JSON.stringify(tagsManifest || {}))];
162
- case 3:
163
- _a.sent();
164
- return [3 /*break*/, 5];
165
- case 4:
166
- err_2 = _a.sent();
167
- console.warn('Failed to update tags manifest.', err_2);
168
- return [3 /*break*/, 5];
169
- case 5: return [2 /*return*/];
170
- }
171
- });
172
- });
173
- };
174
- FileSystemCache.prototype.get = function (key, fetchCache) {
175
- var _a, _b, _c, _d;
176
- return __awaiter(this, void 0, void 0, function () {
177
- var data, filePath, fileData, mtime, meta, _e, _f, cacheEntry, _1, _g, filePath, isAppPath, fileData, mtime, lastModified, parsedData, pageData, _h, _j, _k, _l, _m, _o, _p, meta, _q, _r, _2, _3, cacheTags, tagsHeader, derivedTags, isStale, innerData, derivedTags, wasRevalidated;
178
- var _this = this;
179
- return __generator(this, function (_s) {
180
- switch (_s.label) {
181
- case 0:
182
- data = memoryCache === null || memoryCache === void 0 ? void 0 : memoryCache.get(key);
183
- if (!(!data && process.env.NEXT_RUNTIME !== 'edge')) return [3 /*break*/, 24];
184
- _s.label = 1;
185
- case 1:
186
- _s.trys.push([1, 6, , 7]);
187
- return [4 /*yield*/, this.getFsPath({
188
- pathname: "".concat(key, ".body"),
189
- appDir: true,
190
- })];
191
- case 2:
192
- filePath = (_s.sent()).filePath;
193
- return [4 /*yield*/, this.fs.readFile(filePath)];
194
- case 3:
195
- fileData = _s.sent();
196
- return [4 /*yield*/, this.fs.stat(filePath)];
197
- case 4:
198
- mtime = (_s.sent()).mtime;
199
- _f = (_e = JSON).parse;
200
- return [4 /*yield*/, this.fs.readFile(filePath.replace(/\.body$/, '.meta'))];
201
- case 5:
202
- meta = _f.apply(_e, [(_s.sent()).toString('utf8')]);
203
- cacheEntry = {
204
- lastModified: mtime.getTime(),
205
- value: {
206
- kind: 'ROUTE',
207
- body: fileData,
208
- headers: meta.headers,
209
- status: meta.status,
210
- },
211
- };
212
- return [2 /*return*/, cacheEntry];
213
- case 6:
214
- _1 = _s.sent();
215
- return [3 /*break*/, 7];
216
- case 7:
217
- _s.trys.push([7, 23, , 24]);
218
- return [4 /*yield*/, this.getFsPath({
219
- pathname: fetchCache ? key : "".concat(key, ".html"),
220
- fetchCache: fetchCache,
221
- })];
222
- case 8:
223
- _g = _s.sent(), filePath = _g.filePath, isAppPath = _g.isAppPath;
224
- return [4 /*yield*/, this.fs.readFile(filePath)];
225
- case 9:
226
- fileData = (_s.sent()).toString('utf-8');
227
- return [4 /*yield*/, this.fs.stat(filePath)];
228
- case 10:
229
- mtime = (_s.sent()).mtime;
230
- if (!fetchCache) return [3 /*break*/, 11];
231
- lastModified = mtime.getTime();
232
- parsedData = JSON.parse(fileData);
233
- data = {
234
- lastModified: lastModified,
235
- value: parsedData,
236
- };
237
- return [3 /*break*/, 22];
238
- case 11:
239
- if (!isAppPath) return [3 /*break*/, 14];
240
- _k = (_j = this.fs).readFile;
241
- return [4 /*yield*/, this.getFsPath({
242
- pathname: "".concat(key, ".rsc"),
243
- appDir: true,
244
- })];
245
- case 12: return [4 /*yield*/, _k.apply(_j, [(_s.sent()).filePath])];
246
- case 13:
247
- _h = (_s.sent()).toString('utf8');
248
- return [3 /*break*/, 17];
249
- case 14:
250
- _m = (_l = JSON).parse;
251
- _p = (_o = this.fs).readFile;
252
- return [4 /*yield*/, this.getFsPath({
253
- pathname: "".concat(key, ".json"),
254
- appDir: false,
255
- })];
256
- case 15: return [4 /*yield*/, _p.apply(_o, [(_s.sent()).filePath])];
257
- case 16:
258
- _h = _m.apply(_l, [(_s.sent()).toString('utf8')]);
259
- _s.label = 17;
260
- case 17:
261
- pageData = _h;
262
- meta = {};
263
- if (!isAppPath) return [3 /*break*/, 21];
264
- _s.label = 18;
265
- case 18:
266
- _s.trys.push([18, 20, , 21]);
267
- _r = (_q = JSON).parse;
268
- return [4 /*yield*/, this.fs.readFile(filePath.replace(/\.html$/, '.meta'))];
269
- case 19:
270
- meta = _r.apply(_q, [(_s.sent()).toString('utf-8')]);
271
- return [3 /*break*/, 21];
272
- case 20:
273
- _2 = _s.sent();
274
- return [3 /*break*/, 21];
275
- case 21:
276
- data = {
277
- lastModified: mtime.getTime(),
278
- value: {
279
- kind: 'PAGE',
280
- html: fileData,
281
- pageData: pageData,
282
- headers: meta.headers,
283
- status: meta.status,
284
- },
285
- };
286
- _s.label = 22;
287
- case 22:
288
- if (data) {
289
- memoryCache === null || memoryCache === void 0 ? void 0 : memoryCache.set(key, data);
290
- }
291
- return [3 /*break*/, 24];
292
- case 23:
293
- _3 = _s.sent();
294
- return [3 /*break*/, 24];
295
- case 24:
296
- if (((_a = data === null || data === void 0 ? void 0 : data.value) === null || _a === void 0 ? void 0 : _a.kind) === 'PAGE') {
297
- tagsHeader = (_b = data.value.headers) === null || _b === void 0 ? void 0 : _b['x-next-cache-tags'];
298
- if (typeof tagsHeader === 'string') {
299
- cacheTags = tagsHeader.split(',');
300
- }
301
- }
302
- if (((_c = data === null || data === void 0 ? void 0 : data.value) === null || _c === void 0 ? void 0 : _c.kind) === 'PAGE' && (cacheTags === null || cacheTags === void 0 ? void 0 : cacheTags.length)) {
303
- this.loadTagsManifest();
304
- derivedTags = (0, utils_1.getDerivedTags)(cacheTags || []);
305
- isStale = derivedTags.some(function (tag) {
306
- var _a;
307
- return (((_a = tagsManifest === null || tagsManifest === void 0 ? void 0 : tagsManifest.items[tag]) === null || _a === void 0 ? void 0 : _a.revalidatedAt) &&
308
- (tagsManifest === null || tagsManifest === void 0 ? void 0 : tagsManifest.items[tag].revalidatedAt) >=
309
- ((data === null || data === void 0 ? void 0 : data.lastModified) || Date.now()));
310
- });
311
- // we trigger a blocking validation if an ISR page
312
- // had a tag revalidated, if we want to be a background
313
- // revalidation instead we return data.lastModified = -1
314
- if (isStale) {
315
- data = undefined;
316
- }
317
- }
318
- if (data && ((_d = data === null || data === void 0 ? void 0 : data.value) === null || _d === void 0 ? void 0 : _d.kind) === 'FETCH') {
319
- this.loadTagsManifest();
320
- innerData = data.value.data;
321
- derivedTags = (0, utils_1.getDerivedTags)(innerData.tags || []);
322
- wasRevalidated = derivedTags.some(function (tag) {
323
- var _a;
324
- if (_this.revalidatedTags.includes(tag)) {
325
- return true;
326
- }
327
- return (((_a = tagsManifest === null || tagsManifest === void 0 ? void 0 : tagsManifest.items[tag]) === null || _a === void 0 ? void 0 : _a.revalidatedAt) &&
328
- (tagsManifest === null || tagsManifest === void 0 ? void 0 : tagsManifest.items[tag].revalidatedAt) >=
329
- ((data === null || data === void 0 ? void 0 : data.lastModified) || Date.now()));
330
- });
331
- // When revalidate tag is called we don't return
332
- // stale data so it's updated right away
333
- if (wasRevalidated) {
334
- data = undefined;
335
- }
336
- }
337
- return [2 /*return*/, data || null];
338
- }
339
- });
340
- });
341
- };
342
- FileSystemCache.prototype.set = function (key, data) {
343
- return __awaiter(this, void 0, void 0, function () {
344
- var filePath, isAppPath, htmlPath, _a, _b, filePath;
345
- return __generator(this, function (_c) {
346
- switch (_c.label) {
347
- case 0:
348
- memoryCache === null || memoryCache === void 0 ? void 0 : memoryCache.set(key, {
349
- value: data,
350
- lastModified: Date.now(),
351
- });
352
- if (!this.flushToDisk)
353
- return [2 /*return*/];
354
- if (!((data === null || data === void 0 ? void 0 : data.kind) === 'ROUTE')) return [3 /*break*/, 5];
355
- return [4 /*yield*/, this.getFsPath({
356
- pathname: "".concat(key, ".body"),
357
- appDir: true,
358
- })];
359
- case 1:
360
- filePath = (_c.sent()).filePath;
361
- return [4 /*yield*/, this.fs.mkdir(path_1.default.dirname(filePath))];
362
- case 2:
363
- _c.sent();
364
- return [4 /*yield*/, this.fs.writeFile(filePath, data.body)];
365
- case 3:
366
- _c.sent();
367
- return [4 /*yield*/, this.fs.writeFile(filePath.replace(/\.body$/, '.meta'), JSON.stringify({ headers: data.headers, status: data.status }))];
368
- case 4:
369
- _c.sent();
370
- return [2 /*return*/];
371
- case 5:
372
- if (!((data === null || data === void 0 ? void 0 : data.kind) === 'PAGE')) return [3 /*break*/, 13];
373
- isAppPath = typeof data.pageData === 'string';
374
- return [4 /*yield*/, this.getFsPath({
375
- pathname: "".concat(key, ".html"),
376
- appDir: isAppPath,
377
- })];
378
- case 6:
379
- htmlPath = (_c.sent()).filePath;
380
- return [4 /*yield*/, this.fs.mkdir(path_1.default.dirname(htmlPath))];
381
- case 7:
382
- _c.sent();
383
- return [4 /*yield*/, this.fs.writeFile(htmlPath, data.html)];
384
- case 8:
385
- _c.sent();
386
- _b = (_a = this.fs).writeFile;
387
- return [4 /*yield*/, this.getFsPath({
388
- pathname: "".concat(key, ".").concat(isAppPath ? 'rsc' : 'json'),
389
- appDir: isAppPath,
390
- })];
391
- case 9: return [4 /*yield*/, _b.apply(_a, [(_c.sent()).filePath,
392
- isAppPath ? data.pageData : JSON.stringify(data.pageData)])];
393
- case 10:
394
- _c.sent();
395
- if (!(data.headers || data.status)) return [3 /*break*/, 12];
396
- return [4 /*yield*/, this.fs.writeFile(htmlPath.replace(/\.html$/, '.meta'), JSON.stringify({
397
- headers: data.headers,
398
- status: data.status,
399
- }))];
400
- case 11:
401
- _c.sent();
402
- _c.label = 12;
403
- case 12: return [3 /*break*/, 18];
404
- case 13:
405
- if (!((data === null || data === void 0 ? void 0 : data.kind) === 'FETCH')) return [3 /*break*/, 18];
406
- return [4 /*yield*/, this.getFsPath({
407
- pathname: key,
408
- fetchCache: true,
409
- })];
410
- case 14:
411
- filePath = (_c.sent()).filePath;
412
- return [4 /*yield*/, this.fs.mkdir(path_1.default.dirname(filePath))];
413
- case 15:
414
- _c.sent();
415
- return [4 /*yield*/, this.fs.writeFile(filePath, JSON.stringify(data))];
416
- case 16:
417
- _c.sent();
418
- return [4 /*yield*/, this.setTags(key, data.data.tags || [])];
419
- case 17:
420
- _c.sent();
421
- _c.label = 18;
422
- case 18: return [2 /*return*/];
423
- }
424
- });
425
- });
426
- };
427
- FileSystemCache.prototype.getFsPath = function (_a) {
428
- var pathname = _a.pathname, appDir = _a.appDir, fetchCache = _a.fetchCache;
429
- return __awaiter(this, void 0, void 0, function () {
430
- var isAppPath, filePath, err_3;
431
- return __generator(this, function (_b) {
432
- switch (_b.label) {
433
- case 0:
434
- if (fetchCache) {
435
- // we store in .next/cache/fetch-cache so it can be persisted
436
- // across deploys
437
- return [2 /*return*/, {
438
- filePath: path_1.default.join(this.serverDistDir, '..', 'cache', 'fetch-cache', pathname),
439
- isAppPath: false,
440
- }];
441
- }
442
- isAppPath = false;
443
- filePath = path_1.default.join(this.serverDistDir, 'pages', pathname);
444
- if (!this.appDir || appDir === false)
445
- return [2 /*return*/, {
446
- filePath: filePath,
447
- isAppPath: isAppPath,
448
- }];
449
- _b.label = 1;
450
- case 1:
451
- _b.trys.push([1, 3, , 4]);
452
- return [4 /*yield*/, this.fs.readFile(filePath)];
453
- case 2:
454
- _b.sent();
455
- return [2 /*return*/, {
456
- filePath: filePath,
457
- isAppPath: isAppPath,
458
- }];
459
- case 3:
460
- err_3 = _b.sent();
461
- return [2 /*return*/, {
462
- filePath: path_1.default.join(this.serverDistDir, 'app', pathname),
463
- isAppPath: true,
464
- }];
465
- case 4: return [2 /*return*/];
466
- }
467
- });
468
- });
469
- };
470
- return FileSystemCache;
471
- }());
472
- exports.default = FileSystemCache;
package/server/cache.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { IncrementalCache } from 'next/dist/server/lib/incremental-cache';
2
- export declare const purgeCache: (cache: IncrementalCache, pathname?: string) => Promise<void>;