@qwik.dev/router 2.0.0-rc.4 → 2.0.0-rc.5
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/lib/adapters/shared/vite/index.mjs +1 -1
- package/lib/chunks/head.qwik.mjs +196 -102
- package/lib/chunks/request-path.mjs +9 -1
- package/lib/chunks/system.mjs +65 -10
- package/lib/chunks/user-response.mjs +281 -322
- package/lib/chunks/worker-thread.mjs +22 -11
- package/lib/index.d.ts +8 -6
- package/lib/index.qwik.mjs +239 -83
- package/lib/middleware/node/index.mjs +7 -1
- package/lib/middleware/request-handler/index.d.ts +2 -2
- package/lib/middleware/request-handler/index.mjs +4 -5
- package/lib/ssg/index.d.ts +7 -0
- package/lib/ssg/index.mjs +52 -6
- package/lib/vite/index.mjs +67 -20
- package/package.json +3 -3
package/lib/chunks/system.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as ensureSlash } from "./pathname.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { s as getLoaderName } from "./request-path.mjs";
|
|
3
3
|
import { _ as normalizePath } from "./fs.mjs";
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
@@ -10,6 +10,7 @@ async function createWorkerPool(sys, opts) {
|
|
|
10
10
|
const ssgWorkers = [];
|
|
11
11
|
const sitemapBuffer = [];
|
|
12
12
|
let sitemapStream = null;
|
|
13
|
+
let isClosing = false;
|
|
13
14
|
opts = { ...opts };
|
|
14
15
|
let outDir = opts.outDir;
|
|
15
16
|
if (typeof outDir !== "string") throw new Error(`Missing "outDir" option`);
|
|
@@ -30,16 +31,33 @@ async function createWorkerPool(sys, opts) {
|
|
|
30
31
|
const createWorker = () => {
|
|
31
32
|
let terminateResolve = null;
|
|
32
33
|
const mainTasks = /* @__PURE__ */ new Map();
|
|
34
|
+
const taskFiles = /* @__PURE__ */ new Map();
|
|
33
35
|
let terminateTimeout = null;
|
|
36
|
+
let stoppedPathname;
|
|
37
|
+
let exitCleanup = Promise.resolve();
|
|
38
|
+
let isReady = false;
|
|
39
|
+
let startupError;
|
|
34
40
|
let markWorkerReady;
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
let failWorkerStart;
|
|
42
|
+
const workerReady = new Promise((resolve, reject) => {
|
|
43
|
+
markWorkerReady = () => {
|
|
44
|
+
isReady = true;
|
|
45
|
+
resolve();
|
|
46
|
+
};
|
|
47
|
+
failWorkerStart = reject;
|
|
37
48
|
});
|
|
49
|
+
workerReady.catch(() => {});
|
|
38
50
|
const nodeWorker = new Worker(workerFilePath, { workerData });
|
|
39
51
|
nodeWorker.unref();
|
|
40
52
|
const ssgWorker = {
|
|
41
53
|
activeTasks: 0,
|
|
42
54
|
totalTasks: 0,
|
|
55
|
+
isRendering: (pathname) => mainTasks.has(pathname),
|
|
56
|
+
restart: async (pathname) => {
|
|
57
|
+
stoppedPathname = pathname;
|
|
58
|
+
await nodeWorker.terminate();
|
|
59
|
+
await exitCleanup;
|
|
60
|
+
},
|
|
43
61
|
render: (staticRoute) => {
|
|
44
62
|
return new Promise((resolve, reject) => {
|
|
45
63
|
ssgWorker.activeTasks++;
|
|
@@ -65,7 +83,7 @@ async function createWorkerPool(sys, opts) {
|
|
|
65
83
|
await nodeWorker.terminate();
|
|
66
84
|
resolve();
|
|
67
85
|
}, 1e3);
|
|
68
|
-
workerReady.then(() => nodeWorker.postMessage(msg));
|
|
86
|
+
workerReady.then(() => nodeWorker.postMessage(msg)).catch(() => {});
|
|
69
87
|
});
|
|
70
88
|
if (terminateTimeout) {
|
|
71
89
|
clearTimeout(terminateTimeout);
|
|
@@ -73,17 +91,30 @@ async function createWorkerPool(sys, opts) {
|
|
|
73
91
|
}
|
|
74
92
|
}
|
|
75
93
|
};
|
|
94
|
+
const takeTaskFiles = (pathname) => {
|
|
95
|
+
const files = taskFiles.get(pathname) ?? [];
|
|
96
|
+
taskFiles.delete(pathname);
|
|
97
|
+
return files;
|
|
98
|
+
};
|
|
76
99
|
nodeWorker.on("message", (msg) => {
|
|
77
100
|
switch (msg.type) {
|
|
78
101
|
case "ready":
|
|
79
102
|
markWorkerReady();
|
|
80
103
|
break;
|
|
104
|
+
case "file":
|
|
105
|
+
taskFiles.set(msg.pathname, [...taskFiles.get(msg.pathname) ?? [], msg.filePath]);
|
|
106
|
+
break;
|
|
81
107
|
case "render": {
|
|
82
108
|
const mainTask = mainTasks.get(msg.pathname);
|
|
83
109
|
if (mainTask) {
|
|
84
110
|
mainTasks.delete(msg.pathname);
|
|
85
111
|
ssgWorker.activeTasks--;
|
|
86
|
-
|
|
112
|
+
const files = takeTaskFiles(msg.pathname);
|
|
113
|
+
if (msg.error) removeFiles(files).then(() => mainTask({
|
|
114
|
+
...msg,
|
|
115
|
+
filePath: null
|
|
116
|
+
}));
|
|
117
|
+
else mainTask(msg);
|
|
87
118
|
}
|
|
88
119
|
break;
|
|
89
120
|
}
|
|
@@ -95,6 +126,7 @@ async function createWorkerPool(sys, opts) {
|
|
|
95
126
|
});
|
|
96
127
|
nodeWorker.on("error", (e) => {
|
|
97
128
|
log.error(`worker error`, e);
|
|
129
|
+
if (!isReady) startupError = e instanceof Error ? e : new Error(String(e));
|
|
98
130
|
});
|
|
99
131
|
nodeWorker.on("exit", (code) => {
|
|
100
132
|
log.debug(`worker exit code=${code}`);
|
|
@@ -102,33 +134,50 @@ async function createWorkerPool(sys, opts) {
|
|
|
102
134
|
clearTimeout(terminateTimeout);
|
|
103
135
|
terminateTimeout = null;
|
|
104
136
|
}
|
|
137
|
+
if (!isReady && !isClosing && !stoppedPathname) {
|
|
138
|
+
const cause = startupError ? `: ${startupError.message}` : "";
|
|
139
|
+
failWorkerStart(/* @__PURE__ */ new Error(`SSG worker exited with code ${code} before it was ready${cause}`));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
105
142
|
if (mainTasks.size > 0) {
|
|
143
|
+
const cleanups = [];
|
|
106
144
|
for (const [pathname, resolve] of mainTasks) {
|
|
107
145
|
ssgWorker.activeTasks--;
|
|
108
|
-
resolve({
|
|
146
|
+
const cleanup = removeFiles(takeTaskFiles(pathname)).then(() => resolve({
|
|
109
147
|
type: "render",
|
|
110
148
|
pathname,
|
|
111
149
|
url: "",
|
|
112
150
|
ok: false,
|
|
113
151
|
error: {
|
|
114
|
-
message: `Worker exited with code ${code}`,
|
|
152
|
+
message: stoppedPathname ? `Worker restarted to stop the render of ${stoppedPathname}` : `Worker exited with code ${code}`,
|
|
115
153
|
stack: void 0
|
|
116
154
|
},
|
|
155
|
+
isInterrupted: stoppedPathname !== void 0 && pathname !== stoppedPathname,
|
|
117
156
|
filePath: null,
|
|
118
157
|
contentType: null,
|
|
119
158
|
resourceType: null
|
|
120
|
-
});
|
|
159
|
+
}));
|
|
160
|
+
cleanups.push(cleanup);
|
|
121
161
|
}
|
|
162
|
+
exitCleanup = Promise.all(cleanups);
|
|
122
163
|
mainTasks.clear();
|
|
123
164
|
}
|
|
124
165
|
if (terminateResolve) {
|
|
125
166
|
terminateResolve();
|
|
126
167
|
terminateResolve = null;
|
|
127
168
|
}
|
|
128
|
-
if (code !== 0) console.error(`worker exit ${code}`);
|
|
169
|
+
if (code !== 0 && !stoppedPathname) console.error(`worker exit ${code}`);
|
|
170
|
+
if (!isClosing) replaceWorker(ssgWorker);
|
|
129
171
|
});
|
|
130
172
|
return ssgWorker;
|
|
131
173
|
};
|
|
174
|
+
const replaceWorker = (deadWorker) => {
|
|
175
|
+
const index = ssgWorkers.indexOf(deadWorker);
|
|
176
|
+
if (index !== -1) ssgWorkers[index] = createWorker();
|
|
177
|
+
};
|
|
178
|
+
const restartWorker = async (pathname) => {
|
|
179
|
+
await ssgWorkers.find((worker) => worker.isRendering(pathname))?.restart(pathname);
|
|
180
|
+
};
|
|
132
181
|
const getNextWorker = () => ssgWorkers.sort(ssgWorkerCompare)[0];
|
|
133
182
|
const hasAvailableWorker = () => {
|
|
134
183
|
return getNextWorker().activeTasks < maxTasksPerWorker;
|
|
@@ -136,7 +185,8 @@ async function createWorkerPool(sys, opts) {
|
|
|
136
185
|
const render = async (ssgRoute) => {
|
|
137
186
|
const result = await getNextWorker().render(ssgRoute);
|
|
138
187
|
const isNotFoundPage = result.pathname.endsWith("/404.html");
|
|
139
|
-
|
|
188
|
+
const isRenderedPage = result.ok && !result.error && result.resourceType === "page";
|
|
189
|
+
if (sitemapOutFile && isRenderedPage && !isNotFoundPage) {
|
|
140
190
|
sitemapBuffer.push(`<url><loc>${result.url}</loc></url>`);
|
|
141
191
|
if (sitemapBuffer.length > 50) {
|
|
142
192
|
const siteMapUrls = sitemapBuffer.join("\n") + "\n";
|
|
@@ -147,6 +197,7 @@ async function createWorkerPool(sys, opts) {
|
|
|
147
197
|
return result;
|
|
148
198
|
};
|
|
149
199
|
const close = async () => {
|
|
200
|
+
isClosing = true;
|
|
150
201
|
const promises = [];
|
|
151
202
|
if (sitemapStream) {
|
|
152
203
|
sitemapBuffer.push(`</urlset>`);
|
|
@@ -183,9 +234,11 @@ async function createWorkerPool(sys, opts) {
|
|
|
183
234
|
return {
|
|
184
235
|
hasAvailableWorker,
|
|
185
236
|
render,
|
|
237
|
+
restartWorker,
|
|
186
238
|
close
|
|
187
239
|
};
|
|
188
240
|
}
|
|
241
|
+
var removeFiles = (filePaths) => Promise.allSettled(filePaths.map((filePath) => fs.promises.rm(filePath, { force: true })));
|
|
189
242
|
function ssgWorkerCompare(a, b) {
|
|
190
243
|
if (a.activeTasks < b.activeTasks) return -1;
|
|
191
244
|
if (a.activeTasks > b.activeTasks) return 1;
|
|
@@ -212,11 +265,13 @@ async function createSystem(opts, threadId) {
|
|
|
212
265
|
if (threadId !== void 0) return {
|
|
213
266
|
debug: isDebug ? console.debug.bind(console, `[${threadId}]`) : noop,
|
|
214
267
|
error: console.error.bind(console, `[${threadId}]`),
|
|
268
|
+
warn: console.warn.bind(console, `[${threadId}]`),
|
|
215
269
|
info: isQuiet ? noop : console.info.bind(console, `[${threadId}]`)
|
|
216
270
|
};
|
|
217
271
|
return {
|
|
218
272
|
debug: isDebug ? console.debug.bind(console) : noop,
|
|
219
273
|
error: console.error.bind(console),
|
|
274
|
+
warn: console.warn.bind(console),
|
|
220
275
|
info: isQuiet ? noop : console.info.bind(console)
|
|
221
276
|
};
|
|
222
277
|
};
|