@sigil-dev/grimoire 0.9.21 → 0.9.23
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/README.md +83 -83
- package/index.ts +50 -48
- package/package.json +22 -10
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +871 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -749
- package/src/rendering/preload.ts +102 -101
- package/src/rendering/ssrPlugin.ts +1 -1
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +262 -262
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -44
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +211 -80
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +1 -1
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +799 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +381 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +808 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +1 -1
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import { describe, expect, mock, test } from "bun:test";
|
|
2
|
-
import { collectServices } from "../src/server/plugins";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Can one plugin use a database another plugin opened?
|
|
6
|
-
*
|
|
7
|
-
* The SQL plugin owns the connection and publishes it as a service. A second
|
|
8
|
-
* plugin — jobs, a rate limiter, anything that needs to persist something —
|
|
9
|
-
* wants that same connection rather than opening its own pool and splitting
|
|
10
|
-
* the budget again.
|
|
11
|
-
*
|
|
12
|
-
* As things stand `services()` is called with no arguments, so a plugin can
|
|
13
|
-
* only ever see what it produced itself. sekai-viewer works around this with
|
|
14
|
-
* an `inject` plugin that re-publishes the client, which means the ordering
|
|
15
|
-
* of that plugin in the array is load-bearing and nothing says so.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
type AnyPlugin = Parameters<typeof collectServices>[0][number];
|
|
19
|
-
|
|
20
|
-
function sqlPlugin(key = "db") {
|
|
21
|
-
return {
|
|
22
|
-
name: "sql",
|
|
23
|
-
config: () => ({}),
|
|
24
|
-
services: () => ({ [key]: { tag: "client" } }),
|
|
25
|
-
} as unknown as AnyPlugin;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
describe("plugin services", () => {
|
|
29
|
-
test("a later plugin can read what an earlier one published", async () => {
|
|
30
|
-
const consumer = {
|
|
31
|
-
name: "consumer",
|
|
32
|
-
// The whole point: use the client instead of opening a second pool.
|
|
33
|
-
services: async (ctx: { services: Record<string, unknown> }) => ({
|
|
34
|
-
seen: Boolean(ctx.services.db),
|
|
35
|
-
}),
|
|
36
|
-
} as unknown as AnyPlugin;
|
|
37
|
-
|
|
38
|
-
const collected = await collectServices([sqlPlugin(), consumer]);
|
|
39
|
-
|
|
40
|
-
expect(collected.seen).toBe(true);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("what an earlier plugin published is handed to the consumer", async () => {
|
|
44
|
-
const received: Record<string, unknown> = {};
|
|
45
|
-
const consumer = {
|
|
46
|
-
name: "consumer",
|
|
47
|
-
services: (ctx: { services: Record<string, unknown> }) => {
|
|
48
|
-
Object.assign(received, ctx.services);
|
|
49
|
-
return {};
|
|
50
|
-
},
|
|
51
|
-
} as unknown as AnyPlugin;
|
|
52
|
-
|
|
53
|
-
await collectServices([sqlPlugin("db"), consumer]);
|
|
54
|
-
|
|
55
|
-
expect(received.db).toEqual({ tag: "client" });
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test("the client a consumer reads is the same object the sql plugin owns", async () => {
|
|
59
|
-
const client = { tag: "client" };
|
|
60
|
-
const owner = { name: "sql", services: () => ({ db: client }) } as unknown as AnyPlugin;
|
|
61
|
-
const consumer = {
|
|
62
|
-
name: "consumer",
|
|
63
|
-
services: (ctx: { services: Record<string, unknown> }) => ({ used: ctx.services.db }),
|
|
64
|
-
} as unknown as AnyPlugin;
|
|
65
|
-
|
|
66
|
-
const collected = await collectServices([owner, consumer]);
|
|
67
|
-
|
|
68
|
-
// Identity, not equality: opening a second pool to compare is the
|
|
69
|
-
// thing this is meant to prevent.
|
|
70
|
-
expect(collected.used).toBe(client);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test("a consumer placed before the sql plugin sees nothing, rather than throwing", async () => {
|
|
74
|
-
// Order-independence is the point. A plugin must not have to be
|
|
75
|
-
// registered after the plugin it depends on to be correct.
|
|
76
|
-
const consumer = {
|
|
77
|
-
name: "consumer",
|
|
78
|
-
services: (ctx: { services: Record<string, unknown> }) => ({
|
|
79
|
-
seen: Boolean(ctx.services.db),
|
|
80
|
-
}),
|
|
81
|
-
} as unknown as AnyPlugin;
|
|
82
|
-
|
|
83
|
-
const collected = await collectServices([consumer, sqlPlugin()]);
|
|
84
|
-
|
|
85
|
-
expect(collected.seen).toBe(false);
|
|
86
|
-
expect(collected.db).toEqual({ tag: "client" });
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test("a plugin that ignores the context still works", async () => {
|
|
90
|
-
const solo = {
|
|
91
|
-
name: "solo",
|
|
92
|
-
services: () => ({ a: 1 }),
|
|
93
|
-
} as unknown as AnyPlugin;
|
|
94
|
-
|
|
95
|
-
expect(await collectServices([solo])).toEqual({ a: 1 });
|
|
96
|
-
});
|
|
97
|
-
});
|
|
1
|
+
import { describe, expect, mock, test } from "bun:test";
|
|
2
|
+
import { collectServices } from "../src/server/plugins";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Can one plugin use a database another plugin opened?
|
|
6
|
+
*
|
|
7
|
+
* The SQL plugin owns the connection and publishes it as a service. A second
|
|
8
|
+
* plugin — jobs, a rate limiter, anything that needs to persist something —
|
|
9
|
+
* wants that same connection rather than opening its own pool and splitting
|
|
10
|
+
* the budget again.
|
|
11
|
+
*
|
|
12
|
+
* As things stand `services()` is called with no arguments, so a plugin can
|
|
13
|
+
* only ever see what it produced itself. sekai-viewer works around this with
|
|
14
|
+
* an `inject` plugin that re-publishes the client, which means the ordering
|
|
15
|
+
* of that plugin in the array is load-bearing and nothing says so.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
type AnyPlugin = Parameters<typeof collectServices>[0][number];
|
|
19
|
+
|
|
20
|
+
function sqlPlugin(key = "db") {
|
|
21
|
+
return {
|
|
22
|
+
name: "sql",
|
|
23
|
+
config: () => ({}),
|
|
24
|
+
services: () => ({ [key]: { tag: "client" } }),
|
|
25
|
+
} as unknown as AnyPlugin;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("plugin services", () => {
|
|
29
|
+
test("a later plugin can read what an earlier one published", async () => {
|
|
30
|
+
const consumer = {
|
|
31
|
+
name: "consumer",
|
|
32
|
+
// The whole point: use the client instead of opening a second pool.
|
|
33
|
+
services: async (ctx: { services: Record<string, unknown> }) => ({
|
|
34
|
+
seen: Boolean(ctx.services.db),
|
|
35
|
+
}),
|
|
36
|
+
} as unknown as AnyPlugin;
|
|
37
|
+
|
|
38
|
+
const collected = await collectServices([sqlPlugin(), consumer]);
|
|
39
|
+
|
|
40
|
+
expect(collected.seen).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("what an earlier plugin published is handed to the consumer", async () => {
|
|
44
|
+
const received: Record<string, unknown> = {};
|
|
45
|
+
const consumer = {
|
|
46
|
+
name: "consumer",
|
|
47
|
+
services: (ctx: { services: Record<string, unknown> }) => {
|
|
48
|
+
Object.assign(received, ctx.services);
|
|
49
|
+
return {};
|
|
50
|
+
},
|
|
51
|
+
} as unknown as AnyPlugin;
|
|
52
|
+
|
|
53
|
+
await collectServices([sqlPlugin("db"), consumer]);
|
|
54
|
+
|
|
55
|
+
expect(received.db).toEqual({ tag: "client" });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("the client a consumer reads is the same object the sql plugin owns", async () => {
|
|
59
|
+
const client = { tag: "client" };
|
|
60
|
+
const owner = { name: "sql", services: () => ({ db: client }) } as unknown as AnyPlugin;
|
|
61
|
+
const consumer = {
|
|
62
|
+
name: "consumer",
|
|
63
|
+
services: (ctx: { services: Record<string, unknown> }) => ({ used: ctx.services.db }),
|
|
64
|
+
} as unknown as AnyPlugin;
|
|
65
|
+
|
|
66
|
+
const collected = await collectServices([owner, consumer]);
|
|
67
|
+
|
|
68
|
+
// Identity, not equality: opening a second pool to compare is the
|
|
69
|
+
// thing this is meant to prevent.
|
|
70
|
+
expect(collected.used).toBe(client);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("a consumer placed before the sql plugin sees nothing, rather than throwing", async () => {
|
|
74
|
+
// Order-independence is the point. A plugin must not have to be
|
|
75
|
+
// registered after the plugin it depends on to be correct.
|
|
76
|
+
const consumer = {
|
|
77
|
+
name: "consumer",
|
|
78
|
+
services: (ctx: { services: Record<string, unknown> }) => ({
|
|
79
|
+
seen: Boolean(ctx.services.db),
|
|
80
|
+
}),
|
|
81
|
+
} as unknown as AnyPlugin;
|
|
82
|
+
|
|
83
|
+
const collected = await collectServices([consumer, sqlPlugin()]);
|
|
84
|
+
|
|
85
|
+
expect(collected.seen).toBe(false);
|
|
86
|
+
expect(collected.db).toEqual({ tag: "client" });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a plugin that ignores the context still works", async () => {
|
|
90
|
+
const solo = {
|
|
91
|
+
name: "solo",
|
|
92
|
+
services: () => ({ a: 1 }),
|
|
93
|
+
} as unknown as AnyPlugin;
|
|
94
|
+
|
|
95
|
+
expect(await collectServices([solo])).toEqual({ a: 1 });
|
|
96
|
+
});
|
|
97
|
+
});
|
package/test/preload.test.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
3
|
-
chunksFor,
|
|
4
|
-
parseChunkGraph,
|
|
5
|
-
preloadLinks,
|
|
6
|
-
resetChunkGraph,
|
|
7
|
-
} from "../src/rendering/preload.ts";
|
|
8
|
-
|
|
9
|
-
// the shape Bun writes into hydrate.js for a split build
|
|
10
|
-
const SOURCE = `var x = 1;
|
|
11
|
-
__chunks(import.meta.url,["hy","home","cards","shared","leaf"],[["./hydrate.js",3],["./chunk-home.js",3],["./chunk-cards.js",3,4],["./chunk-shared.js"],["./chunk-leaf.js",3]],0);
|
|
12
|
-
var routes = {
|
|
13
|
-
"/": { __lazy: () => (__preload("home"), import("./chunk-home.js")) },
|
|
14
|
-
"/cards/:id": { __lazy: () => (__preload("cards"), import("./chunk-cards.js")) },
|
|
15
|
-
};`;
|
|
16
|
-
|
|
17
|
-
afterEach(() => resetChunkGraph());
|
|
18
|
-
|
|
19
|
-
describe("chunk graph", () => {
|
|
20
|
-
test("routes map to chunks and closures include shared imports", () => {
|
|
21
|
-
const g = parseChunkGraph(SOURCE)!;
|
|
22
|
-
expect(g.routes.get("/")).toBe("home");
|
|
23
|
-
expect(chunksFor(g, "/").sort()).toEqual(["chunk-home.js", "chunk-shared.js"]);
|
|
24
|
-
expect(chunksFor(g, "/cards/:id").sort()).toEqual([
|
|
25
|
-
"chunk-cards.js",
|
|
26
|
-
"chunk-leaf.js",
|
|
27
|
-
"chunk-shared.js",
|
|
28
|
-
]);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("hydrate.js itself is never listed, unknown routes give nothing", () => {
|
|
32
|
-
const g = parseChunkGraph(SOURCE)!;
|
|
33
|
-
expect(chunksFor(g, "/cards/:id")).not.toContain("hydrate.js");
|
|
34
|
-
expect(chunksFor(g, "/missing")).toEqual([]);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test("output without a chunk graph parses to null", () => {
|
|
38
|
-
expect(parseChunkGraph("console.log(1)")).toBeNull();
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
describe("preloadLinks", () => {
|
|
43
|
-
test("is empty in dev and when no build exists", () => {
|
|
44
|
-
expect(preloadLinks("/", "", true)).toBe("");
|
|
45
|
-
// cwd here has no .grimoire/client/hydrate.js
|
|
46
|
-
expect(preloadLinks("/", "")).toBe("");
|
|
47
|
-
});
|
|
48
|
-
});
|
|
1
|
+
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
chunksFor,
|
|
4
|
+
parseChunkGraph,
|
|
5
|
+
preloadLinks,
|
|
6
|
+
resetChunkGraph,
|
|
7
|
+
} from "../src/rendering/preload.ts";
|
|
8
|
+
|
|
9
|
+
// the shape Bun writes into hydrate.js for a split build
|
|
10
|
+
const SOURCE = `var x = 1;
|
|
11
|
+
__chunks(import.meta.url,["hy","home","cards","shared","leaf"],[["./hydrate.js",3],["./chunk-home.js",3],["./chunk-cards.js",3,4],["./chunk-shared.js"],["./chunk-leaf.js",3]],0);
|
|
12
|
+
var routes = {
|
|
13
|
+
"/": { __lazy: () => (__preload("home"), import("./chunk-home.js")) },
|
|
14
|
+
"/cards/:id": { __lazy: () => (__preload("cards"), import("./chunk-cards.js")) },
|
|
15
|
+
};`;
|
|
16
|
+
|
|
17
|
+
afterEach(() => resetChunkGraph());
|
|
18
|
+
|
|
19
|
+
describe("chunk graph", () => {
|
|
20
|
+
test("routes map to chunks and closures include shared imports", () => {
|
|
21
|
+
const g = parseChunkGraph(SOURCE)!;
|
|
22
|
+
expect(g.routes.get("/")).toBe("home");
|
|
23
|
+
expect(chunksFor(g, "/").sort()).toEqual(["chunk-home.js", "chunk-shared.js"]);
|
|
24
|
+
expect(chunksFor(g, "/cards/:id").sort()).toEqual([
|
|
25
|
+
"chunk-cards.js",
|
|
26
|
+
"chunk-leaf.js",
|
|
27
|
+
"chunk-shared.js",
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("hydrate.js itself is never listed, unknown routes give nothing", () => {
|
|
32
|
+
const g = parseChunkGraph(SOURCE)!;
|
|
33
|
+
expect(chunksFor(g, "/cards/:id")).not.toContain("hydrate.js");
|
|
34
|
+
expect(chunksFor(g, "/missing")).toEqual([]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("output without a chunk graph parses to null", () => {
|
|
38
|
+
expect(parseChunkGraph("console.log(1)")).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("preloadLinks", () => {
|
|
43
|
+
test("is empty in dev and when no build exists", () => {
|
|
44
|
+
expect(preloadLinks("/", "", true)).toBe("");
|
|
45
|
+
// cwd here has no .grimoire/client/hydrate.js
|
|
46
|
+
expect(preloadLinks("/", "")).toBe("");
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
mkdir,
|
|
4
|
+
mkdtemp,
|
|
5
|
+
readFile,
|
|
6
|
+
rm,
|
|
7
|
+
stat,
|
|
8
|
+
writeFile,
|
|
9
|
+
} from "node:fs/promises";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import type { AdapterContext } from "../src/adapters/types";
|
|
13
|
+
import { stripBase } from "../src/base";
|
|
14
|
+
import type { RouteFile, RouteTree } from "../src/routing/scanner";
|
|
15
|
+
import {
|
|
16
|
+
dataFileFor,
|
|
17
|
+
extractLinks,
|
|
18
|
+
fillPattern,
|
|
19
|
+
htmlFileFor,
|
|
20
|
+
markStatic,
|
|
21
|
+
prerenderSite,
|
|
22
|
+
} from "../src/server/prerender";
|
|
23
|
+
|
|
24
|
+
describe("fillPattern", () => {
|
|
25
|
+
test("static, param, optional and rest segments", () => {
|
|
26
|
+
expect(fillPattern("/", {})).toBe("/");
|
|
27
|
+
expect(fillPattern("/blog/:slug", { slug: "a b" })).toBe("/blog/a%20b");
|
|
28
|
+
expect(fillPattern("/:lang?/about", {})).toBe("/about");
|
|
29
|
+
expect(fillPattern("/:lang?/about", { lang: "de" })).toBe("/de/about");
|
|
30
|
+
expect(fillPattern("/docs/*rest", { rest: "a/b c" })).toBe("/docs/a/b%20c");
|
|
31
|
+
expect(fillPattern("/docs/*rest", { rest: "" })).toBe("/docs");
|
|
32
|
+
});
|
|
33
|
+
test("null when a required param is missing", () => {
|
|
34
|
+
expect(fillPattern("/blog/:slug", {})).toBeNull();
|
|
35
|
+
expect(fillPattern("/blog/:slug", { slug: "" })).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("file names", () => {
|
|
40
|
+
test("pages become directory indexes", () => {
|
|
41
|
+
expect(htmlFileFor("/")).toBe("index.html");
|
|
42
|
+
expect(htmlFileFor("/about")).toBe("about/index.html");
|
|
43
|
+
expect(htmlFileFor("/a/b/")).toBe("a/b/index.html");
|
|
44
|
+
expect(htmlFileFor("/legacy.html")).toBe("legacy.html");
|
|
45
|
+
});
|
|
46
|
+
test("data files sit beside the page", () => {
|
|
47
|
+
expect(dataFileFor("/")).toBe("__data.json");
|
|
48
|
+
expect(dataFileFor("/about")).toBe("about/__data.json");
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("extractLinks", () => {
|
|
53
|
+
const page = [
|
|
54
|
+
`<a href="/a">a</a> <a href='/b/?x=1#top'>b</a> <a href="https://x.dev/c">no</a>`,
|
|
55
|
+
`<a href="http://localhost/d">d</a> <a href="//cdn/e">no</a> <a href="/feed.xml">no</a>`,
|
|
56
|
+
`<a href="/page.html">p</a> <a href="#frag">no</a> <a href="mailto:x@y">no</a> <link href="/a">`,
|
|
57
|
+
].join("\n");
|
|
58
|
+
test("same-origin page paths only, normalised and de-duplicated", () => {
|
|
59
|
+
expect(extractLinks(page, "http://localhost").sort()).toEqual([
|
|
60
|
+
"/a",
|
|
61
|
+
"/b",
|
|
62
|
+
"/d",
|
|
63
|
+
"/page.html",
|
|
64
|
+
]);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("markStatic", () => {
|
|
69
|
+
test("goes in the head, or in front when there is none", () => {
|
|
70
|
+
expect(markStatic("<html><head><title>x</title></head></html>")).toContain(
|
|
71
|
+
"</script></head>",
|
|
72
|
+
);
|
|
73
|
+
expect(markStatic("<p>hi</p>").startsWith("<script>")).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
// prerenderSite against a stand-in app
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
|
|
81
|
+
const route = (
|
|
82
|
+
path: string,
|
|
83
|
+
type: RouteFile["type"],
|
|
84
|
+
params: string[] = [],
|
|
85
|
+
): RouteFile => ({
|
|
86
|
+
path,
|
|
87
|
+
filePath: `/app/src/routes${path === "/" ? "" : path}/+${type}.tsx`,
|
|
88
|
+
clientPath: `/src/routes${path === "/" ? "" : path}/+${type}.tsx`,
|
|
89
|
+
type,
|
|
90
|
+
paramNames: params,
|
|
91
|
+
optionalParamNames: [],
|
|
92
|
+
restParamNames: [],
|
|
93
|
+
paramMatchers: {},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const html = (body: string) =>
|
|
97
|
+
new Response(`<html><head></head><body>${body}</body></html>`, {
|
|
98
|
+
headers: { "content-type": "text/html" },
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
let dirs: string[] = [];
|
|
102
|
+
afterEach(async () => {
|
|
103
|
+
for (const d of dirs) await rm(d, { recursive: true, force: true });
|
|
104
|
+
dirs = [];
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
async function setup(opts: {
|
|
108
|
+
routes: RouteFile[];
|
|
109
|
+
servers?: RouteFile[];
|
|
110
|
+
modules?: Record<string, Record<string, unknown>>;
|
|
111
|
+
pages: Record<string, () => Response>;
|
|
112
|
+
strict?: boolean;
|
|
113
|
+
matchers?: Record<string, (v: string) => boolean>;
|
|
114
|
+
base?: string;
|
|
115
|
+
}) {
|
|
116
|
+
const root = await mkdtemp(join(tmpdir(), "sigil-prerender-"));
|
|
117
|
+
dirs.push(root);
|
|
118
|
+
await mkdir(join(root, "public"), { recursive: true });
|
|
119
|
+
await writeFile(join(root, "public", "robots.txt"), "ok");
|
|
120
|
+
const tree = {
|
|
121
|
+
routes: opts.routes,
|
|
122
|
+
layouts: [],
|
|
123
|
+
servers: opts.servers ?? [],
|
|
124
|
+
errors: [],
|
|
125
|
+
paramMatcherFiles: [],
|
|
126
|
+
} as RouteTree;
|
|
127
|
+
const app = {
|
|
128
|
+
tree,
|
|
129
|
+
matchers: opts.matchers,
|
|
130
|
+
config: { base: opts.base },
|
|
131
|
+
fetch: async (req: Request) => {
|
|
132
|
+
let path = new URL(req.url).pathname;
|
|
133
|
+
// what the real app does under a base: strip it, 404 outside it
|
|
134
|
+
if (opts.base) {
|
|
135
|
+
const route = stripBase(path, opts.base);
|
|
136
|
+
if (route === null) return new Response("nf", { status: 404 });
|
|
137
|
+
path = route;
|
|
138
|
+
}
|
|
139
|
+
if (req.headers.get("x-grimoire-navigate")) {
|
|
140
|
+
return new Response(JSON.stringify({ pattern: path }), {
|
|
141
|
+
headers: { "content-type": "application/json" },
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
if (path === "/__grimoire_not_found__") {
|
|
145
|
+
return new Response("<html><head></head>nope</html>", {
|
|
146
|
+
status: 404,
|
|
147
|
+
headers: { "content-type": "text/html" },
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
const make = opts.pages[path];
|
|
151
|
+
return make ? make() : new Response("nf", { status: 404 });
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
const warnings: string[] = [];
|
|
155
|
+
const ctx = {
|
|
156
|
+
app,
|
|
157
|
+
root,
|
|
158
|
+
config: {},
|
|
159
|
+
plugins: [],
|
|
160
|
+
log: { info() {}, warn: (m: string) => warnings.push(m) },
|
|
161
|
+
} as unknown as AdapterContext;
|
|
162
|
+
const load = async (p: string) => opts.modules?.[p] ?? {};
|
|
163
|
+
return {
|
|
164
|
+
root,
|
|
165
|
+
warnings,
|
|
166
|
+
run: () => prerenderSite(ctx, { out: "build", strict: opts.strict }, load),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const exists = (p: string) =>
|
|
171
|
+
stat(p).then(
|
|
172
|
+
() => true,
|
|
173
|
+
() => false,
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
describe("prerenderSite", () => {
|
|
177
|
+
test("writes pages, data files, public/ and 404.html; crawls links", async () => {
|
|
178
|
+
const t = await setup({
|
|
179
|
+
routes: [
|
|
180
|
+
route("/", "page"),
|
|
181
|
+
route("/about", "page"),
|
|
182
|
+
route("/blog/:slug", "page", ["slug"]),
|
|
183
|
+
],
|
|
184
|
+
pages: {
|
|
185
|
+
"/": () =>
|
|
186
|
+
html('<a href="/about">about</a><a href="/blog/hello">post</a>'),
|
|
187
|
+
"/about": () => html("about"),
|
|
188
|
+
"/blog/hello": () => html("hello"),
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
const report = await t.run();
|
|
192
|
+
expect(report.pages).toEqual(["/", "/about", "/blog/hello"]);
|
|
193
|
+
const out = join(t.root, "build");
|
|
194
|
+
expect(await readFile(join(out, "about/index.html"), "utf8")).toContain(
|
|
195
|
+
"__grimoire_static__=1",
|
|
196
|
+
);
|
|
197
|
+
expect(
|
|
198
|
+
await readFile(join(out, "blog/hello/__data.json"), "utf8"),
|
|
199
|
+
).toContain("pattern");
|
|
200
|
+
expect(await readFile(join(out, "robots.txt"), "utf8")).toBe("ok");
|
|
201
|
+
expect(await exists(join(out, "404.html"))).toBe(true);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("under a base: requests and links carry it, files do not", async () => {
|
|
205
|
+
const t = await setup({
|
|
206
|
+
base: "/repo",
|
|
207
|
+
routes: [route("/", "page"), route("/about", "page"), route("/gone", "page")],
|
|
208
|
+
pages: {
|
|
209
|
+
"/": () => html('<a href="/repo/about">about</a><a href="/about">unprefixed</a>'),
|
|
210
|
+
"/about": () => html("about"),
|
|
211
|
+
"/gone": () => html("never linked, so only a seed finds it"),
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
const report = await t.run();
|
|
215
|
+
// "/about" without the base is outside it, so it was not followed on its own
|
|
216
|
+
expect(report.pages).toContain("/about");
|
|
217
|
+
const out = join(t.root, "build");
|
|
218
|
+
expect(await exists(join(out, "about/index.html"))).toBe(true);
|
|
219
|
+
expect(await exists(join(out, "repo"))).toBe(false);
|
|
220
|
+
expect(await readFile(join(out, "about/__data.json"), "utf8")).toContain("pattern");
|
|
221
|
+
expect(await exists(join(out, "404.html"))).toBe(true);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("entries() supplies params for a dynamic route", async () => {
|
|
225
|
+
const dyn = route("/blog/:slug", "page", ["slug"]);
|
|
226
|
+
const t = await setup({
|
|
227
|
+
routes: [dyn],
|
|
228
|
+
modules: {
|
|
229
|
+
[dyn.filePath]: { entries: () => [{ slug: "a" }, { slug: "b" }] },
|
|
230
|
+
},
|
|
231
|
+
pages: { "/blog/a": () => html("a"), "/blog/b": () => html("b") },
|
|
232
|
+
});
|
|
233
|
+
expect((await t.run()).pages).toEqual(["/blog/a", "/blog/b"]);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("a [param=matcher] route is reached through its matcher", async () => {
|
|
237
|
+
const potion = {
|
|
238
|
+
...route("/potions/:index", "page", ["index"]),
|
|
239
|
+
paramMatchers: { index: "int" },
|
|
240
|
+
};
|
|
241
|
+
const t = await setup({
|
|
242
|
+
routes: [route("/", "page"), potion],
|
|
243
|
+
matchers: { int: (v) => /^\d+$/.test(v) },
|
|
244
|
+
modules: { [potion.filePath]: { entries: () => [{ index: "3" }] } },
|
|
245
|
+
pages: {
|
|
246
|
+
"/": () => html('<a href="/potions/7">p</a><a href="/potions/x">bad</a>'),
|
|
247
|
+
"/potions/3": () => html("three"),
|
|
248
|
+
"/potions/7": () => html("seven"),
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
const report = await t.run();
|
|
252
|
+
expect(report.pages).toEqual(["/", "/potions/3", "/potions/7"]);
|
|
253
|
+
expect(report.problems).toEqual([]);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("a dynamic route nothing reaches fails the build, naming the fix", async () => {
|
|
257
|
+
const t = await setup({
|
|
258
|
+
routes: [route("/blog/:slug", "page", ["slug"])],
|
|
259
|
+
pages: {},
|
|
260
|
+
});
|
|
261
|
+
await expect(t.run()).rejects.toThrow(/never rendered[\s\S]*entries\(\)/);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("prerender = false, actions and plain endpoints each fail", async () => {
|
|
265
|
+
const off = route("/off", "page");
|
|
266
|
+
const form = route("/form", "page");
|
|
267
|
+
const formServer = route("/form", "pageServer");
|
|
268
|
+
const api = route("/api/x", "server");
|
|
269
|
+
const t = await setup({
|
|
270
|
+
routes: [off, form, formServer],
|
|
271
|
+
servers: [api],
|
|
272
|
+
modules: {
|
|
273
|
+
[off.filePath]: { prerender: false },
|
|
274
|
+
[formServer.filePath]: { load() {}, create() {} },
|
|
275
|
+
[api.filePath]: { GET() {} },
|
|
276
|
+
},
|
|
277
|
+
pages: {},
|
|
278
|
+
});
|
|
279
|
+
const err = await t.run().then(
|
|
280
|
+
() => null,
|
|
281
|
+
(e: Error) => e,
|
|
282
|
+
);
|
|
283
|
+
expect(err?.message).toContain("sets prerender = false");
|
|
284
|
+
expect(err?.message).toContain("server actions (create)");
|
|
285
|
+
expect(err?.message).toContain("server endpoint");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test("strict: false warns and skips instead of failing", async () => {
|
|
289
|
+
const off = route("/off", "page");
|
|
290
|
+
const t = await setup({
|
|
291
|
+
routes: [route("/", "page"), off],
|
|
292
|
+
modules: { [off.filePath]: { prerender: false } },
|
|
293
|
+
pages: { "/": () => html("home") },
|
|
294
|
+
strict: false,
|
|
295
|
+
});
|
|
296
|
+
const report = await t.run();
|
|
297
|
+
expect(report.pages).toEqual(["/"]);
|
|
298
|
+
expect(t.warnings.join()).toContain("prerender = false");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("a GET endpoint with prerender = true and an extension becomes a file", async () => {
|
|
302
|
+
const sitemap = route("/sitemap.xml", "server");
|
|
303
|
+
const t = await setup({
|
|
304
|
+
routes: [],
|
|
305
|
+
servers: [sitemap],
|
|
306
|
+
modules: { [sitemap.filePath]: { prerender: true, GET() {} } },
|
|
307
|
+
pages: {
|
|
308
|
+
"/sitemap.xml": () =>
|
|
309
|
+
new Response("<urlset/>", {
|
|
310
|
+
headers: { "content-type": "application/xml" },
|
|
311
|
+
}),
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
await t.run();
|
|
315
|
+
expect(await readFile(join(t.root, "build/sitemap.xml"), "utf8")).toBe(
|
|
316
|
+
"<urlset/>",
|
|
317
|
+
);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("a redirect becomes a meta-refresh page", async () => {
|
|
321
|
+
const t = await setup({
|
|
322
|
+
routes: [route("/old", "page"), route("/new", "page")],
|
|
323
|
+
pages: {
|
|
324
|
+
"/old": () =>
|
|
325
|
+
new Response(null, { status: 302, headers: { location: "/new" } }),
|
|
326
|
+
"/new": () => html("n"),
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
await t.run();
|
|
330
|
+
expect(
|
|
331
|
+
await readFile(join(t.root, "build/old/index.html"), "utf8"),
|
|
332
|
+
).toContain("url=/new");
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test("refuses an out directory that would delete the project", async () => {
|
|
336
|
+
const root = await mkdtemp(join(tmpdir(), "sigil-prerender-"));
|
|
337
|
+
dirs.push(root);
|
|
338
|
+
const ctx = {
|
|
339
|
+
app: {
|
|
340
|
+
tree: { routes: [], layouts: [], servers: [], errors: [] },
|
|
341
|
+
config: {},
|
|
342
|
+
fetch: async () => new Response(),
|
|
343
|
+
},
|
|
344
|
+
root,
|
|
345
|
+
log: { info() {}, warn() {} },
|
|
346
|
+
} as unknown as AdapterContext;
|
|
347
|
+
for (const out of [".", "src", "public", "..", "node_modules/x"]) {
|
|
348
|
+
await expect(prerenderSite(ctx, { out })).rejects.toThrow(
|
|
349
|
+
/adapter-static/,
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
});
|