@enonic/nextjs-adapter 2.0.1 → 2.0.3
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.
- package/ComponentRegistry.d.ts +64 -0
- package/ComponentRegistry.js +121 -0
- package/UrlProcessor.d.ts +25 -0
- package/UrlProcessor.js +127 -0
- package/baseMappings.d.ts +1 -0
- package/{src/baseMappings.ts → baseMappings.js} +3 -13
- package/guillotine/fetchContent.d.ts +49 -0
- package/{src/guillotine/fetchContent.ts → guillotine/fetchContent.js} +244 -493
- package/guillotine/getMetaData.d.ts +94 -0
- package/guillotine/getMetaData.js +128 -0
- package/{src/index.ts → index.d.ts} +1 -1
- package/index.js +5 -0
- package/package.json +2 -2
- package/query/Shortcut.d.ts +1 -0
- package/server/ClearableFileCache.d.ts +31 -0
- package/server/ClearableFileCache.js +472 -0
- package/server/cache.d.ts +2 -0
- package/server/cache.js +129 -0
- package/server/index.d.ts +5 -0
- package/server/index.js +132 -0
- package/utils.d.ts +114 -0
- package/{src/utils.ts → utils.js} +81 -154
- package/views/BaseComponent.d.ts +25 -0
- package/{src/views/BaseComponent.tsx → views/BaseComponent.jsx} +39 -73
- package/views/BaseContent.d.ts +4 -0
- package/{src/views/BaseContent.tsx → views/BaseContent.jsx} +17 -24
- package/views/BaseLayout.d.ts +17 -0
- package/views/BaseLayout.jsx +24 -0
- package/views/BaseMacro.d.ts +16 -0
- package/{src/views/BaseMacro.tsx → views/BaseMacro.jsx} +29 -45
- package/views/BasePage.d.ts +19 -0
- package/views/BasePage.jsx +34 -0
- package/views/BasePart.d.ts +19 -0
- package/views/BasePart.jsx +24 -0
- package/views/Empty.d.ts +3 -0
- package/{src/views/Empty.tsx → views/Empty.jsx} +0 -2
- package/views/Fragment.d.ts +10 -0
- package/views/Fragment.jsx +30 -0
- package/views/MainView.d.ts +4 -0
- package/views/MainView.jsx +14 -0
- package/views/Region.d.ts +20 -0
- package/views/Region.jsx +52 -0
- package/views/RichTextView.d.ts +16 -0
- package/{src/views/RichTextView.tsx → views/RichTextView.jsx} +10 -31
- package/views/SingleComponent.d.ts +5 -0
- package/{src/views/SingleComponent.tsx → views/SingleComponent.jsx} +2 -8
- package/views/StaticContent.d.ts +15 -0
- package/views/StaticContent.jsx +36 -0
- package/views/Text.d.ts +9 -0
- package/views/Text.jsx +4 -0
- package/views/macros/DefaultMacro.d.ts +4 -0
- package/views/macros/DefaultMacro.jsx +3 -0
- package/views/macros/DisableMacro.d.ts +3 -0
- package/views/macros/DisableMacro.jsx +2 -0
- package/.eslint-childrc.js +0 -17
- package/.eslintrc.js +0 -15
- package/.github/dependabot.yml +0 -20
- package/.github/pull-merger.yml +0 -1
- package/.github/workflows/release.yml +0 -39
- package/LICENSE +0 -201
- package/RELEASE.md +0 -75
- package/server/.eslintrc.js +0 -11
- package/server/ClearableFileCache.ts +0 -410
- package/server/cache.ts +0 -50
- package/server/index.ts +0 -74
- package/server/tsconfig.json +0 -23
- package/src/.eslintrc.js +0 -11
- package/src/ComponentRegistry.ts +0 -189
- package/src/UrlProcessor.ts +0 -115
- package/src/guillotine/getMetaData.ts +0 -246
- package/src/tsconfig.json +0 -20
- package/src/views/BaseLayout.tsx +0 -45
- package/src/views/BasePage.tsx +0 -56
- package/src/views/BasePart.tsx +0 -50
- package/src/views/Fragment.tsx +0 -45
- package/src/views/MainView.tsx +0 -21
- package/src/views/Region.tsx +0 -89
- package/src/views/StaticContent.tsx +0 -40
- package/src/views/Text.tsx +0 -15
- package/src/views/macros/DefaultMacro.tsx +0 -9
- package/src/views/macros/DisableMacro.tsx +0 -8
- /package/{src/query/Shortcut.ts → query/Shortcut.js} +0 -0
|
@@ -0,0 +1,472 @@
|
|
|
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.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
+
function step(op) {
|
|
39
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
41
|
+
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;
|
|
42
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
+
switch (op[0]) {
|
|
44
|
+
case 0: case 1: t = op; break;
|
|
45
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
+
default:
|
|
49
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
+
if (t[2]) _.ops.pop();
|
|
54
|
+
_.trys.pop(); continue;
|
|
55
|
+
}
|
|
56
|
+
op = body.call(thisArg, _);
|
|
57
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
62
|
+
exports.purgeCache = void 0;
|
|
63
|
+
var fs = __importStar(require("fs"));
|
|
64
|
+
var getCacheHandler = function (cache) {
|
|
65
|
+
return cache.cacheHandler;
|
|
66
|
+
};
|
|
67
|
+
var purgeCache = function (cache, pathname) { return __awaiter(void 0, void 0, void 0, function () {
|
|
68
|
+
return __generator(this, function (_a) {
|
|
69
|
+
if (pathname) {
|
|
70
|
+
return [2 /*return*/, purgePath(cache, pathname)];
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return [2 /*return*/, purgeAll(cache)];
|
|
74
|
+
}
|
|
75
|
+
return [2 /*return*/];
|
|
76
|
+
});
|
|
77
|
+
}); };
|
|
78
|
+
exports.purgeCache = purgeCache;
|
|
79
|
+
var purgePath = function (cache, pathname) { return __awaiter(void 0, void 0, void 0, function () {
|
|
80
|
+
var handler, fsPathObj;
|
|
81
|
+
return __generator(this, function (_a) {
|
|
82
|
+
switch (_a.label) {
|
|
83
|
+
case 0:
|
|
84
|
+
handler = getCacheHandler(cache);
|
|
85
|
+
return [4 /*yield*/, handler.getFsPath({ pathname: pathname })];
|
|
86
|
+
case 1:
|
|
87
|
+
fsPathObj = _a.sent();
|
|
88
|
+
try {
|
|
89
|
+
/* Delete entry in cache */
|
|
90
|
+
handler.delete(pathname);
|
|
91
|
+
/* Delete files from disc */
|
|
92
|
+
fs.unlinkSync("".concat(fsPathObj.filePath, ".html"));
|
|
93
|
+
fs.unlinkSync("".concat(fsPathObj.filePath, ".json"));
|
|
94
|
+
console.info("\nCache deleted for '".concat(fsPathObj.filePath, "'\n"));
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
console.error("Could not delete cache of '".concat(fsPathObj.filePath, "':"), err);
|
|
98
|
+
}
|
|
99
|
+
return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}); };
|
|
103
|
+
var purgeAll = function (cache) { return __awaiter(void 0, void 0, void 0, function () {
|
|
104
|
+
var handler, pagesRoot, err_1;
|
|
105
|
+
return __generator(this, function (_a) {
|
|
106
|
+
switch (_a.label) {
|
|
107
|
+
case 0:
|
|
108
|
+
handler = getCacheHandler(cache);
|
|
109
|
+
_a.label = 1;
|
|
110
|
+
case 1:
|
|
111
|
+
_a.trys.push([1, 3, , 4]);
|
|
112
|
+
/* Purge cache */
|
|
113
|
+
handler.clear();
|
|
114
|
+
return [4 /*yield*/, handler.getFsPath({ pathname: '' })];
|
|
115
|
+
case 2:
|
|
116
|
+
pagesRoot = (_a.sent()).filePath;
|
|
117
|
+
if (fs.existsSync(pagesRoot)) {
|
|
118
|
+
fs.rmSync(pagesRoot, { recursive: true });
|
|
119
|
+
}
|
|
120
|
+
console.info('\nCache was successfully purged\n');
|
|
121
|
+
return [3 /*break*/, 4];
|
|
122
|
+
case 3:
|
|
123
|
+
err_1 = _a.sent();
|
|
124
|
+
console.error('Could not purge cache:', err_1);
|
|
125
|
+
return [3 /*break*/, 4];
|
|
126
|
+
case 4: return [2 /*return*/];
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}); };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { NextServer } from 'next/dist/server/next';
|
|
2
|
+
export declare const PURGE_CACHE_URL = "/_/enonic/cache/purge";
|
|
3
|
+
export declare const withEnonicCache: (config: any) => any;
|
|
4
|
+
declare const _default: (next: (options: any) => any) => Promise<NextServer>;
|
|
5
|
+
export default _default;
|