@sigil-dev/grimoire 0.9.10 → 0.9.11
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/package.json +3 -3
- package/src/client/csrf.ts +1 -1
- package/src/client/enhance.ts +63 -29
- package/src/client/router.ts +35 -11
- package/src/dev/compile-module.ts +12 -16
- package/src/dev/loader.ts +25 -4
- package/src/dev/resolve.ts +1 -1
- package/src/env/modules.ts +62 -0
- package/src/env/typegen.ts +2 -2
- package/src/rendering/head.ts +2 -5
- package/src/rendering/hydrate.ts +2 -0
- package/src/rendering/index.ts +144 -25
- package/src/rendering/ssrPlugin.ts +28 -13
- package/src/routing/transform-routes.ts +16 -16
- package/src/server/build.ts +17 -25
- package/src/server/cookie-utils.ts +19 -5
- package/src/server/coordinator.ts +113 -54
- package/src/server/hooks.ts +3 -1
- package/src/server/index.ts +172 -73
- package/src/server/resolver.ts +98 -0
- package/src/server/worker.ts +6 -5
- package/src/typegen/index.ts +17 -4
- package/test/client-router.test.ts +188 -0
- package/test/coordinator.test.ts +85 -0
- package/test/dev-loader.test.ts +30 -0
- package/test/env.test.ts +78 -0
- package/test/middleware.test.ts +20 -0
- package/test/rendering.test.ts +46 -0
- package/test/server.test.ts +204 -9
- package/test/streaming.test.ts +42 -1
- package/test/typegen.test.ts +31 -1
- package/src/client/head.ts +0 -30
package/test/server.test.ts
CHANGED
|
@@ -150,6 +150,17 @@ export const websocket = {
|
|
|
150
150
|
};`,
|
|
151
151
|
);
|
|
152
152
|
|
|
153
|
+
// a page beside a WebSocket-only +server.ts
|
|
154
|
+
await mkdir(join(wsDir, "room"), { recursive: true });
|
|
155
|
+
await writeFile(
|
|
156
|
+
join(wsDir, "room", "+server.ts"),
|
|
157
|
+
`export const websocket = { message(ws, data) { ws.send(data); } };`,
|
|
158
|
+
);
|
|
159
|
+
await writeFile(
|
|
160
|
+
join(wsDir, "room", "+page.tsx"),
|
|
161
|
+
`export default () => "<h1>room page</h1>";`,
|
|
162
|
+
);
|
|
163
|
+
|
|
153
164
|
server = await createServer({
|
|
154
165
|
port: 3005,
|
|
155
166
|
routes: wsDir,
|
|
@@ -160,6 +171,16 @@ export const websocket = {
|
|
|
160
171
|
afterAll(() => {
|
|
161
172
|
server.stop();
|
|
162
173
|
});
|
|
174
|
+
test("a method the endpoint doesn't handle renders the page", async () => {
|
|
175
|
+
const res = await fetch("http://localhost:3005/room");
|
|
176
|
+
expect(res.status).toBe(200);
|
|
177
|
+
expect(await res.text()).toContain("room page");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("with no page, it's still 405", async () => {
|
|
181
|
+
const res = await fetch("http://localhost:3005/echo");
|
|
182
|
+
expect(res.status).toBe(405);
|
|
183
|
+
});
|
|
163
184
|
|
|
164
185
|
function wsConnect(path: string): Promise<WebSocket> {
|
|
165
186
|
return new Promise((resolve, reject) => {
|
|
@@ -257,7 +278,7 @@ describe("Server — named form actions (?/name)", () => {
|
|
|
257
278
|
await mkdir(join(tmpDir, "form"), { recursive: true });
|
|
258
279
|
await writeFile(
|
|
259
280
|
join(tmpDir, "form", "+page.tsx"),
|
|
260
|
-
'export default () => "<h1>form</h1>";',
|
|
281
|
+
'export default ({ form }) => "<h1>form</h1>" + (form ? "form-says:" + form.method : "");',
|
|
261
282
|
);
|
|
262
283
|
await writeFile(
|
|
263
284
|
join(tmpDir, "form", "+page.server.ts"),
|
|
@@ -273,9 +294,36 @@ describe("Server — named form actions (?/name)", () => {
|
|
|
273
294
|
" },",
|
|
274
295
|
" };",
|
|
275
296
|
"}",
|
|
297
|
+
"export function load() { return {}; }",
|
|
276
298
|
"export async function POST() {",
|
|
277
299
|
' return { redirect: "/done" };',
|
|
278
300
|
"}",
|
|
301
|
+
"export function GET() {",
|
|
302
|
+
' return { redirect: "/ran-get" };',
|
|
303
|
+
"}",
|
|
304
|
+
].join("\n"),
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
// a page with a scoped style block (tag split: this file's own
|
|
308
|
+
// preload strips literal style blocks from sources it loads)
|
|
309
|
+
const tag = (name: string) => `<${name}>`;
|
|
310
|
+
await mkdir(join(tmpDir, "styled"), { recursive: true });
|
|
311
|
+
await writeFile(
|
|
312
|
+
join(tmpDir, "styled", "+page.tsx"),
|
|
313
|
+
`export default () => <h1 class="t">styled</h1>;\n${tag("style")}\n.t { color: red; }\n.u { color: blue; }\n${tag("/style")}\n`,
|
|
314
|
+
);
|
|
315
|
+
await mkdir(join(tmpDir, "forms"), { recursive: true });
|
|
316
|
+
await writeFile(
|
|
317
|
+
join(tmpDir, "forms", "+page.tsx"),
|
|
318
|
+
`export default () => '<form method="post" id="a"></form><form method="POST" action="?/x" id="b"></form><form action="/search" id="c"></form><form id="d"></form>';`,
|
|
319
|
+
);
|
|
320
|
+
// a streaming page: the loading shell goes out before the page renders
|
|
321
|
+
await mkdir(join(tmpDir, "slow"), { recursive: true });
|
|
322
|
+
await writeFile(
|
|
323
|
+
join(tmpDir, "slow", "+page.tsx"),
|
|
324
|
+
[
|
|
325
|
+
`export const loading = () => '<form method="post" id="shell"></form>';`,
|
|
326
|
+
`export default () => '<form method="post" id="page"></form>';`,
|
|
279
327
|
].join("\n"),
|
|
280
328
|
);
|
|
281
329
|
|
|
@@ -298,6 +346,7 @@ describe("Server — named form actions (?/name)", () => {
|
|
|
298
346
|
headers: {
|
|
299
347
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
300
348
|
Cookie: `_csrf=${csrfToken}`,
|
|
349
|
+
"X-Grimoire-Form": "1",
|
|
301
350
|
},
|
|
302
351
|
body: `x=1&_csrf=${csrfToken}`,
|
|
303
352
|
});
|
|
@@ -312,6 +361,24 @@ describe("Server — named form actions (?/name)", () => {
|
|
|
312
361
|
expect(json.data.method).toBe("POST");
|
|
313
362
|
});
|
|
314
363
|
|
|
364
|
+
test("a plain POST that fails re-renders the page with a form prop", async () => {
|
|
365
|
+
const res = await fetch(`${base}/form?/create`, {
|
|
366
|
+
method: "POST",
|
|
367
|
+
headers: {
|
|
368
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
369
|
+
Cookie: `_csrf=${csrfToken}`,
|
|
370
|
+
},
|
|
371
|
+
body: `x=1&_csrf=${csrfToken}`,
|
|
372
|
+
});
|
|
373
|
+
expect(res.status).toBe(400);
|
|
374
|
+
expect(res.headers.get("content-type")).toContain("text/html");
|
|
375
|
+
const html = await res.text();
|
|
376
|
+
expect(html).toContain("<h1>form</h1>");
|
|
377
|
+
expect(html).toContain("form-says:POST");
|
|
378
|
+
// hydration gets the same prop
|
|
379
|
+
expect(html).toMatch(/"form":\{"ok":false[^}]*"method":"POST"/);
|
|
380
|
+
});
|
|
381
|
+
|
|
315
382
|
test("unknown named action returns 404", async () => {
|
|
316
383
|
const res = await fetch(`${base}/form?/nope`, {
|
|
317
384
|
method: "POST",
|
|
@@ -324,15 +391,82 @@ describe("Server — named form actions (?/name)", () => {
|
|
|
324
391
|
expect(res.status).toBe(404);
|
|
325
392
|
});
|
|
326
393
|
|
|
327
|
-
test("GET named action
|
|
394
|
+
test("GET never runs a named action; it renders the page", async () => {
|
|
328
395
|
const res = await fetch(`${base}/form?/create`);
|
|
329
|
-
expect(res.status).toBe(
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
396
|
+
expect(res.status).toBe(200);
|
|
397
|
+
expect(res.headers.get("content-type")).toContain("text/html");
|
|
398
|
+
expect(await res.text()).toContain("form");
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
test("a page's scoped CSS is in the server-rendered <head>", async () => {
|
|
402
|
+
const html = await (await fetch(`${base}/styled`)).text();
|
|
403
|
+
const head = html.slice(0, html.indexOf("</head>"));
|
|
404
|
+
const hash = /<h1 class="t (s[0-9a-f]{8})">/.exec(html)?.[1];
|
|
405
|
+
expect(hash).toBeDefined();
|
|
406
|
+
expect(head).toContain(`<style id="sigil-${hash}">`);
|
|
407
|
+
expect(head).toContain(`.t.${hash} { color: red; }`);
|
|
408
|
+
expect(head).toContain(`.u.${hash} { color: blue; }`);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
test("every POST form gets a CSRF input, GET forms never do", async () => {
|
|
412
|
+
const html = await (await fetch(`${base}/forms`)).text();
|
|
413
|
+
const token = (id: string) =>
|
|
414
|
+
new RegExp(
|
|
415
|
+
`<form[^>]*id="${id}"[^>]*><input type="hidden" name="_csrf"`,
|
|
416
|
+
).test(html);
|
|
417
|
+
expect(token("a")).toBe(true); // no action attribute
|
|
418
|
+
expect(token("b")).toBe(true);
|
|
419
|
+
expect(token("c")).toBe(false); // GET: the token would land in the URL
|
|
420
|
+
expect(token("d")).toBe(false);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
test("a GET export in +page.server.ts is not run for page views", async () => {
|
|
424
|
+
const res = await fetch(`${base}/form`, { redirect: "manual" });
|
|
425
|
+
expect(res.status).toBe(200);
|
|
426
|
+
expect(await res.text()).toContain("<h1>form</h1>");
|
|
427
|
+
const head = await fetch(`${base}/form`, { method: "HEAD" });
|
|
428
|
+
expect(head.status).toBe(200);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test("streaming pages get CSRF tokens and the cookie too", async () => {
|
|
432
|
+
const res = await fetch(`${base}/slow`);
|
|
433
|
+
const token = /_csrf=([a-f0-9]+)/.exec(
|
|
434
|
+
res.headers.get("set-cookie") ?? "",
|
|
435
|
+
)?.[1];
|
|
436
|
+
expect(token).toBeDefined();
|
|
437
|
+
const html = await res.text();
|
|
438
|
+
for (const id of ["shell", "page"]) {
|
|
439
|
+
expect(html).toContain(
|
|
440
|
+
`<form method="post" id="${id}"><input type="hidden" name="_csrf" value="${token}"`,
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
test("an existing CSRF cookie is kept, so other tabs' forms still work", async () => {
|
|
446
|
+
const res = await fetch(`${base}/forms`, {
|
|
447
|
+
headers: { Cookie: `_csrf=${csrfToken}` },
|
|
448
|
+
});
|
|
449
|
+
expect(res.headers.get("set-cookie")).toContain(`_csrf=${csrfToken}`);
|
|
450
|
+
expect(await res.text()).toContain(`value="${csrfToken}"`);
|
|
451
|
+
// a malformed cookie is replaced
|
|
452
|
+
const bad = await fetch(`${base}/forms`, {
|
|
453
|
+
headers: { Cookie: "_csrf=nope" },
|
|
454
|
+
});
|
|
455
|
+
expect(bad.headers.get("set-cookie")).toMatch(/_csrf=[0-9a-f]{64}/);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
test("load and the other module hooks are not actions", async () => {
|
|
459
|
+
for (const name of ["load", "canMatch", "csrf"]) {
|
|
460
|
+
const res = await fetch(`${base}/form?/${name}`, {
|
|
461
|
+
method: "POST",
|
|
462
|
+
headers: {
|
|
463
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
464
|
+
Cookie: `_csrf=${csrfToken}`,
|
|
465
|
+
},
|
|
466
|
+
body: `x=1&_csrf=${csrfToken}`,
|
|
467
|
+
});
|
|
468
|
+
expect(res.status).toBe(404);
|
|
469
|
+
}
|
|
336
470
|
});
|
|
337
471
|
|
|
338
472
|
test("POST named action without CSRF token is forbidden", async () => {
|
|
@@ -344,6 +478,25 @@ describe("Server — named form actions (?/name)", () => {
|
|
|
344
478
|
expect(res.status).toBe(403);
|
|
345
479
|
});
|
|
346
480
|
|
|
481
|
+
test("an enhanced submission gets its outcome as JSON, not a 303", async () => {
|
|
482
|
+
const res = await fetch(`${base}/form`, {
|
|
483
|
+
method: "POST",
|
|
484
|
+
headers: {
|
|
485
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
486
|
+
Cookie: `_csrf=${csrfToken}`,
|
|
487
|
+
"X-Grimoire-Form": "1",
|
|
488
|
+
},
|
|
489
|
+
body: `x=1&_csrf=${csrfToken}`,
|
|
490
|
+
redirect: "manual",
|
|
491
|
+
});
|
|
492
|
+
expect(res.status).toBe(200);
|
|
493
|
+
expect(await res.json()).toEqual({
|
|
494
|
+
type: "success",
|
|
495
|
+
location: "/done",
|
|
496
|
+
data: { redirect: "/done" },
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
|
|
347
500
|
test("method dispatch without query key is unchanged", async () => {
|
|
348
501
|
const res = await fetch(`${base}/form`, {
|
|
349
502
|
method: "POST",
|
|
@@ -358,3 +511,45 @@ describe("Server — named form actions (?/name)", () => {
|
|
|
358
511
|
expect(res.headers.get("location")).toBe("/done");
|
|
359
512
|
});
|
|
360
513
|
});
|
|
514
|
+
|
|
515
|
+
describe("Server — error pages", () => {
|
|
516
|
+
let dir: string;
|
|
517
|
+
let server: Awaited<ReturnType<typeof createServer>>;
|
|
518
|
+
const base = "http://localhost:3012";
|
|
519
|
+
|
|
520
|
+
beforeAll(async () => {
|
|
521
|
+
dir = join(tmpdir(), `grimoire-errors-${Date.now()}`);
|
|
522
|
+
await mkdir(join(dir, "boom"), { recursive: true });
|
|
523
|
+
await writeFile(
|
|
524
|
+
join(dir, "+error.tsx"),
|
|
525
|
+
"export default ({ status, message }) => `<h1>oops ${status}: ${message}</h1>`;",
|
|
526
|
+
);
|
|
527
|
+
await writeFile(
|
|
528
|
+
join(dir, "boom", "+page.server.ts"),
|
|
529
|
+
'export function load() { throw new Error("db password is hunter2"); }',
|
|
530
|
+
);
|
|
531
|
+
await writeFile(
|
|
532
|
+
join(dir, "boom", "+page.tsx"),
|
|
533
|
+
"export default () => 'never';",
|
|
534
|
+
);
|
|
535
|
+
server = await createServer({ port: 3012, routes: dir, _skipBuild: true });
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
afterAll(() => server.stop());
|
|
539
|
+
|
|
540
|
+
test("an unknown URL renders +error.tsx as a whole document", async () => {
|
|
541
|
+
const res = await fetch(`${base}/nowhere`);
|
|
542
|
+
expect(res.status).toBe(404);
|
|
543
|
+
const html = await res.text();
|
|
544
|
+
expect(html).toContain("<!DOCTYPE html>");
|
|
545
|
+
expect(html).toContain("oops 404: Not Found");
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
test("an unexpected error renders +error.tsx with a generic message", async () => {
|
|
549
|
+
const res = await fetch(`${base}/boom`);
|
|
550
|
+
expect(res.status).toBe(500);
|
|
551
|
+
const html = await res.text();
|
|
552
|
+
expect(html).toContain("oops 500: Internal Server Error");
|
|
553
|
+
expect(html).not.toContain("hunter2");
|
|
554
|
+
});
|
|
555
|
+
});
|
package/test/streaming.test.ts
CHANGED
|
@@ -61,6 +61,45 @@ describe("Streaming SSR", () => {
|
|
|
61
61
|
expect(html).toContain('"id":"fireball"');
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
+
test("late head tags survive quotes and scripts in a streaming page", async () => {
|
|
65
|
+
const dir = join(tmpdir(), `grimoire-late-head-${Date.now()}`);
|
|
66
|
+
await mkdir(dir, { recursive: true });
|
|
67
|
+
const file = join(dir, "page.js");
|
|
68
|
+
const head = join(import.meta.dir, "../src/rendering/head.ts");
|
|
69
|
+
await writeFile(
|
|
70
|
+
file,
|
|
71
|
+
[
|
|
72
|
+
`import { Head } from ${JSON.stringify(head)};`,
|
|
73
|
+
`export const loading = () => "<p>wait</p>";`,
|
|
74
|
+
`export default () => { Head({ children: "<title>You've read it</title><script>var a = 1</script>" }); return "<p>done</p>"; };`,
|
|
75
|
+
].join("\n"),
|
|
76
|
+
);
|
|
77
|
+
// <Head> collects for the server only where there is no DOM
|
|
78
|
+
const g = globalThis as { document?: unknown };
|
|
79
|
+
const dom = g.document;
|
|
80
|
+
delete g.document;
|
|
81
|
+
try {
|
|
82
|
+
const res = await renderRoute(
|
|
83
|
+
makeMatched("/late", file),
|
|
84
|
+
new Request("http://localhost/late"),
|
|
85
|
+
);
|
|
86
|
+
const html = await res.text();
|
|
87
|
+
g.document = dom;
|
|
88
|
+
const script =
|
|
89
|
+
/<script>(\(function\(\)\{var p=new DOMParser[^]*?)<\/script>/.exec(
|
|
90
|
+
html,
|
|
91
|
+
)?.[1];
|
|
92
|
+
expect(script).toBeDefined();
|
|
93
|
+
// valid JavaScript, and nothing in it can close the <script> early
|
|
94
|
+
expect(() => new Function(script!)).not.toThrow();
|
|
95
|
+
expect(script).not.toContain("</");
|
|
96
|
+
expect(script).toContain("You've read it");
|
|
97
|
+
} finally {
|
|
98
|
+
g.document = dom;
|
|
99
|
+
await rm(dir, { recursive: true, force: true });
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
64
103
|
test("head script tag comes before head content", async () => {
|
|
65
104
|
const matched = makeMatched(
|
|
66
105
|
"/head-test",
|
|
@@ -227,7 +266,7 @@ describe("streamed promises (deferred data)", () => {
|
|
|
227
266
|
await writeFile(join(dir, "+page.server.ts"), serverSrc);
|
|
228
267
|
await writeFile(
|
|
229
268
|
join(dir, "+page.tsx"),
|
|
230
|
-
"export default function P({ data }) { return <div>{data.fast}{String(data.slow)}{String(data.boom)}</div>; }",
|
|
269
|
+
"export default function P({ data }) { return <div>{data.fast}{String(data.slow)}{String(data.boom)}{data.slow === undefined ? \"[pending]\" : \"\"}</div>; }",
|
|
231
270
|
);
|
|
232
271
|
const tree = await scanRoutes(dir);
|
|
233
272
|
const matched = matchRoute(tree, new URL("http://localhost/"));
|
|
@@ -254,6 +293,8 @@ describe("streamed promises (deferred data)", () => {
|
|
|
254
293
|
// the shell flushed before the promise settled
|
|
255
294
|
const shell = chunks.find((c) => c.includes("__grimoire_state__"))!;
|
|
256
295
|
expect(shell).not.toContain('"late"');
|
|
296
|
+
// the page rendered with the pending value as undefined, not a marker
|
|
297
|
+
expect(shell).toContain("[pending]");
|
|
257
298
|
// a later chunk carries the self-contained patch script
|
|
258
299
|
const late = chunks.find((c) => c.includes("grimoire:deferred"));
|
|
259
300
|
expect(late).toBeDefined();
|
package/test/typegen.test.ts
CHANGED
|
@@ -485,7 +485,7 @@ describe("generateTypes — route with +page.server.ts", () => {
|
|
|
485
485
|
test("Actions excludes load and default from keys", async () => {
|
|
486
486
|
const content = await readGenerated("src/routes/blog/[slug]");
|
|
487
487
|
expect(content).toContain(
|
|
488
|
-
'type _ActionKeys = Exclude<keyof _PS, "load" | "default">;',
|
|
488
|
+
'type _ActionKeys = Exclude<keyof _PS, "load" | "default" | "canMatch" | "csrf" | "loading">;',
|
|
489
489
|
);
|
|
490
490
|
});
|
|
491
491
|
|
|
@@ -695,3 +695,33 @@ export {};`,
|
|
|
695
695
|
await unlink(join(routeDir, "wrong-param-type.ts"));
|
|
696
696
|
});
|
|
697
697
|
});
|
|
698
|
+
|
|
699
|
+
describe("tsconfig paths for aliases", () => {
|
|
700
|
+
test("each alias gets a paths entry relative to .grimoire", async () => {
|
|
701
|
+
const root = join(tmpdir(), `grimoire-paths-${Date.now()}`);
|
|
702
|
+
await mkdir(join(root, "src/routes"), { recursive: true });
|
|
703
|
+
try {
|
|
704
|
+
const tree = await scanRoutes(join(root, "src/routes"), root);
|
|
705
|
+
await generateTypes(tree, {
|
|
706
|
+
projectRoot: root,
|
|
707
|
+
routesDir: join(root, "src/routes"),
|
|
708
|
+
outDir: join(root, ".grimoire", "types"),
|
|
709
|
+
aliases: { $lib: join(root, "src/lib"), "@ui": join(root, "ui") },
|
|
710
|
+
});
|
|
711
|
+
const config = JSON.parse(
|
|
712
|
+
await readFile(
|
|
713
|
+
join(root, ".grimoire", "tsconfig.generated.json"),
|
|
714
|
+
"utf-8",
|
|
715
|
+
),
|
|
716
|
+
);
|
|
717
|
+
expect(config.compilerOptions.paths).toEqual({
|
|
718
|
+
$lib: ["../src/lib"],
|
|
719
|
+
"$lib/*": ["../src/lib/*"],
|
|
720
|
+
"@ui": ["../ui"],
|
|
721
|
+
"@ui/*": ["../ui/*"],
|
|
722
|
+
});
|
|
723
|
+
} finally {
|
|
724
|
+
await rm(root, { recursive: true, force: true });
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
});
|
package/src/client/head.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
let headElements: (string | Node)[] = [];
|
|
2
|
-
|
|
3
|
-
export function resetHead(): void {
|
|
4
|
-
headElements = [];
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function collectHead(): (string | Node)[] {
|
|
8
|
-
return headElements;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function applyHead(): void {
|
|
12
|
-
//biome-ignore lint: ChildNode.remove() returns void??? sure typescript
|
|
13
|
-
document.querySelectorAll("[data-sigil-head]").forEach((el) => el.remove());
|
|
14
|
-
for (const el of headElements) {
|
|
15
|
-
if (el instanceof Node) {
|
|
16
|
-
const clone = el.cloneNode(true) as HTMLElement;
|
|
17
|
-
clone.dataset.sigilHead = "1";
|
|
18
|
-
document.head.appendChild(clone);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function Head({ children }: { children: string | Node }): Comment {
|
|
24
|
-
if (typeof document === "undefined") {
|
|
25
|
-
headElements.push(children);
|
|
26
|
-
} else if (children instanceof Node) {
|
|
27
|
-
(headElements as Node[]).push(children);
|
|
28
|
-
}
|
|
29
|
-
return document.createComment("head");
|
|
30
|
-
}
|