@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10
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/dist/action-context.js +1 -1
- package/dist/actions.d.ts +7 -0
- package/dist/actions.js +34 -10
- package/dist/after.d.ts +28 -0
- package/dist/after.js +48 -0
- package/dist/assets-static.d.ts +10 -1
- package/dist/assets-static.js +10 -2
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +20 -0
- package/dist/boundary.js +1 -1
- package/dist/builtin-tags.d.ts +12 -0
- package/dist/builtin-tags.js +42 -0
- package/dist/cache-adapter.js +1 -1
- package/dist/cache-scope.d.ts +8 -0
- package/dist/cache-scope.js +14 -1
- package/dist/cloudflare-workers.d.ts +2 -0
- package/dist/cloudflare-workers.js +27 -0
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/dev-overlay.d.ts +54 -3
- package/dist/dev-overlay.js +496 -91
- package/dist/dom.d.ts +4 -0
- package/dist/dom.js +62 -16
- package/dist/effects.js +43 -10
- package/dist/events.d.ts +2 -0
- package/dist/events.js +18 -0
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/handler-html.js +4 -4
- package/dist/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +9 -190
- package/dist/handler-segments.d.ts +6 -0
- package/dist/handler-segments.js +18 -4
- package/dist/head-tags.d.ts +9 -0
- package/dist/head-tags.js +93 -6
- package/dist/highlight-bind.js +1 -1
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +17 -12
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +147 -74
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/island-swap.d.ts +47 -0
- package/dist/island-swap.js +635 -0
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-types.d.ts +99 -3
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +407 -80
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-classes.js +1 -1
- package/dist/krate-f-highlight.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +455 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- package/dist/krate-runtime.js +5 -5
- package/dist/krate-suspense.js +1 -1
- package/dist/list.d.ts +14 -4
- package/dist/list.js +30 -23
- package/dist/metadata.js +5 -2
- package/dist/navigation-react.d.ts +13 -0
- package/dist/navigation-react.js +86 -0
- package/dist/navigation.d.ts +19 -0
- package/dist/navigation.js +35 -0
- package/dist/node-server.js +3 -3
- package/dist/og.js +1 -1
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +57 -0
- package/dist/react-compat.d.ts +39 -0
- package/dist/react-compat.js +359 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/region-wire.d.ts +10 -0
- package/dist/region-wire.js +154 -0
- package/dist/request-context.js +1 -1
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/resp.js +1 -1
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router-lite.js +3 -9
- package/dist/router.d.ts +1 -1
- package/dist/router.js +675 -100
- package/dist/server-handler.d.ts +69 -1
- package/dist/server-handler.js +515 -96
- package/dist/server-renderer.js +16 -7
- package/dist/server.d.ts +73 -16
- package/dist/server.js +619 -102
- package/dist/sha256.js +9 -9
- package/dist/signal.js +47 -20
- package/dist/site-config.d.ts +15 -0
- package/dist/site-config.js +28 -0
- package/dist/slot.d.ts +12 -0
- package/dist/slot.js +23 -0
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +12 -4
- package/dist/suspense.js +34 -2
- package/dist/testing.d.ts +62 -0
- package/dist/testing.js +90 -0
- package/dist/use-id.js +1 -1
- package/package.json +28 -3
- package/dist/action-context.test.d.ts +0 -1
- package/dist/action-context.test.js +0 -72
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -477
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -244
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -233
- package/dist/class-component.test.d.ts +0 -1
- package/dist/class-component.test.js +0 -462
- package/dist/class-helpers.test.d.ts +0 -1
- package/dist/class-helpers.test.js +0 -169
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -57
- package/dist/derived.test.d.ts +0 -1
- package/dist/derived.test.js +0 -140
- package/dist/dev-overlay.test.d.ts +0 -1
- package/dist/dev-overlay.test.js +0 -152
- package/dist/discrete.test.d.ts +0 -1
- package/dist/discrete.test.js +0 -100
- package/dist/dom.test.d.ts +0 -1
- package/dist/dom.test.js +0 -31
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -163
- package/dist/effects.test.d.ts +0 -1
- package/dist/effects.test.js +0 -485
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -447
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -256
- package/dist/handler-routing.test.d.ts +0 -1
- package/dist/handler-routing.test.js +0 -99
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -217
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -131
- package/dist/highlight-bind.test.d.ts +0 -1
- package/dist/highlight-bind.test.js +0 -124
- package/dist/highlight.test.d.ts +0 -1
- package/dist/highlight.test.js +0 -56
- package/dist/lanes.test.d.ts +0 -1
- package/dist/lanes.test.js +0 -59
- package/dist/list.test.d.ts +0 -1
- package/dist/list.test.js +0 -344
- package/dist/metadata.test.d.ts +0 -1
- package/dist/metadata.test.js +0 -167
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.js +0 -130
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -492
- package/dist/optimistic-lanes.test.d.ts +0 -1
- package/dist/optimistic-lanes.test.js +0 -45
- package/dist/patterns.test.d.ts +0 -1
- package/dist/patterns.test.js +0 -98
- package/dist/portal.test.d.ts +0 -1
- package/dist/portal.test.js +0 -99
- package/dist/react-effects.test.d.ts +0 -1
- package/dist/react-effects.test.js +0 -96
- package/dist/react-suspense.test.d.ts +0 -1
- package/dist/react-suspense.test.js +0 -194
- package/dist/react.test.d.ts +0 -1
- package/dist/react.test.js +0 -60
- package/dist/react19.test.d.ts +0 -1
- package/dist/react19.test.js +0 -176
- package/dist/reactivity-core.test.d.ts +0 -1
- package/dist/reactivity-core.test.js +0 -152
- package/dist/reconcile.test.d.ts +0 -1
- package/dist/reconcile.test.js +0 -110
- package/dist/refs.test.d.ts +0 -1
- package/dist/refs.test.js +0 -257
- package/dist/router-layout.test.d.ts +0 -1
- package/dist/router-layout.test.js +0 -249
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -464
- package/dist/router-slots.test.d.ts +0 -1
- package/dist/router-slots.test.js +0 -144
- package/dist/router-speculation.test.d.ts +0 -1
- package/dist/router-speculation.test.js +0 -25
- package/dist/router-styles.test.d.ts +0 -1
- package/dist/router-styles.test.js +0 -98
- package/dist/router.test.d.ts +0 -1
- package/dist/router.test.js +0 -36
- package/dist/server-async.test.d.ts +0 -1
- package/dist/server-async.test.js +0 -216
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -535
- package/dist/server-highlight.test.d.ts +0 -1
- package/dist/server-highlight.test.js +0 -32
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -266
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -88
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal-lanes.test.d.ts +0 -1
- package/dist/signal-lanes.test.js +0 -32
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/slots.test.d.ts +0 -1
- package/dist/slots.test.js +0 -169
- package/dist/snapshots.test.d.ts +0 -1
- package/dist/snapshots.test.js +0 -208
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -620
- package/dist/ssr-core.test.d.ts +0 -1
- package/dist/ssr-core.test.js +0 -324
- package/dist/static-memory.test.d.ts +0 -1
- package/dist/static-memory.test.js +0 -26
- package/dist/storage.test.d.ts +0 -1
- package/dist/storage.test.js +0 -46
- package/dist/suspense-actions.test.d.ts +0 -1
- package/dist/suspense-actions.test.js +0 -72
- package/dist/suspense.test.d.ts +0 -1
- package/dist/suspense.test.js +0 -518
|
@@ -1,620 +0,0 @@
|
|
|
1
|
-
// @vitest-environment node
|
|
2
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
-
import { createHash } from "node:crypto";
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
-
import { createSSRCore, variantKey } from "./ssr-core.js";
|
|
6
|
-
import { cacheRuntime, installRenderStore, markNoStore, patchFetch, recordCacheRead } from "./cache-scope.js";
|
|
7
|
-
import { forbidden, notFound, unauthorized } from "./request-context.js";
|
|
8
|
-
import { jsx } from "./server.js";
|
|
9
|
-
// The renderer installs the cache scope store once per process; so does this file.
|
|
10
|
-
installRenderStore(new AsyncLocalStorage());
|
|
11
|
-
let errors = [];
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
errors = [];
|
|
14
|
-
vi.spyOn(console, "error").mockImplementation((...a) => void errors.push(a));
|
|
15
|
-
vi.spyOn(console, "log").mockImplementation(() => { });
|
|
16
|
-
});
|
|
17
|
-
afterEach(() => {
|
|
18
|
-
vi.restoreAllMocks();
|
|
19
|
-
vi.useRealTimers();
|
|
20
|
-
});
|
|
21
|
-
function make(s) {
|
|
22
|
-
const background = [];
|
|
23
|
-
const core = createSSRCore({
|
|
24
|
-
manifest: { pages: s.pages || [], regions: s.regions || {} },
|
|
25
|
-
loadPageModule: async (page) => {
|
|
26
|
-
const m = (s.modules || {})[page.bundlePath || page.source];
|
|
27
|
-
if (!m)
|
|
28
|
-
throw new Error("no module " + page.source);
|
|
29
|
-
return m;
|
|
30
|
-
},
|
|
31
|
-
loadRuntimeRenderer: (rel) => {
|
|
32
|
-
const r = (s.runtime || {})[rel];
|
|
33
|
-
if (!r)
|
|
34
|
-
throw new Error("no runtime bundle " + rel);
|
|
35
|
-
return r;
|
|
36
|
-
},
|
|
37
|
-
background: (p) => void background.push(p),
|
|
38
|
-
...s.opts,
|
|
39
|
-
});
|
|
40
|
-
return { core, settle: () => Promise.allSettled(background.splice(0)) };
|
|
41
|
-
}
|
|
42
|
-
const req = (route, extra = {}) => ({ route, url: route, method: "GET", headers: {}, ...extra });
|
|
43
|
-
const page = (route, mode = "ssr", extra = {}) => ({ route, source: route + ".tsx", mode, bundlePath: route, ...extra });
|
|
44
|
-
const text = (s) => () => jsx("p", { children: s });
|
|
45
|
-
describe("variantKey", () => {
|
|
46
|
-
it("sorts and encodes params and query, and keeps them apart", () => {
|
|
47
|
-
expect(variantKey({ route: "/r" })).toBe("/r");
|
|
48
|
-
expect(variantKey({ route: "/r", params: { b: "2", a: "1 x" } })).toBe("/r?a=1%20x&b=2");
|
|
49
|
-
expect(variantKey({ route: "/r", query: { q: "a&b=c" } })).toBe("/r#q=a%26b%3Dc");
|
|
50
|
-
// a param and a query value with the same name are different variants
|
|
51
|
-
expect(variantKey({ route: "/r", params: { a: "1" } })).not.toBe(variantKey({ route: "/r", query: { a: "1" } }));
|
|
52
|
-
expect(variantKey({ route: "/r", params: { a: "1" }, query: { a: "1" } })).toBe("/r?a=1#a=1");
|
|
53
|
-
});
|
|
54
|
-
it("gives two dynamic params two keys", () => {
|
|
55
|
-
expect(variantKey({ route: "/v/[id]", params: { id: "a" } })).not.toBe(variantKey({ route: "/v/[id]", params: { id: "b" } }));
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
describe("generateMetadata in a page render", () => {
|
|
59
|
-
const body = (s) => () => jsx("p", { children: s });
|
|
60
|
-
it("puts the tags in front of the html as a head block, merged with the layouts the build bundled", async () => {
|
|
61
|
-
const { core } = make({
|
|
62
|
-
pages: [page("/m/[id]")],
|
|
63
|
-
modules: {
|
|
64
|
-
"/m/[id]": {
|
|
65
|
-
default: body("hi"),
|
|
66
|
-
async generateMetadata({ params }, parent) {
|
|
67
|
-
const { id } = await params;
|
|
68
|
-
return { title: "Item " + id, description: String((await parent).description) };
|
|
69
|
-
},
|
|
70
|
-
__krateMetadata: [{ file: "_layout.tsx", mod: { metadata: { title: { default: "Site", template: "%s | Site" }, description: "layout" } } }],
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
const r = await core.renderPage(req("/m/[id]", { params: { id: "7" } }));
|
|
75
|
-
expect(r.status).toBe(200);
|
|
76
|
-
expect(r.html).toBe("<!--krate-head--><title>Item 7 | Site</title>\n<meta name=\"description\" content=\"layout\"><!--/krate-head--><p>hi</p>");
|
|
77
|
-
});
|
|
78
|
-
it("is skipped when only literal metadata exists (the build bakes it)", async () => {
|
|
79
|
-
const { core } = make({ pages: [page("/m")], modules: { "/m": { default: body("hi"), metadata: { title: "T" } } } });
|
|
80
|
-
expect((await core.renderPage(req("/m"))).html).toBe("<p>hi</p>");
|
|
81
|
-
});
|
|
82
|
-
it("still renders the page when generateMetadata throws, and logs it", async () => {
|
|
83
|
-
const { core } = make({
|
|
84
|
-
pages: [page("/m")],
|
|
85
|
-
modules: { "/m": { default: body("hi"), generateMetadata: () => { throw new Error("boom"); } } },
|
|
86
|
-
});
|
|
87
|
-
const r = await core.renderPage(req("/m"));
|
|
88
|
-
expect(r).toMatchObject({ status: 200, html: "<p>hi</p>" });
|
|
89
|
-
expect(errors.some((e) => String(e[0]).includes("generateMetadata"))).toBe(true);
|
|
90
|
-
});
|
|
91
|
-
it("answers 404 for notFound() in generateMetadata and caches nothing", async () => {
|
|
92
|
-
const { core } = make({
|
|
93
|
-
pages: [page("/m", "isr", { revalidate: 60 })],
|
|
94
|
-
modules: { "/m": { default: body("hi"), generateMetadata: () => notFound() } },
|
|
95
|
-
});
|
|
96
|
-
expect(await core.renderPage(req("/m"))).toMatchObject({ status: 404, notFound: true });
|
|
97
|
-
expect(await core.renderPage(req("/m"))).toMatchObject({ status: 404, notFound: true });
|
|
98
|
-
});
|
|
99
|
-
it("caches the head block with the ISR entry and serves it on a hit", async () => {
|
|
100
|
-
let calls = 0;
|
|
101
|
-
const { core } = make({
|
|
102
|
-
pages: [page("/m", "isr", { revalidate: 60 })],
|
|
103
|
-
modules: { "/m": { default: body("hi"), generateMetadata: () => { calls++; return { title: "C" + calls }; } } },
|
|
104
|
-
});
|
|
105
|
-
const first = await core.renderPage(req("/m"));
|
|
106
|
-
const second = await core.renderPage(req("/m"));
|
|
107
|
-
expect(first.cacheStatus).toBe("miss");
|
|
108
|
-
expect(second.cacheStatus).toBe("hit");
|
|
109
|
-
expect(second.html).toBe(first.html);
|
|
110
|
-
expect(second.html).toContain("<title>C1</title>");
|
|
111
|
-
expect(calls).toBe(1);
|
|
112
|
-
});
|
|
113
|
-
it("shares the data cache with the page: one request for a URL both read", async () => {
|
|
114
|
-
let upstream = 0;
|
|
115
|
-
vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ title: "From API", n: ++upstream }), { headers: { "content-type": "application/json" } })));
|
|
116
|
-
cacheRuntime().fetchPatched = false;
|
|
117
|
-
patchFetch();
|
|
118
|
-
const read = async () => (await fetch("http://data/post/1", { next: { tags: ["post:1"], revalidate: 60 } })).json();
|
|
119
|
-
const { core } = make({
|
|
120
|
-
pages: [page("/d")],
|
|
121
|
-
modules: {
|
|
122
|
-
"/d": {
|
|
123
|
-
default: async () => jsx("p", { children: (await read()).title }),
|
|
124
|
-
generateMetadata: async () => ({ title: (await read()).title }),
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
});
|
|
128
|
-
const r = await core.renderPage(req("/d"));
|
|
129
|
-
expect(r.html).toContain("<title>From API</title>");
|
|
130
|
-
expect(r.html).toContain("<p>From API</p>");
|
|
131
|
-
expect(upstream).toBe(1);
|
|
132
|
-
vi.unstubAllGlobals();
|
|
133
|
-
cacheRuntime().fetchPatched = false;
|
|
134
|
-
});
|
|
135
|
-
it("records the cache reads of generateMetadata on the same render, so the entry is tagged and no-store holds", async () => {
|
|
136
|
-
const { core } = make({
|
|
137
|
-
pages: [page("/t", "isr", { revalidate: 60 })],
|
|
138
|
-
modules: {
|
|
139
|
-
"/t": {
|
|
140
|
-
default: body("hi"),
|
|
141
|
-
generateMetadata: () => {
|
|
142
|
-
recordCacheRead(["posts"], undefined);
|
|
143
|
-
return { title: "Tagged" };
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
expect((await core.renderPage(req("/t"))).cacheStatus).toBe("miss");
|
|
149
|
-
expect((await core.renderPage(req("/t"))).cacheStatus).toBe("hit");
|
|
150
|
-
// revalidateTag("posts") purges the entry: generateMetadata read the tag.
|
|
151
|
-
expect(await core.isrStore.deleteTags(["posts"])).toBe(1);
|
|
152
|
-
expect((await core.renderPage(req("/t"))).cacheStatus).toBe("miss");
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
describe("renderPage", () => {
|
|
156
|
-
it("answers 404 for a route that is not in the manifest", async () => {
|
|
157
|
-
const { core } = make({ pages: [page("/a")] });
|
|
158
|
-
expect(await core.renderPage(req("/zzz"))).toMatchObject({ status: 404, notFound: true, html: "" });
|
|
159
|
-
});
|
|
160
|
-
it("finds a page with or without its trailing slash, and keeps '/' intact", async () => {
|
|
161
|
-
const { core } = make({
|
|
162
|
-
pages: [page("/a/"), page("/b"), page("/")],
|
|
163
|
-
modules: { "/a/": { default: text("A") }, "/b": { default: text("B") }, "/": { default: text("root") } },
|
|
164
|
-
});
|
|
165
|
-
expect((await core.renderPage(req("/a"))).html).toBe("<p>A</p>");
|
|
166
|
-
expect((await core.renderPage(req("/b/"))).html).toBe("<p>B</p>");
|
|
167
|
-
expect((await core.renderPage(req("/"))).html).toBe("<p>root</p>");
|
|
168
|
-
expect(core.findPage("/missing/")).toBeUndefined();
|
|
169
|
-
});
|
|
170
|
-
it("escapes text and attribute values coming from the request", async () => {
|
|
171
|
-
const { core } = make({
|
|
172
|
-
pages: [page("/e")],
|
|
173
|
-
modules: {
|
|
174
|
-
"/e": {
|
|
175
|
-
default: (props) => jsx("div", { title: props.query.t, children: [jsx("p", { children: props.query.t }), jsx("p", { children: props.params.id })] }),
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
const r = await core.renderPage(req("/e", { query: { t: `"><script>alert(1)</script>` }, params: { id: "<b>&" } }));
|
|
180
|
-
expect(r.status).toBe(200);
|
|
181
|
-
expect(r.html).not.toContain("<script>");
|
|
182
|
-
expect(r.html).not.toContain("<b>");
|
|
183
|
-
expect(r.html).toContain("<script>");
|
|
184
|
-
expect(r.html).toContain("title=");
|
|
185
|
-
expect(r.html).toContain("<b>&");
|
|
186
|
-
});
|
|
187
|
-
it("hands the page awaitable params and searchParams", async () => {
|
|
188
|
-
const { core } = make({
|
|
189
|
-
pages: [page("/p/[id]")],
|
|
190
|
-
modules: {
|
|
191
|
-
"/p/[id]": {
|
|
192
|
-
default: async (props) => {
|
|
193
|
-
const { id } = await props.params;
|
|
194
|
-
const { tab } = await props.searchParams;
|
|
195
|
-
return jsx("p", { children: `${id}/${tab}/${props.params.id}/${props.searchParams.tab}` });
|
|
196
|
-
},
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
});
|
|
200
|
-
const r = await core.renderPage(req("/p/[id]", { params: { id: "7" }, query: { tab: "x" } }));
|
|
201
|
-
expect(r.html).toBe("<p>7/x/7/x</p>");
|
|
202
|
-
});
|
|
203
|
-
it("serves request headers and cookies to server components", async () => {
|
|
204
|
-
const { getRequestContext } = await import("./request-context.js");
|
|
205
|
-
const seen = [];
|
|
206
|
-
const { core } = make({
|
|
207
|
-
pages: [page("/h")],
|
|
208
|
-
modules: {
|
|
209
|
-
"/h": {
|
|
210
|
-
default: () => {
|
|
211
|
-
const c = getRequestContext();
|
|
212
|
-
seen.push(c.headers.get("x-test") + "|" + c.cookies.get("sid"));
|
|
213
|
-
return jsx("p", { children: "ok" });
|
|
214
|
-
},
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
});
|
|
218
|
-
await core.renderPage(req("/h", { headers: { "x-test": "yes", cookie: "sid=abc; other=1" } }));
|
|
219
|
-
expect(seen).toEqual(["yes|abc"]);
|
|
220
|
-
});
|
|
221
|
-
describe("failures", () => {
|
|
222
|
-
const boom = () => () => {
|
|
223
|
-
const e = new Error("boom");
|
|
224
|
-
e.stack = "at x";
|
|
225
|
-
throw e;
|
|
226
|
-
};
|
|
227
|
-
it("answers 500 with a short digest of the error, never the error text in the HTML", async () => {
|
|
228
|
-
const { core } = make({ pages: [page("/b")], modules: { "/b": { default: boom() } } });
|
|
229
|
-
const r = await core.renderPage(req("/b"));
|
|
230
|
-
expect(r.status).toBe(500);
|
|
231
|
-
expect(r.html).toBe("");
|
|
232
|
-
expect(r.error).toBe("boom");
|
|
233
|
-
expect(r.digest).toMatch(/^[0-9a-f]{10}$/);
|
|
234
|
-
expect(errors.flat().join(" ")).toContain(`digest ${r.digest}`);
|
|
235
|
-
});
|
|
236
|
-
it("computes the digest as sha256(message + newline + stack) cut to 10 hex digits, the same on every host", async () => {
|
|
237
|
-
// Pinned: build/segmentserve.go errorDigest must produce this exact value.
|
|
238
|
-
const golden = "8a2474a04d";
|
|
239
|
-
const portable = make({ pages: [page("/b")], modules: { "/b": { default: boom() } } });
|
|
240
|
-
const node = make({
|
|
241
|
-
pages: [page("/b")],
|
|
242
|
-
modules: { "/b": { default: boom() } },
|
|
243
|
-
opts: { sha256Hex: (s) => createHash("sha256").update(s).digest("hex") },
|
|
244
|
-
});
|
|
245
|
-
expect((await portable.core.renderPage(req("/b"))).digest).toBe(golden);
|
|
246
|
-
expect((await node.core.renderPage(req("/b"))).digest).toBe(golden);
|
|
247
|
-
expect(createHash("sha256").update("boom\nat x").digest("hex").slice(0, 10)).toBe(golden);
|
|
248
|
-
});
|
|
249
|
-
it("turns a missing default export into a 500", async () => {
|
|
250
|
-
const { core } = make({ pages: [page("/n")], modules: { "/n": {} } });
|
|
251
|
-
expect(await core.renderPage(req("/n"))).toMatchObject({ status: 500, html: "" });
|
|
252
|
-
});
|
|
253
|
-
it("turns a module that cannot load into a 500", async () => {
|
|
254
|
-
const { core } = make({ pages: [page("/n")], modules: {} });
|
|
255
|
-
const r = await core.renderPage(req("/n"));
|
|
256
|
-
expect(r.status).toBe(500);
|
|
257
|
-
expect(r.error).toContain("no module");
|
|
258
|
-
});
|
|
259
|
-
it("maps forbidden() to 403 and unauthorized() to 401 and never caches them", async () => {
|
|
260
|
-
const { core } = make({
|
|
261
|
-
pages: [{ ...page("/fb"), mode: "isr", revalidate: 60 }, page("/un")],
|
|
262
|
-
modules: { "/fb": { default: () => forbidden() }, "/un": { default: () => unauthorized() } },
|
|
263
|
-
});
|
|
264
|
-
expect(await core.renderPage(req("/fb"))).toMatchObject({ status: 403, denied: 403, html: "" });
|
|
265
|
-
expect(await core.renderPage(req("/fb"))).toMatchObject({ status: 403, denied: 403 });
|
|
266
|
-
expect((await core.renderPage(req("/fb"))).cacheStatus).not.toBe("hit");
|
|
267
|
-
expect(await core.renderPage(req("/un"))).toMatchObject({ status: 401, denied: 401 });
|
|
268
|
-
expect(await core.renderPage(req("/un"))).not.toHaveProperty("notFound");
|
|
269
|
-
});
|
|
270
|
-
it("maps notFound() to 404 and a redirect signal to 307", async () => {
|
|
271
|
-
const { core } = make({
|
|
272
|
-
pages: [page("/nf"), page("/rd"), page("/rd2")],
|
|
273
|
-
modules: {
|
|
274
|
-
"/nf": { default: () => notFound() },
|
|
275
|
-
"/rd": {
|
|
276
|
-
default: () => {
|
|
277
|
-
throw Object.assign(new Error("NEXT_REDIRECT"), { __krateRedirect: "/login" });
|
|
278
|
-
},
|
|
279
|
-
},
|
|
280
|
-
"/rd2": {
|
|
281
|
-
default: () => {
|
|
282
|
-
throw Object.assign(new Error("x"), { digest: "NEXT_REDIRECT;replace;/next;307" });
|
|
283
|
-
},
|
|
284
|
-
},
|
|
285
|
-
},
|
|
286
|
-
});
|
|
287
|
-
expect(await core.renderPage(req("/nf"))).toMatchObject({ status: 404, notFound: true });
|
|
288
|
-
expect(await core.renderPage(req("/rd"))).toMatchObject({ status: 307, redirect: "/login" });
|
|
289
|
-
expect(await core.renderPage(req("/rd2"))).toMatchObject({ status: 307, redirect: "/next" });
|
|
290
|
-
expect(errors).toEqual([]);
|
|
291
|
-
});
|
|
292
|
-
it("stops a render that outlives the timeout", async () => {
|
|
293
|
-
const { core } = make({
|
|
294
|
-
pages: [page("/slow")],
|
|
295
|
-
modules: { "/slow": { default: () => new Promise(() => { }) } },
|
|
296
|
-
opts: { timeoutMs: 30 },
|
|
297
|
-
});
|
|
298
|
-
const r = await core.renderPage(req("/slow"));
|
|
299
|
-
expect(r.status).toBe(500);
|
|
300
|
-
expect(r.error).toBe("render timed out after 30ms");
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
describe("ISR pages", () => {
|
|
305
|
-
function isr(extra = {}, revalidate = 10) {
|
|
306
|
-
let n = 0;
|
|
307
|
-
const s = make({
|
|
308
|
-
pages: [page("/i/[id]", "isr", { revalidate })],
|
|
309
|
-
modules: {
|
|
310
|
-
"/i/[id]": {
|
|
311
|
-
default: (props) => {
|
|
312
|
-
const q = props.searchParams.page;
|
|
313
|
-
return jsx("p", { children: `${props.params.id}:${q ?? "-"}:${++n}` });
|
|
314
|
-
},
|
|
315
|
-
},
|
|
316
|
-
},
|
|
317
|
-
opts: extra,
|
|
318
|
-
});
|
|
319
|
-
return { ...s, renders: () => n, get: (id, query) => s.core.renderPage(req("/i/[id]", { params: { id }, query })) };
|
|
320
|
-
}
|
|
321
|
-
it("reports miss, then hit, and keeps variants apart", async () => {
|
|
322
|
-
const { get, renders } = isr();
|
|
323
|
-
expect(await get("a")).toMatchObject({ cacheStatus: "miss", html: "<p>a:-:1</p>" });
|
|
324
|
-
expect(await get("a")).toMatchObject({ cacheStatus: "hit", cached: true, html: "<p>a:-:1</p>" });
|
|
325
|
-
expect(await get("b")).toMatchObject({ cacheStatus: "miss", html: "<p>b:-:2</p>" });
|
|
326
|
-
expect(renders()).toBe(2);
|
|
327
|
-
});
|
|
328
|
-
it("keys the cache by the query keys the page read, and only those", async () => {
|
|
329
|
-
const { get, renders } = isr();
|
|
330
|
-
expect((await get("a", { page: "1", utm: "x" })).cacheStatus).toBe("miss");
|
|
331
|
-
// the page read `page`, not `utm`: another utm value shares the entry
|
|
332
|
-
expect(await get("a", { page: "1", utm: "y" })).toMatchObject({ cacheStatus: "hit", html: "<p>a:1:1</p>" });
|
|
333
|
-
// another `page` is another page
|
|
334
|
-
expect(await get("a", { page: "2" })).toMatchObject({ cacheStatus: "miss", html: "<p>a:2:2</p>" });
|
|
335
|
-
expect(renders()).toBe(2);
|
|
336
|
-
});
|
|
337
|
-
it("keys a page that enumerates the whole query by the whole query", async () => {
|
|
338
|
-
let n = 0;
|
|
339
|
-
const { core } = make({
|
|
340
|
-
pages: [page("/all", "isr", { revalidate: 10 })],
|
|
341
|
-
modules: { "/all": { default: (props) => jsx("p", { children: `${JSON.stringify(Object.keys(props.searchParams).sort())}:${++n}` }) } },
|
|
342
|
-
});
|
|
343
|
-
const get = (query) => core.renderPage(req("/all", { query }));
|
|
344
|
-
expect((await get({ a: "1" })).cacheStatus).toBe("miss");
|
|
345
|
-
expect((await get({ a: "1" })).cacheStatus).toBe("hit");
|
|
346
|
-
expect((await get({ a: "1", b: "2" })).cacheStatus).toBe("miss");
|
|
347
|
-
expect(n).toBe(2);
|
|
348
|
-
});
|
|
349
|
-
it("serves a stale entry at once and refreshes it once in the background", async () => {
|
|
350
|
-
vi.useFakeTimers();
|
|
351
|
-
const { get, renders, settle } = isr({}, 10);
|
|
352
|
-
await get("a");
|
|
353
|
-
vi.setSystemTime(Date.now() + 11000);
|
|
354
|
-
const [s1, s2] = await Promise.all([get("a"), get("a")]);
|
|
355
|
-
expect(s1).toMatchObject({ cacheStatus: "stale", html: "<p>a:-:1</p>" });
|
|
356
|
-
expect(s2.cacheStatus).toBe("stale");
|
|
357
|
-
await settle();
|
|
358
|
-
// two concurrent stale requests shared one refresh
|
|
359
|
-
expect(renders()).toBe(2);
|
|
360
|
-
expect(await get("a")).toMatchObject({ cacheStatus: "hit", html: "<p>a:-:2</p>" });
|
|
361
|
-
});
|
|
362
|
-
it("does not cache a render that opted out, nor one that was invalidated while it ran", async () => {
|
|
363
|
-
let n = 0;
|
|
364
|
-
const { core } = make({
|
|
365
|
-
pages: [page("/ns", "isr", { revalidate: 10 })],
|
|
366
|
-
modules: {
|
|
367
|
-
"/ns": {
|
|
368
|
-
default: () => {
|
|
369
|
-
markNoStore();
|
|
370
|
-
return jsx("p", { children: String(++n) });
|
|
371
|
-
},
|
|
372
|
-
},
|
|
373
|
-
},
|
|
374
|
-
});
|
|
375
|
-
const a = await core.renderPage(req("/ns"));
|
|
376
|
-
const b = await core.renderPage(req("/ns"));
|
|
377
|
-
expect(a).toMatchObject({ noStore: true, cacheStatus: "miss", html: "<p>1</p>" });
|
|
378
|
-
expect(b).toMatchObject({ cacheStatus: "miss", html: "<p>2</p>" });
|
|
379
|
-
});
|
|
380
|
-
it("ties entries to the tags the render read, so a tag invalidation finds them", async () => {
|
|
381
|
-
let n = 0;
|
|
382
|
-
const changes = [];
|
|
383
|
-
const { core } = make({
|
|
384
|
-
pages: [page("/t/[id]", "isr", { revalidate: 100 })],
|
|
385
|
-
modules: {
|
|
386
|
-
"/t/[id]": {
|
|
387
|
-
default: (props) => {
|
|
388
|
-
recordCacheRead(["posts", "post:" + props.params.id], undefined);
|
|
389
|
-
return jsx("p", { children: String(++n) });
|
|
390
|
-
},
|
|
391
|
-
},
|
|
392
|
-
},
|
|
393
|
-
opts: { onIsrChange: (immediate) => void changes.push(immediate) },
|
|
394
|
-
});
|
|
395
|
-
const get = (id) => core.renderPage(req("/t/[id]", { params: { id } }));
|
|
396
|
-
await get("1");
|
|
397
|
-
await get("2");
|
|
398
|
-
expect((await get("1")).cacheStatus).toBe("hit");
|
|
399
|
-
expect(changes).toEqual([false, false]);
|
|
400
|
-
const out = await core.applyInternalOp("cache-invalidate", { tags: ["post:1"] });
|
|
401
|
-
expect(out).toMatchObject({ ok: true, isrEntries: 1 });
|
|
402
|
-
expect(changes[changes.length - 1]).toBe(true);
|
|
403
|
-
expect((await get("1")).cacheStatus).toBe("miss");
|
|
404
|
-
expect((await get("2")).cacheStatus).toBe("hit");
|
|
405
|
-
const all = await core.applyInternalOp("cache-invalidate", { tags: ["posts"] });
|
|
406
|
-
expect(all.isrEntries).toBe(2);
|
|
407
|
-
});
|
|
408
|
-
});
|
|
409
|
-
describe("applyInternalOp", () => {
|
|
410
|
-
function counter() {
|
|
411
|
-
let n = 0;
|
|
412
|
-
const s = make({
|
|
413
|
-
pages: [page("/c/[id]", "isr", { revalidate: 100 }), page("/live", "ssr")],
|
|
414
|
-
modules: {
|
|
415
|
-
"/c/[id]": { default: (props) => jsx("p", { children: `${props.params?.id ?? "base"}:${++n}` }) },
|
|
416
|
-
"/live": { default: text("live") },
|
|
417
|
-
},
|
|
418
|
-
});
|
|
419
|
-
return { ...s, renders: () => n, get: (id) => s.core.renderPage(req("/c/[id]", id ? { params: { id } } : {})) };
|
|
420
|
-
}
|
|
421
|
-
it("cache-invalidate with a path target drops one params variant, or the whole route without params", async () => {
|
|
422
|
-
const { core, get } = counter();
|
|
423
|
-
await get("a");
|
|
424
|
-
await get("b");
|
|
425
|
-
expect(await core.applyInternalOp("cache-invalidate", { targets: [{ route: "/c/[id]", params: { id: "a" } }] })).toMatchObject({ ok: true, isrEntries: 1 });
|
|
426
|
-
expect((await get("a")).cacheStatus).toBe("miss");
|
|
427
|
-
expect((await get("b")).cacheStatus).toBe("hit");
|
|
428
|
-
expect(await core.applyInternalOp("cache-invalidate", { targets: [{ route: "/c/[id]" }] })).toMatchObject({ isrEntries: 2 });
|
|
429
|
-
expect((await get("b")).cacheStatus).toBe("miss");
|
|
430
|
-
});
|
|
431
|
-
it("cache-invalidate ignores malformed input", async () => {
|
|
432
|
-
const { core } = counter();
|
|
433
|
-
expect(await core.applyInternalOp("cache-invalidate", { tags: [1, "", null], targets: [null, { route: 5 }, "x"] })).toMatchObject({ ok: true, isrEntries: 0, regionEntries: 0 });
|
|
434
|
-
});
|
|
435
|
-
it("revalidate-route clears every variant and renders the base variant again", async () => {
|
|
436
|
-
const { core, get, renders } = counter();
|
|
437
|
-
await get("a");
|
|
438
|
-
const before = renders();
|
|
439
|
-
expect(await core.applyInternalOp("revalidate-route", { route: "/c/[id]" })).toEqual({ ok: true });
|
|
440
|
-
expect(renders()).toBe(before + 1);
|
|
441
|
-
expect((await get("a")).cacheStatus).toBe("miss");
|
|
442
|
-
});
|
|
443
|
-
it("refresh-route does nothing for a page that is not ISR", async () => {
|
|
444
|
-
const { core } = counter();
|
|
445
|
-
expect(await core.applyInternalOp("refresh-route", { route: "/live" })).toEqual({ ok: true, refreshed: 0 });
|
|
446
|
-
expect(await core.applyInternalOp("refresh-route", { route: "/missing" })).toEqual({ ok: true, refreshed: 0 });
|
|
447
|
-
});
|
|
448
|
-
it("devInvalidate drops cached variants of a route", async () => {
|
|
449
|
-
const { core, get } = counter();
|
|
450
|
-
await get("a");
|
|
451
|
-
core.devInvalidate("/c/[id]");
|
|
452
|
-
expect((await get("a")).cacheStatus).toBe("miss");
|
|
453
|
-
});
|
|
454
|
-
});
|
|
455
|
-
describe("regions", () => {
|
|
456
|
-
const region = (id, extra = {}) => ({ id, bundlePath: "r/" + id, ...extra });
|
|
457
|
-
it("rejects a region without a bundle; it is fatal only for a page with server components", async () => {
|
|
458
|
-
const plain = make({ pages: [page("/p")], regions: {} });
|
|
459
|
-
expect(await plain.core.renderRegion(page("/p"), { id: "x" }, req("/p"))).toMatchObject({ type: "error", id: "x", status: 400, fatal: false });
|
|
460
|
-
const sc = make({ pages: [page("/p", "ssr", { serverComponents: true })] });
|
|
461
|
-
expect(await sc.core.renderRegion(page("/p", "ssr", { serverComponents: true }), { id: "x" }, req("/p"))).toMatchObject({ fatal: true });
|
|
462
|
-
});
|
|
463
|
-
it("passes the baked props with the request params and query to the runtime bundle", async () => {
|
|
464
|
-
const seen = [];
|
|
465
|
-
const { core } = make({
|
|
466
|
-
pages: [page("/p")],
|
|
467
|
-
runtime: {
|
|
468
|
-
"r/a": (json) => {
|
|
469
|
-
seen.push(JSON.parse(typeof json === "string" ? json : JSON.stringify(json)));
|
|
470
|
-
return "<i>A</i>";
|
|
471
|
-
},
|
|
472
|
-
},
|
|
473
|
-
});
|
|
474
|
-
const frame = await core.renderRegion(page("/p"), region("a", { props: { price: 5 } }), req("/p", { params: { id: "1" }, query: { q: "x" } }));
|
|
475
|
-
expect(frame).toEqual({ type: "region", id: "a", status: 200, html: "<i>A</i>" });
|
|
476
|
-
expect(seen).toEqual([{ price: 5, params: { id: "1" }, query: { q: "x" } }]);
|
|
477
|
-
seen.length = 0;
|
|
478
|
-
await core.renderRegion(page("/p"), region("a", { props: { price: 5 } }), req("/p", { params: {}, query: {} }));
|
|
479
|
-
expect(seen).toEqual([{ price: 5 }]);
|
|
480
|
-
});
|
|
481
|
-
it("does not cache regions of a plain SSR page", async () => {
|
|
482
|
-
let n = 0;
|
|
483
|
-
const { core } = make({ pages: [page("/p")], runtime: { "r/a": () => `<i>${++n}</i>` } });
|
|
484
|
-
const a = await core.renderRegion(page("/p"), region("a"), req("/p"));
|
|
485
|
-
const b = await core.renderRegion(page("/p"), region("a"), req("/p"));
|
|
486
|
-
expect([a.html, b.html, a.cacheStatus]).toEqual(["<i>1</i>", "<i>2</i>", undefined]);
|
|
487
|
-
});
|
|
488
|
-
it("caches regions of an ISR page for the page window: miss, hit, stale with one background refresh", async () => {
|
|
489
|
-
vi.useFakeTimers();
|
|
490
|
-
let n = 0;
|
|
491
|
-
const p = page("/p/[id]", "isr", { revalidate: 10 });
|
|
492
|
-
const { core, settle } = make({ pages: [p], runtime: { "r/a": () => `<i>${++n}</i>` } });
|
|
493
|
-
const r = region("a");
|
|
494
|
-
const q = req("/p/[id]", { params: { id: "1" } });
|
|
495
|
-
expect(await core.renderRegion(p, r, q)).toMatchObject({ html: "<i>1</i>", cacheStatus: "miss" });
|
|
496
|
-
expect(await core.renderRegion(p, r, q)).toMatchObject({ html: "<i>1</i>", cacheStatus: "hit", cached: true });
|
|
497
|
-
// another params variant is another entry
|
|
498
|
-
expect(await core.renderRegion(p, r, req("/p/[id]", { params: { id: "2" } }))).toMatchObject({ html: "<i>2</i>", cacheStatus: "miss" });
|
|
499
|
-
vi.setSystemTime(Date.now() + 11000);
|
|
500
|
-
const [a, b] = await Promise.all([core.renderRegion(p, r, q), core.renderRegion(p, r, q)]);
|
|
501
|
-
expect([a.cacheStatus, b.cacheStatus]).toEqual(["stale", "stale"]);
|
|
502
|
-
await settle();
|
|
503
|
-
expect(n).toBe(3);
|
|
504
|
-
expect(await core.renderRegion(p, r, q)).toMatchObject({ html: "<i>3</i>", cacheStatus: "hit" });
|
|
505
|
-
});
|
|
506
|
-
it("lets the region's own window win, and ppr caches the regions of a non-ISR page", async () => {
|
|
507
|
-
let n = 0;
|
|
508
|
-
const own = make({ pages: [page("/p")], runtime: { "r/a": () => `<i>${++n}</i>` } });
|
|
509
|
-
const p = page("/p");
|
|
510
|
-
expect((await own.core.renderRegion(p, region("a", { revalidate: 30 }), req("/p"))).cacheStatus).toBe("miss");
|
|
511
|
-
expect((await own.core.renderRegion(p, region("a", { revalidate: 30 }), req("/p"))).cacheStatus).toBe("hit");
|
|
512
|
-
let m = 0;
|
|
513
|
-
const ppr = make({ pages: [page("/p")], runtime: { "r/a": () => `<i>${++m}</i>` }, opts: { ppr: true } });
|
|
514
|
-
expect((await ppr.core.renderRegion(p, region("a"), req("/p"))).cacheStatus).toBe("miss");
|
|
515
|
-
expect((await ppr.core.renderRegion(p, region("a"), req("/p"))).cacheStatus).toBe("hit");
|
|
516
|
-
});
|
|
517
|
-
it("does not cache a region that read uncached data", async () => {
|
|
518
|
-
let n = 0;
|
|
519
|
-
const p = page("/p/[id]", "isr", { revalidate: 10 });
|
|
520
|
-
const { core } = make({
|
|
521
|
-
pages: [p],
|
|
522
|
-
runtime: {
|
|
523
|
-
"r/a": () => {
|
|
524
|
-
markNoStore();
|
|
525
|
-
return `<i>${++n}</i>`;
|
|
526
|
-
},
|
|
527
|
-
},
|
|
528
|
-
});
|
|
529
|
-
const first = await core.renderRegion(p, region("a"), req("/p/[id]"));
|
|
530
|
-
expect(first).toMatchObject({ noStore: true, cacheStatus: "miss" });
|
|
531
|
-
expect((await core.renderRegion(p, region("a"), req("/p/[id]"))).html).toBe("<i>2</i>");
|
|
532
|
-
});
|
|
533
|
-
it("reports a failing region as an error frame with a digest, fatal only for server components", async () => {
|
|
534
|
-
const fail = () => {
|
|
535
|
-
const e = new Error("boom");
|
|
536
|
-
e.stack = "at x";
|
|
537
|
-
throw e;
|
|
538
|
-
};
|
|
539
|
-
const { core } = make({ pages: [page("/p")], runtime: { "r/a": fail }, modules: { "/p": { Broken: fail } } });
|
|
540
|
-
const plain = await core.renderRegion(page("/p"), region("a"), req("/p"));
|
|
541
|
-
expect(plain).toMatchObject({ type: "error", id: "a", status: 500, error: "boom", digest: "8a2474a04d", fatal: false });
|
|
542
|
-
const server = await core.renderRegion(page("/p"), { id: "s", serverExport: "Broken" }, req("/p"));
|
|
543
|
-
expect(server).toMatchObject({ type: "error", status: 500, fatal: true });
|
|
544
|
-
});
|
|
545
|
-
it("answers a region that calls forbidden() or unauthorized() with a 403 or 401 frame", async () => {
|
|
546
|
-
const { core } = make({
|
|
547
|
-
pages: [page("/p")],
|
|
548
|
-
runtime: { "r/fb": () => forbidden(), "r/un": () => unauthorized() },
|
|
549
|
-
});
|
|
550
|
-
expect(await core.renderRegion(page("/p"), region("fb"), req("/p"))).toEqual({ type: "region", id: "fb", status: 403, html: "", denied: 403 });
|
|
551
|
-
expect(await core.renderRegion(page("/p"), region("un"), req("/p"))).toEqual({ type: "region", id: "un", status: 401, html: "", denied: 401 });
|
|
552
|
-
});
|
|
553
|
-
it("answers a region that calls notFound() with a 404 frame and a redirect with a 307 frame", async () => {
|
|
554
|
-
const { core } = make({
|
|
555
|
-
pages: [page("/p")],
|
|
556
|
-
runtime: {
|
|
557
|
-
"r/nf": () => notFound(),
|
|
558
|
-
"r/rd": () => {
|
|
559
|
-
throw Object.assign(new Error("r"), { __krateRedirect: "/to" });
|
|
560
|
-
},
|
|
561
|
-
},
|
|
562
|
-
});
|
|
563
|
-
expect(await core.renderRegion(page("/p"), region("nf"), req("/p"))).toEqual({ type: "region", id: "nf", status: 404, html: "", notFound: true });
|
|
564
|
-
expect(await core.renderRegion(page("/p"), region("rd"), req("/p"))).toEqual({ type: "region", id: "rd", status: 307, html: "", redirect: "/to" });
|
|
565
|
-
});
|
|
566
|
-
it("renders an async server component region from the page bundle's export", async () => {
|
|
567
|
-
const { core } = make({
|
|
568
|
-
pages: [page("/p")],
|
|
569
|
-
modules: {
|
|
570
|
-
"/p": {
|
|
571
|
-
Price: async (props) => {
|
|
572
|
-
await new Promise((ok) => setTimeout(ok, 5));
|
|
573
|
-
return jsx("b", { children: `${props.item}:${props.params.id}` });
|
|
574
|
-
},
|
|
575
|
-
},
|
|
576
|
-
},
|
|
577
|
-
});
|
|
578
|
-
const frame = await core.renderRegion(page("/p"), { id: "s", serverExport: "Price", props: { item: "pen" } }, req("/p", { params: { id: "9" } }));
|
|
579
|
-
expect(frame).toMatchObject({ type: "region", status: 200, html: "<b>pen:9</b>" });
|
|
580
|
-
const missing = await core.renderRegion(page("/p"), { id: "s", serverExport: "Nope" }, req("/p"));
|
|
581
|
-
expect(missing).toMatchObject({ type: "error", status: 500, fatal: true });
|
|
582
|
-
expect(String(missing.error)).toContain("does not export Nope");
|
|
583
|
-
});
|
|
584
|
-
});
|
|
585
|
-
describe("renderPageRegion", () => {
|
|
586
|
-
it("wraps a page render as a coarse region frame and extracts the title", async () => {
|
|
587
|
-
const { core } = make({
|
|
588
|
-
pages: [page("/p", "ssr")],
|
|
589
|
-
modules: { "/p": { default: () => jsx("main", { children: [jsx("title", { children: "Hello & bye" }), jsx("p", { children: "x" })] }) } },
|
|
590
|
-
});
|
|
591
|
-
const frame = await core.renderPageRegion(page("/p"), "page", req("/p"));
|
|
592
|
-
expect(frame).toMatchObject({ type: "region", id: "page", kind: "page", status: 200 });
|
|
593
|
-
expect(frame.title).toBe("Hello &amp; bye");
|
|
594
|
-
expect(String(frame.html)).toContain("<p>x</p>");
|
|
595
|
-
});
|
|
596
|
-
it("carries cache status, not-found, redirect and error details", async () => {
|
|
597
|
-
const { core } = make({
|
|
598
|
-
pages: [page("/i", "isr", { revalidate: 10 }), page("/nf"), page("/rd"), page("/err", "ssr", { serverComponents: true }), page("/err2")],
|
|
599
|
-
modules: {
|
|
600
|
-
"/i": { default: text("i") },
|
|
601
|
-
"/nf": { default: () => notFound() },
|
|
602
|
-
"/rd": {
|
|
603
|
-
default: () => {
|
|
604
|
-
throw Object.assign(new Error("r"), { __krateRedirect: "/to" });
|
|
605
|
-
},
|
|
606
|
-
},
|
|
607
|
-
"/err": { default: () => { throw new Error("bad"); } },
|
|
608
|
-
"/err2": { default: () => { throw new Error("bad"); } },
|
|
609
|
-
},
|
|
610
|
-
});
|
|
611
|
-
expect(await core.renderPageRegion(page("/i", "isr", { revalidate: 10 }), "page", req("/i"))).toMatchObject({ cacheStatus: "miss" });
|
|
612
|
-
expect(await core.renderPageRegion(page("/nf"), "page", req("/nf"))).toMatchObject({ status: 404, notFound: true });
|
|
613
|
-
expect(await core.renderPageRegion(page("/rd"), "page", req("/rd"))).toMatchObject({ status: 307, redirect: "/to" });
|
|
614
|
-
const fatal = await core.renderPageRegion(page("/err", "ssr", { serverComponents: true }), "page", req("/err"));
|
|
615
|
-
expect(fatal).toMatchObject({ status: 500, error: "bad", fatal: true });
|
|
616
|
-
expect(fatal.digest).toMatch(/^[0-9a-f]{10}$/);
|
|
617
|
-
const soft = await core.renderPageRegion(page("/err2"), "page", req("/err2"));
|
|
618
|
-
expect(soft.fatal).toBeUndefined();
|
|
619
|
-
});
|
|
620
|
-
});
|
package/dist/ssr-core.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|