foldkit-ssr 0.11.0 → 0.12.0
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 +606 -61
- package/dist/{client-qmdYY-Eb.d.ts → client-BEHlUHBv.d.ts} +10 -7
- package/dist/client-BEHlUHBv.d.ts.map +1 -0
- package/dist/client.d.ts +6 -5
- package/dist/client.js +63 -15
- package/dist/client.js.map +1 -1
- package/dist/context.d.ts +1 -1
- package/dist/context.js.map +1 -1
- package/dist/diagnostics.d.ts +2 -2
- package/dist/diagnostics.js +7 -1
- package/dist/diagnostics.js.map +1 -1
- package/dist/handoff.d.ts +23 -0
- package/dist/handoff.d.ts.map +1 -0
- package/dist/handoff.js +15 -0
- package/dist/handoff.js.map +1 -0
- package/dist/index.d.ts +66 -26
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +242 -45
- package/dist/index.js.map +1 -1
- package/dist/keep.d.ts +3 -1
- package/dist/keep.d.ts.map +1 -1
- package/dist/keep.js +21 -6
- package/dist/keep.js.map +1 -1
- package/dist/{listen-DaUPRKVR.d.ts → listen-7HzKBhqU.d.ts} +11 -2
- package/dist/{listen-DaUPRKVR.d.ts.map → listen-7HzKBhqU.d.ts.map} +1 -1
- package/dist/listen.d.ts +2 -2
- package/dist/listen.js +17 -7
- package/dist/listen.js.map +1 -1
- package/dist/loader.d.ts +13 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +156 -0
- package/dist/loader.js.map +1 -0
- package/dist/marks-CX15BwOw.d.ts +39 -0
- package/dist/marks-CX15BwOw.d.ts.map +1 -0
- package/dist/marks.d.ts +2 -0
- package/dist/marks.js +41 -0
- package/dist/marks.js.map +1 -0
- package/dist/{resumable-BPN_qQHU.d.ts → resumable-BV9_OKxr.d.ts} +7 -10
- package/dist/resumable-BV9_OKxr.d.ts.map +1 -0
- package/dist/resumable.d.ts +2 -1
- package/dist/resumable.js +16 -19
- package/dist/resumable.js.map +1 -1
- package/dist/runtimeChunk.d.ts +13 -0
- package/dist/runtimeChunk.d.ts.map +1 -0
- package/dist/runtimeChunk.js +36 -0
- package/dist/runtimeChunk.js.map +1 -0
- package/dist/runtimeUrl.d.ts +10 -0
- package/dist/runtimeUrl.d.ts.map +1 -0
- package/dist/runtimeUrl.js +13 -0
- package/dist/runtimeUrl.js.map +1 -0
- package/dist/{shared-CJN-m58Z.d.ts → shared-mQ8CXr-r.d.ts} +53 -18
- package/dist/shared-mQ8CXr-r.d.ts.map +1 -0
- package/dist/shared.d.ts +3 -2
- package/dist/shared.js +34 -13
- package/dist/shared.js.map +1 -1
- package/dist/vite.d.ts +3 -2
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +2 -1
- package/dist/vite.js.map +1 -1
- package/package.json +11 -6
- package/dist/client-qmdYY-Eb.d.ts.map +0 -1
- package/dist/resumable-BPN_qQHU.d.ts.map +0 -1
- package/dist/shared-CJN-m58Z.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { withContext } from "./context.js";
|
|
2
2
|
import { cachedPageDiffers, handlerUnnamed, pageFailed, reportingTo } from "./diagnostics.js";
|
|
3
|
-
import { BINDING_ATTRIBUTE,
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { BINDING_ATTRIBUTE, FOLDKIT_APP_ATTRIBUTE, KEEP_ATTRIBUTE, RESUME_ATTRIBUTE, UNTIL_LIMIT_MS } from "./marks.js";
|
|
4
|
+
import { FALLBACK_DEPTH_FIELD, FALLBACK_FIELD, SLOT_ATTRIBUTE } from "./resumable.js";
|
|
5
|
+
import { HEAD_END_MARKER, HEAD_MARKER, META_ATTRIBUTE, ResumeRefused, STATIC_ATTRIBUTE, STYLES_MARKER, allowedTags, attributeOf, codecOf, metaMarkup, modelFrom, pathKey, plan, projectionsOf, resume, routeOf, serializeJsonScript, serving, startingFrom, staticRegion, tagOf, unbound } from "./shared.js";
|
|
6
|
+
import { Diagnostics, Resume, SSR as SSR$1 } from "./client.js";
|
|
6
7
|
import { requestContext } from "./requestContext.js";
|
|
7
8
|
import { decodeKept, fits, headerOf, keptApplications, keptDocuments, pageKey, samePage, tracked } from "./pageCache.js";
|
|
8
9
|
import { Cause, Context, Effect, Exit, Layer, Option, Result, Schema } from "effect";
|
|
@@ -24,6 +25,8 @@ import { FOLDKIT_FLAGS_ATTRIBUTE, Rendered, Responded, injectIntoTemplate, rende
|
|
|
24
25
|
* handover. This module is the server's side and the browser's
|
|
25
26
|
* (`foldkit-ssr/client`) together.
|
|
26
27
|
*/
|
|
28
|
+
/** The set `foldkit-mixins` collects drawn classes in, under the same registered symbol. */
|
|
29
|
+
const DRAWN_CLASSES = Symbol.for("foldkit-plus.drawnClasses");
|
|
27
30
|
/** The projections of the plan's entries that are active for `model`. */
|
|
28
31
|
const activeProjections = (plan, model) => plan.surfaces.flatMap((source) => projectionsOf(source, model));
|
|
29
32
|
/**
|
|
@@ -57,7 +60,8 @@ const envelopeOf = (resume, payload, options) => serializeJsonScript({
|
|
|
57
60
|
...options.route === void 0 ? {} : { route: options.route },
|
|
58
61
|
...options.match === "path" ? { match: "path" } : {},
|
|
59
62
|
...options.bindings === void 0 || options.bindings.length === 0 ? {} : { bindings: options.bindings },
|
|
60
|
-
...options.events === void 0 || options.events.length === 0 ? {} : { events: options.events }
|
|
63
|
+
...options.events === void 0 || options.events.length === 0 ? {} : { events: options.events },
|
|
64
|
+
...resume.start === "now" ? {} : { start: resume.start }
|
|
61
65
|
});
|
|
62
66
|
/**
|
|
63
67
|
* The rendered application with the envelope on its stamped root, beside
|
|
@@ -97,36 +101,107 @@ const isInRootTag = (html, at) => {
|
|
|
97
101
|
return true;
|
|
98
102
|
};
|
|
99
103
|
/**
|
|
100
|
-
* The rendered markup with `
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* root holds no children: styles for one are refused, naming the tag, rather
|
|
104
|
-
* than served where no browser could keep them.
|
|
104
|
+
* The rendered markup with `insert` as the root's last children. A void root
|
|
105
|
+
* holds no children: what it would carry is refused, naming the tag, rather
|
|
106
|
+
* than served where no browser could keep it.
|
|
105
107
|
*/
|
|
106
|
-
const
|
|
108
|
+
const beforeRootEnd = (html, insert, refused) => {
|
|
107
109
|
const tag = /^<([A-Za-z][A-Za-z0-9-]*)/.exec(html)?.[1];
|
|
108
110
|
const at = tag === void 0 ? -1 : html.search(new RegExp(`</${tag}>\\s*$`));
|
|
109
|
-
if (tag === void 0 || at === -1) return Result.fail(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
if (tag === void 0 || at === -1) return Result.fail(refused(tag ?? "?"));
|
|
112
|
+
return Result.succeed(`${html.slice(0, at)}${insert}${html.slice(at)}`);
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The rendered markup with `extra` after the styles marker, so the first
|
|
116
|
+
* paint is styled before any script runs where no template head can take
|
|
117
|
+
* it; `SSR.hydrate` moves them into the head.
|
|
118
|
+
*/
|
|
119
|
+
const withInRoot = (html, extra) => beforeRootEnd(html, `<!--${STYLES_MARKER}-->${extra}`, (tag) => new ResumeUnsafe({
|
|
120
|
+
reason: "VoidRootWithStyles",
|
|
121
|
+
message: `styles need a root element that can hold them, but the view returned <${tag}>: put the styles in the template's head with SSR.generate, or return an element root`
|
|
122
|
+
}));
|
|
123
|
+
const HEAD_OPEN = `<!--${HEAD_MARKER}-->`;
|
|
124
|
+
const HEAD_CLOSE = `<!--${HEAD_END_MARKER}-->`;
|
|
125
|
+
/**
|
|
126
|
+
* Roots a parser does not keep `<meta>` inside: a table's (it is moved out),
|
|
127
|
+
* foreign content (it breaks out), and raw text, where the markers are text.
|
|
128
|
+
*/
|
|
129
|
+
const HEADLESS_ROOTS = /* @__PURE__ */ new Set([
|
|
130
|
+
"table",
|
|
131
|
+
"thead",
|
|
132
|
+
"tbody",
|
|
133
|
+
"tfoot",
|
|
134
|
+
"tr",
|
|
135
|
+
"colgroup",
|
|
136
|
+
"svg",
|
|
137
|
+
"math",
|
|
138
|
+
"textarea",
|
|
139
|
+
"title",
|
|
140
|
+
"style",
|
|
141
|
+
"script",
|
|
142
|
+
"xmp",
|
|
143
|
+
"iframe",
|
|
144
|
+
"noembed",
|
|
145
|
+
"noframes",
|
|
146
|
+
"noscript",
|
|
147
|
+
"plaintext"
|
|
148
|
+
]);
|
|
149
|
+
const rootTagOf = (html) => /^<([A-Za-z][A-Za-z0-9-]*)/.exec(html)?.[1]?.toLowerCase();
|
|
150
|
+
/**
|
|
151
|
+
* The rendered markup with the plan's `meta` between the head markers at the
|
|
152
|
+
* root's end, where `SSR.headed` takes it from. A root that cannot carry it
|
|
153
|
+
* to the host intact is refused, naming the tag.
|
|
154
|
+
*/
|
|
155
|
+
const withHeadInRoot = (html, markup) => {
|
|
156
|
+
if (markup === "") return Result.succeed(html);
|
|
157
|
+
const refused = (tag) => new ResumeUnsafe({
|
|
158
|
+
reason: "RootRefusesHead",
|
|
159
|
+
message: `the plan's meta needs a root that keeps it until SSR.headed moves it, but the view returned <${tag}>: serve the page with SSR.generate, or return another element root`
|
|
160
|
+
});
|
|
161
|
+
const tag = rootTagOf(html);
|
|
162
|
+
if (tag !== void 0 && HEADLESS_ROOTS.has(tag)) return Result.fail(refused(tag));
|
|
163
|
+
return beforeRootEnd(html, `${HEAD_OPEN}${markup}${HEAD_CLOSE}`, refused);
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* The head block `withHeadInRoot` put in a rendered root, and the root
|
|
167
|
+
* without it: the last close marker, followed only by the styles or the
|
|
168
|
+
* root's end, and the last open marker before it, between which is only
|
|
169
|
+
* escaped `meta`. Markers anywhere else (text a view drew in a `<style>`) are
|
|
170
|
+
* not a block, and stay where they are.
|
|
171
|
+
*/
|
|
172
|
+
const pageHeadOf = (html) => {
|
|
173
|
+
const tag = rootTagOf(html);
|
|
174
|
+
const end = html.lastIndexOf(HEAD_CLOSE);
|
|
175
|
+
if (tag === void 0 || HEADLESS_ROOTS.has(tag) || end === -1) return Option.none();
|
|
176
|
+
const after = html.slice(end + HEAD_CLOSE.length);
|
|
177
|
+
if (!after.startsWith(`<!--foldkit-plus-styles-->`) && !new RegExp(`^</${tag}>\\s*$`, "i").test(after)) return Option.none();
|
|
178
|
+
const start = html.lastIndexOf(HEAD_OPEN, end);
|
|
179
|
+
if (start === -1) return Option.none();
|
|
180
|
+
return Option.some({
|
|
181
|
+
head: html.slice(start + HEAD_OPEN.length, end),
|
|
182
|
+
html: `${html.slice(0, start)}${after}`
|
|
183
|
+
});
|
|
114
184
|
};
|
|
115
185
|
/**
|
|
116
186
|
* The Model the browser will start from when the server's Model is `model`:
|
|
117
187
|
* the payload taken through the page as the envelope carries it, as JSON, and
|
|
118
|
-
* resumed exactly as `SSR.resume` resumes it
|
|
119
|
-
*
|
|
188
|
+
* resumed exactly as `SSR.resume` resumes it, with each restored path taking
|
|
189
|
+
* the server's value (the browser reads the same address and cookie). A part
|
|
190
|
+
* that cannot restore its own capture fails here, on the server.
|
|
120
191
|
*/
|
|
121
|
-
const browserModelOf = (plan, payload) =>
|
|
192
|
+
const browserModelOf = (plan, payload, model) => {
|
|
193
|
+
const resumed = modelFrom(plan, JSON.parse(serializeJsonScript(payload)));
|
|
194
|
+
return Result.isFailure(resumed) ? resumed : Result.succeed(plan.restored.reduce((browser, place) => place.copy(browser, model), resumed.success));
|
|
195
|
+
};
|
|
122
196
|
const pathOf = (path) => path.length === 0 ? "(the whole Model)" : path.join(".");
|
|
123
197
|
/**
|
|
124
|
-
* A path is covered by a sent or local path equal to it or
|
|
125
|
-
* empty path is the whole Model, which no pick covers.
|
|
198
|
+
* A path is covered by a sent, restored or local path equal to it or
|
|
199
|
+
* containing it. An empty path is the whole Model, which no pick covers.
|
|
126
200
|
*/
|
|
127
201
|
const coverOf = (path, plan) => {
|
|
128
202
|
const contains = (outer) => outer.length > 0 && outer.length <= path.length && outer.every((key, index) => path[index] === key);
|
|
129
203
|
if (plan.state.dependencies.some(contains)) return "state";
|
|
204
|
+
if (plan.restored.some(({ path: outer }) => contains(outer))) return "restored";
|
|
130
205
|
if (plan.local.some(contains)) return "local";
|
|
131
206
|
return "missing";
|
|
132
207
|
};
|
|
@@ -138,7 +213,7 @@ const readsKey = (projection) => projection === void 0 ? "inactive" : JSON.strin
|
|
|
138
213
|
* worked out for a Model, not for the plan alone.
|
|
139
214
|
*/
|
|
140
215
|
const inspect = (plan, model) => {
|
|
141
|
-
const resumed = browserModelOf(plan, payloadOf(plan, model));
|
|
216
|
+
const resumed = browserModelOf(plan, payloadOf(plan, model), model);
|
|
142
217
|
if (Result.isFailure(resumed)) throw new Error(`SSR.inspect: ${resumed.failure}`);
|
|
143
218
|
return coverage(plan, model, resumed.success);
|
|
144
219
|
};
|
|
@@ -156,6 +231,7 @@ const coverage = (plan, model, browser) => {
|
|
|
156
231
|
id: plan.id,
|
|
157
232
|
state: plan.state.dependencies.map(pathOf),
|
|
158
233
|
local: plan.local.map(pathOf),
|
|
234
|
+
restored: plan.restored.map(({ path }) => pathOf(path)),
|
|
159
235
|
parts: plan.parts.map((part) => part.id),
|
|
160
236
|
surfaces: plan.surfaces.flatMap((source) => {
|
|
161
237
|
const surface = Option.match(familyOf(source), {
|
|
@@ -236,7 +312,9 @@ var ResumeUnsafe = class extends Schema.TaggedError()("ResumeUnsafe", {
|
|
|
236
312
|
"UndeclaredSurfaces",
|
|
237
313
|
"BindingInStaticRegion",
|
|
238
314
|
"VoidRootWithStyles",
|
|
239
|
-
"
|
|
315
|
+
"RootRefusesHead",
|
|
316
|
+
"UnkeepableField",
|
|
317
|
+
"UnkeptField"
|
|
240
318
|
]),
|
|
241
319
|
message: Schema.String
|
|
242
320
|
}) {};
|
|
@@ -365,6 +443,7 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
365
443
|
const inStatic = [];
|
|
366
444
|
const unnamed = [];
|
|
367
445
|
const unkeepable = [];
|
|
446
|
+
const unkept = [];
|
|
368
447
|
const fallback = fallbackEncoder(plan);
|
|
369
448
|
const capturing = withContext({
|
|
370
449
|
...config,
|
|
@@ -380,7 +459,8 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
380
459
|
region: void 0,
|
|
381
460
|
inStatic,
|
|
382
461
|
unnamed,
|
|
383
|
-
unkeepable
|
|
462
|
+
unkeepable,
|
|
463
|
+
unkept
|
|
384
464
|
});
|
|
385
465
|
const full = yield* renderToString(capturing, options);
|
|
386
466
|
unkeepable.push(...unplacedKeeps(full.html));
|
|
@@ -396,6 +476,10 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
396
476
|
reason: "UnkeepableField",
|
|
397
477
|
message: `the page cannot keep what is typed into these fields before it boots:\n- ${unkeepable.join("\n- ")}`
|
|
398
478
|
});
|
|
479
|
+
if (plan.start !== "now" && unkept.length > 0) return yield* new ResumeUnsafe({
|
|
480
|
+
reason: "UnkeptField",
|
|
481
|
+
message: `the plan starts ${plan.start}, and what is typed into these fields before it boots would be lost at its first render: give each KeepTyped, or start now:\n- ${unkept.join("\n- ")}`
|
|
482
|
+
});
|
|
399
483
|
const started = served;
|
|
400
484
|
const commands = started.commands ?? [];
|
|
401
485
|
if (commands.length > 0 && plan.boot === void 0) return yield* new ResumeUnsafe({
|
|
@@ -403,7 +487,7 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
403
487
|
message: `init returned ${commands.map((command) => command.name).join(", ")}, which no browser would run: name them in the plan's boot`
|
|
404
488
|
});
|
|
405
489
|
const payload = payloadOf(plan, started.model);
|
|
406
|
-
const resumed = browserModelOf(plan, payload);
|
|
490
|
+
const resumed = browserModelOf(plan, payload, started.model);
|
|
407
491
|
if (Result.isFailure(resumed)) return yield* new ResumeUnsafe({
|
|
408
492
|
reason: "UnrestorablePart",
|
|
409
493
|
message: resumed.failure
|
|
@@ -423,6 +507,9 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
423
507
|
reason: "Uncovered",
|
|
424
508
|
message: `the plan does not cover what the browser reads:\n- ${uncovered.join("\n- ")}`
|
|
425
509
|
});
|
|
510
|
+
const drawn = /* @__PURE__ */ new Set();
|
|
511
|
+
const global = globalThis;
|
|
512
|
+
global[DRAWN_CLASSES] = drawn;
|
|
426
513
|
const rendered = yield* renderToString(withContext(startingFrom(config, { model: browser }), {
|
|
427
514
|
mode: "replay",
|
|
428
515
|
regions,
|
|
@@ -430,7 +517,9 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
430
517
|
fallback,
|
|
431
518
|
wrap: (message) => message,
|
|
432
519
|
depth: 0
|
|
433
|
-
}), options)
|
|
520
|
+
}), options).pipe(Effect.ensuring(Effect.sync(() => {
|
|
521
|
+
if (global[DRAWN_CLASSES] === drawn) delete global[DRAWN_CLASSES];
|
|
522
|
+
})));
|
|
434
523
|
const encoded = encodeBindings(plan, servedBindings);
|
|
435
524
|
const reencoded = encodeBindings(plan, browserBindings);
|
|
436
525
|
if (Result.isFailure(encoded)) return yield* new ResumeUnsafe({
|
|
@@ -454,7 +543,9 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
454
543
|
bindings: encoded.success,
|
|
455
544
|
events: [.../* @__PURE__ */ new Set([...servedBindings.map((binding) => binding.event), ...unnamed.map((handler) => handler.event)])].sort()
|
|
456
545
|
});
|
|
457
|
-
const
|
|
546
|
+
const headed = options.metaInRoot === true ? withHeadInRoot(rendered.html, meta) : Result.succeed(rendered.html);
|
|
547
|
+
if (Result.isFailure(headed)) return yield* headed.failure;
|
|
548
|
+
const styled = options.styles === void 0 ? Result.succeed(headed.success) : withInRoot(headed.success, options.styles(rendered, drawn));
|
|
458
549
|
if (Result.isFailure(styled)) return yield* styled.failure;
|
|
459
550
|
return {
|
|
460
551
|
page: {
|
|
@@ -463,6 +554,7 @@ const renderMatching = (config, plan, options, match) => Effect.gen(function* ()
|
|
|
463
554
|
html: withEnvelopeAttribute(styled.success, envelope)
|
|
464
555
|
},
|
|
465
556
|
meta,
|
|
557
|
+
drawn,
|
|
466
558
|
envelope,
|
|
467
559
|
unnamed
|
|
468
560
|
},
|
|
@@ -500,7 +592,7 @@ const withFilledTags = (template, rendered) => {
|
|
|
500
592
|
return template;
|
|
501
593
|
};
|
|
502
594
|
/** The plan's `meta`, then what `head` adds, one line apart. */
|
|
503
|
-
const headOf = (result, head) => [result.meta, head?.(result.rendered) ?? ""].filter((markup) => markup !== "").join("\n");
|
|
595
|
+
const headOf = (result, head) => [result.meta, head?.(result.rendered, result.drawn) ?? ""].filter((markup) => markup !== "").join("\n");
|
|
504
596
|
/**
|
|
505
597
|
* The page to serve: the rendered application in the template, with the
|
|
506
598
|
* plan's `meta` and `head`'s markup, if any, before `</head>`. The envelope
|
|
@@ -508,6 +600,28 @@ const headOf = (result, head) => [result.meta, head?.(result.rendered) ?? ""].fi
|
|
|
508
600
|
* with a head, and nothing else.
|
|
509
601
|
*/
|
|
510
602
|
const page = (template, result, options = {}) => injectIntoTemplate(withHead(withFilledTags(template, result.rendered), headOf(result, options.head)), result.rendered);
|
|
603
|
+
/**
|
|
604
|
+
* `render`, the host's document renderer, with the head markup `SSR.entry`
|
|
605
|
+
* carries in the rendered root (a plan's `meta`) moved before the document's
|
|
606
|
+
* `</head>`, where link previews and crawlers read it. Wrap whatever draws the
|
|
607
|
+
* document: `SSR.headed(renderDocument)`, or, for a host that hands Foldkit a
|
|
608
|
+
* template, `SSR.headed(application => injectIntoTemplate(template,
|
|
609
|
+
* application))`. The markup is taken from the rendered root alone, where the
|
|
610
|
+
* entry put it, so nothing a view draws elsewhere is moved. Without it the
|
|
611
|
+
* markup stays in the body, and `SSR.hydrate` reports `HeadNotMoved`.
|
|
612
|
+
*/
|
|
613
|
+
const headed = (render) => (application, ...args) => Option.match(pageHeadOf(application.html), {
|
|
614
|
+
onNone: () => render(application, ...args),
|
|
615
|
+
onSome: ({ head, html }) => {
|
|
616
|
+
const document = render({
|
|
617
|
+
...application,
|
|
618
|
+
html
|
|
619
|
+
}, ...args);
|
|
620
|
+
const at = document.search(/<\/head>/i);
|
|
621
|
+
if (at === -1) throw new Error("foldkit-ssr: the document has no </head> to put the head in");
|
|
622
|
+
return `${document.slice(0, at)}${head}${document.slice(at)}`;
|
|
623
|
+
}
|
|
624
|
+
});
|
|
511
625
|
/** The file a static host serves for a path: `/about` is `about/index.html`. */
|
|
512
626
|
const fileOf = (path) => {
|
|
513
627
|
const trimmed = path.replace(/^\/+/, "").replace(/\/+$/, "");
|
|
@@ -635,16 +749,15 @@ const generate = (config, plan, options) => Effect.gen(function* () {
|
|
|
635
749
|
* `handleRequest` still classifies static misses and answers `HEAD` without
|
|
636
750
|
* a body.
|
|
637
751
|
*
|
|
638
|
-
* What the page says of itself (`meta`)
|
|
639
|
-
*
|
|
640
|
-
*
|
|
752
|
+
* What the page says of itself (`meta`) belongs in the head, which the host
|
|
753
|
+
* owns here: the entry carries it in the rendered root, and the host's
|
|
754
|
+
* document renderer, wrapped in `SSR.headed`, moves it before `</head>`.
|
|
641
755
|
*/
|
|
642
756
|
const entry = (config, plan, options) => {
|
|
643
757
|
const cache = Option.map(Option.fromUndefinedOr(options.cache), (given) => ({
|
|
644
758
|
...given,
|
|
645
759
|
policy: cachePolicy(given)
|
|
646
760
|
}));
|
|
647
|
-
if (plan.meta !== void 0) throw new Error(`foldkit-ssr: plan "${plan.id}" says what the page says of itself (meta), which is written into the template's head, and the template is the host's: serve it with SSR.generate, not SSR.entry`);
|
|
648
761
|
const listed = options.origins === "same-origin" ? [] : options.origins ?? [];
|
|
649
762
|
for (const origin of listed) if (!URL.canParse(origin) || new URL(origin).origin !== origin) throw new Error(`foldkit-ssr: ${JSON.stringify(origin)} in origins is not an origin`);
|
|
650
763
|
if (options.answerPost !== void 0 && plan.fallback !== "server") throw new Error(`foldkit-ssr: plan "${plan.id}" has no server fallback, so no form is posted to answer (answerPost)`);
|
|
@@ -701,8 +814,12 @@ const entry = (config, plan, options) => {
|
|
|
701
814
|
if (posting && !postedFrom(request, originsOf(request))) return responded(plainText(403, "The form was not posted from this site."));
|
|
702
815
|
const flagsOf = options.flags;
|
|
703
816
|
const statusOf = options.status;
|
|
817
|
+
const redirectOf = options.redirect;
|
|
704
818
|
const { prepare } = options;
|
|
705
|
-
const styled =
|
|
819
|
+
const styled = {
|
|
820
|
+
...options.styles === void 0 ? {} : { styles: options.styles },
|
|
821
|
+
metaInRoot: true
|
|
822
|
+
};
|
|
706
823
|
const exit = await Effect.runPromiseExit(Effect.gen(function* () {
|
|
707
824
|
const flags = flagsOf === void 0 ? void 0 : yield* Effect.tryPromise(async () => flagsOf(request));
|
|
708
825
|
const flagged = flagsOf === void 0 ? {} : { flags };
|
|
@@ -723,13 +840,25 @@ const entry = (config, plan, options) => {
|
|
|
723
840
|
});
|
|
724
841
|
return Responded(extra === void 0 ? redirect : merge(redirect, extra));
|
|
725
842
|
}
|
|
726
|
-
const
|
|
843
|
+
const start = {
|
|
727
844
|
url: request.url,
|
|
728
845
|
...flagged
|
|
729
|
-
}
|
|
846
|
+
};
|
|
847
|
+
const started = posting || prepare === void 0 && redirectOf === void 0 ? Option.none() : Option.some(yield* preparedStart(request, config, start, prepare));
|
|
848
|
+
const sentTo = Option.flatMap(started, ({ model }) => redirectOf === void 0 ? Option.none() : Option.map(redirectOf(model, request), (path) => ({
|
|
849
|
+
path,
|
|
850
|
+
model
|
|
851
|
+
})));
|
|
852
|
+
if (Option.isSome(sentTo)) {
|
|
853
|
+
const { path, model } = sentTo.value;
|
|
854
|
+
return yield* Effect.sync(() => Responded(redirected(path, request.url, headersOf(request, Option.some(model)))));
|
|
855
|
+
}
|
|
856
|
+
const served = posting ? yield* handleServed(request, config, plan, post()) : yield* renderMatching(Option.match(started, {
|
|
857
|
+
onNone: () => config,
|
|
858
|
+
onSome: ({ config }) => config
|
|
859
|
+
}), plan, {
|
|
730
860
|
buildId: options.buildId,
|
|
731
|
-
|
|
732
|
-
...flagged,
|
|
861
|
+
...start,
|
|
733
862
|
...styled
|
|
734
863
|
}, matchOf(request.url));
|
|
735
864
|
const extra = yield* Effect.sync(() => headersOf(request, Option.some(served.model)));
|
|
@@ -753,7 +882,32 @@ const entry = (config, plan, options) => {
|
|
|
753
882
|
}
|
|
754
883
|
return exit.value;
|
|
755
884
|
};
|
|
756
|
-
|
|
885
|
+
/**
|
|
886
|
+
* A prerendered page, as the file every visitor is given: refused, by
|
|
887
|
+
* rejecting, unless it was drawn from its URL alone and sets nothing a
|
|
888
|
+
* file cannot carry.
|
|
889
|
+
*/
|
|
890
|
+
const prerender = async (request) => {
|
|
891
|
+
const watched = {
|
|
892
|
+
read: {
|
|
893
|
+
names: /* @__PURE__ */ new Set(),
|
|
894
|
+
all: false
|
|
895
|
+
},
|
|
896
|
+
reads: []
|
|
897
|
+
};
|
|
898
|
+
const result = await answer(request, watched);
|
|
899
|
+
const refuse = (reason) => /* @__PURE__ */ new Error(`foldkit-ssr: ${routeOf(request.url)} is prerendered, a file every visitor is given, but ${reason}; render it per request instead (leave it out of prerendered)`);
|
|
900
|
+
if (result._tag !== "Rendered") throw refuse(`it was answered ${result.response.status}`);
|
|
901
|
+
const set = [...new Headers(result.headers).keys()];
|
|
902
|
+
const refused = notPublic(result.status ?? 200, watched).pipe(Option.orElse(() => watched.read.names.size === 0 ? Option.none() : Option.some(`it read the request's ${inWords([...watched.read.names])} header${watched.read.names.size > 1 ? "s" : ""}`)), Option.orElse(() => set.length === 0 ? Option.none() : Option.some(`its headers set ${inWords(set)}`)));
|
|
903
|
+
if (Option.isSome(refused)) throw refuse(refused.value);
|
|
904
|
+
return Rendered(result.application);
|
|
905
|
+
};
|
|
906
|
+
const prerendering = (request) => {
|
|
907
|
+
const method = request.method.toUpperCase();
|
|
908
|
+
return (method === "GET" || method === "HEAD") && matchOf(request.url) === "path";
|
|
909
|
+
};
|
|
910
|
+
if (Option.isNone(cache)) return { renderPage: (request) => prerendering(request) ? prerender(request) : answer(request) };
|
|
757
911
|
const { store, check, policy } = cache.value;
|
|
758
912
|
const reportTo = (request) => {
|
|
759
913
|
const { diagnostics } = options;
|
|
@@ -771,13 +925,13 @@ const entry = (config, plan, options) => {
|
|
|
771
925
|
};
|
|
772
926
|
/** The page as a store keeps it, when nothing it was drawn from was the request's own. */
|
|
773
927
|
const keepable = (request, result, watched) => {
|
|
774
|
-
if (result._tag !== "Rendered" || (result.status ?? 200
|
|
928
|
+
if (result._tag !== "Rendered" || Option.isSome(notPublic(result.status ?? 200, watched))) return Option.none();
|
|
775
929
|
if (![...watched.read.names].every((name) => !request.headers.has(name))) return Option.none();
|
|
776
930
|
if (Option.isSome(headerOf(result.headers, "set-cookie")) || Option.isSome(headerOf(result.headers, "cache-control"))) return Option.none();
|
|
777
931
|
let shortest = policy;
|
|
778
932
|
const tags = /* @__PURE__ */ new Set();
|
|
779
933
|
for (const read of watched.reads) {
|
|
780
|
-
if (read._tag === "Private")
|
|
934
|
+
if (read._tag === "Private") continue;
|
|
781
935
|
shortest = shorterPolicy(shortest, read.policy);
|
|
782
936
|
for (const tag of read.tags) tags.add(tag);
|
|
783
937
|
}
|
|
@@ -804,6 +958,7 @@ const entry = (config, plan, options) => {
|
|
|
804
958
|
});
|
|
805
959
|
};
|
|
806
960
|
const renderPage = async (request) => {
|
|
961
|
+
if (prerendering(request)) return prerender(request);
|
|
807
962
|
const method = request.method.toUpperCase();
|
|
808
963
|
if (method !== "GET" && method !== "HEAD") return unkept(await answer(request));
|
|
809
964
|
const key = await Effect.runPromise(pageKey(options.buildId, request.url));
|
|
@@ -834,6 +989,21 @@ const entry = (config, plan, options) => {
|
|
|
834
989
|
return { renderPage };
|
|
835
990
|
};
|
|
836
991
|
/**
|
|
992
|
+
* Why a render drew a page from more than its URL, so it is no page every
|
|
993
|
+
* visitor may be given: one not answered `200`, one that read every header,
|
|
994
|
+
* or one that made a private read. None when it drew it from the URL alone,
|
|
995
|
+
* the headers it read by name aside, which `cache` varies by and a
|
|
996
|
+
* prerendered file cannot.
|
|
997
|
+
*/
|
|
998
|
+
const notPublic = (status, watched) => {
|
|
999
|
+
if (status !== 200) return Option.some(`it was answered ${status}`);
|
|
1000
|
+
if (watched.read.all) return Option.some("it read every header of its request (it listed them, cloned the request or read a request context)");
|
|
1001
|
+
for (const read of watched.reads) if (read._tag === "Private") return Option.some(`it read ${read.read}, which is not public (a source with authorize, filter, visible or a per-viewer member, one with no cache, or a read that failed)`);
|
|
1002
|
+
return Option.none();
|
|
1003
|
+
};
|
|
1004
|
+
/** Names in a sentence: `a`, `a and b`, `a, b and c`. */
|
|
1005
|
+
const inWords = (names) => names.length < 2 ? names.join("") : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`;
|
|
1006
|
+
/**
|
|
837
1007
|
* The page cache's checker, for a test: builds the entry with `make`, given a
|
|
838
1008
|
* store in memory and `check`, and answers each request twice, so a page
|
|
839
1009
|
* kept for one request is drawn again for every later one it serves.
|
|
@@ -870,6 +1040,27 @@ const sameAddress = (url) => {
|
|
|
870
1040
|
return `${pathname.replace(/^\/+/, "/")}${search}`;
|
|
871
1041
|
};
|
|
872
1042
|
/**
|
|
1043
|
+
* `SSR.entry`'s answer to a page `redirect` sends elsewhere: a `303` to `path`
|
|
1044
|
+
* with the entry's `headers`, said `private, no-store` over theirs, since it
|
|
1045
|
+
* was decided for one reader. Throws for a path that is not one on the
|
|
1046
|
+
* request's own origin: resolved there, it must keep that origin and start
|
|
1047
|
+
* with a single `/`, so `//elsewhere.test`, `/\elsewhere.test` (which a
|
|
1048
|
+
* browser reads as `//`), a scheme, or a path whose dot segments resolve to
|
|
1049
|
+
* `//` are refused. The location written is the path as resolved.
|
|
1050
|
+
*/
|
|
1051
|
+
const redirected = (path, url, headers) => {
|
|
1052
|
+
const origin = new URL(url).origin;
|
|
1053
|
+
const resolved = path.startsWith("/") ? new URL(path, origin) : void 0;
|
|
1054
|
+
if (resolved === void 0 || resolved.origin !== origin || resolved.pathname.startsWith("//")) throw new Error(`redirect gave ${JSON.stringify(path)}, which is no path on this origin: give one starting with a single /`);
|
|
1055
|
+
const response = new Response(null, {
|
|
1056
|
+
status: 303,
|
|
1057
|
+
headers: { location: `${resolved.pathname}${resolved.search}${resolved.hash}` }
|
|
1058
|
+
});
|
|
1059
|
+
headers?.forEach((value, name) => response.headers.append(name, value));
|
|
1060
|
+
response.headers.set("cache-control", "private, no-store");
|
|
1061
|
+
return response;
|
|
1062
|
+
};
|
|
1063
|
+
/**
|
|
873
1064
|
* What `headers` may not set: what frames the response, which the host's
|
|
874
1065
|
* server writes, and `location`, which only the entry writes.
|
|
875
1066
|
*/
|
|
@@ -982,11 +1173,14 @@ const startOf = (config, options) => Effect.gen(function* () {
|
|
|
982
1173
|
*/
|
|
983
1174
|
const preparedStart = (request, config, options, prepare) => Effect.gen(function* () {
|
|
984
1175
|
const started = yield* startOf(config, options);
|
|
985
|
-
const model = yield* Effect.orDie(prepare(started.model, request));
|
|
986
|
-
return
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1176
|
+
const model = prepare === void 0 ? started.model : yield* Effect.orDie(prepare(started.model, request));
|
|
1177
|
+
return {
|
|
1178
|
+
model,
|
|
1179
|
+
config: startingFrom(config, {
|
|
1180
|
+
...started,
|
|
1181
|
+
model
|
|
1182
|
+
})
|
|
1183
|
+
};
|
|
990
1184
|
});
|
|
991
1185
|
/**
|
|
992
1186
|
* Answers a form posted by a page rendered with `fallback: 'server'`: the
|
|
@@ -1001,7 +1195,8 @@ const handle = (request, config, plan, options) => Effect.map(handleServed(reque
|
|
|
1001
1195
|
const handleServed = (request, config, plan, options) => Effect.flatMap(postedModel(request, config, plan, options), (model) => renderMatching(startingFrom(config, { model }), plan, {
|
|
1002
1196
|
buildId: options.buildId,
|
|
1003
1197
|
url: request.url,
|
|
1004
|
-
...options.styles === void 0 ? {} : { styles: options.styles }
|
|
1198
|
+
...options.styles === void 0 ? {} : { styles: options.styles },
|
|
1199
|
+
...options.metaInRoot === true ? { metaInRoot: true } : {}
|
|
1005
1200
|
}, "full"));
|
|
1006
1201
|
/** The Model a posted form reaches: its Message decoded and checked, then `update` and its Commands. */
|
|
1007
1202
|
const postedModel = (request, config, plan, options) => Effect.scoped(Effect.gen(function* () {
|
|
@@ -1063,11 +1258,13 @@ const SSR = {
|
|
|
1063
1258
|
hydrate: SSR$1.hydrate,
|
|
1064
1259
|
inspect,
|
|
1065
1260
|
static: staticRegion,
|
|
1261
|
+
unbound,
|
|
1066
1262
|
serving,
|
|
1067
1263
|
generate,
|
|
1068
1264
|
entry,
|
|
1069
1265
|
checkCache,
|
|
1070
1266
|
keptDocuments,
|
|
1267
|
+
headed,
|
|
1071
1268
|
handle,
|
|
1072
1269
|
serializeJsonScript,
|
|
1073
1270
|
sitemap,
|