@ogygia/postrender 0.13.0 → 0.15.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/dist/adapters/akamai.d.ts +6 -0
- package/dist/adapters/akamai.js +11 -15
- package/dist/adapters/cloudflare-kv.d.ts +12 -0
- package/dist/adapters/cloudflare-kv.js +12 -0
- package/dist/adapters/cloudflare.d.ts +6 -0
- package/dist/adapters/cloudflare.js +8 -1
- package/dist/adapters/cloudfront.js +5 -4
- package/dist/adapters/upstash.d.ts +7 -0
- package/dist/adapters/upstash.js +11 -4
- package/dist/adapters/valkey.d.ts +7 -0
- package/dist/adapters/valkey.js +9 -4
- package/dist/app.js +8 -3
- package/dist/handle.d.ts +16 -0
- package/dist/handle.js +19 -5
- package/dist/index.d.ts +25 -0
- package/dist/index.js +3 -4
- package/dist/key.d.ts +0 -8
- package/dist/key.js +3 -11
- package/dist/memory-store.js +4 -3
- package/dist/messages.d.ts +6 -0
- package/dist/messages.js +14 -10
- package/dist/observe.d.ts +6 -10
- package/dist/observe.js +10 -59
- package/dist/registry.d.ts +2 -0
- package/dist/registry.js +45 -27
- package/dist/server.d.ts +2 -5
- package/dist/server.js +183 -60
- package/dist/stitch.d.ts +8 -22
- package/dist/stitch.js +42 -66
- package/dist/types.d.ts +5 -12
- package/dist/vite.d.ts +17 -1
- package/dist/vite.js +18 -11
- package/package.json +9 -6
package/dist/stitch.d.ts
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* postrender — SERVE-TIME STITCHING (the zero-flash upgrade over `render: 'live'`).
|
|
3
|
-
*
|
|
4
|
-
* A `with { render: 'deferred', stitch: 'serve' }` hole ships in the stored shell as a normal
|
|
5
|
-
* signed hole PLUS a `stitch` attribute. On every serve of that entry the handle renders the
|
|
6
|
-
* hole server-side RIGHT THEN (the visitor's cookies ride the request) and SPLICES the HTML
|
|
7
|
-
* into the stored bytes — one response, personalized first paint, view-source included.
|
|
8
|
-
*
|
|
9
|
-
* Laws: fail-open PER HOLE (a failed/timed-out render keeps the fallback + the client fetch —
|
|
10
|
-
* a slow upstream never blocks the page); the spliced wrapper becomes a neutral
|
|
11
|
-
* `display:contents` div so nested islands inside the stitched HTML are TOP-LEVEL regions that
|
|
12
|
-
* wake themselves (no region ancestor to ride or wait on); stitched entries are per-visitor —
|
|
13
|
-
* the capture stamps them `private, no-store` and mints no validators.
|
|
14
|
-
*
|
|
15
|
-
* Pure string functions (unit-tested); the handle threads its own `#render_capability`.
|
|
16
|
-
*/
|
|
17
|
-
/** Cheap presence probe — the capture uses it to flag entries; serves use it to skip the scan. */
|
|
18
|
-
export declare const STITCH_HOLE_RE: RegExp;
|
|
19
1
|
/** How a stitch hole fills: `'serve'` = the ORIGIN re-renders + splices on every serve (per-visitor
|
|
20
2
|
* page, edge-bypassed) · `'edge'` = the CDN fills an ESI include per request (the shell stays
|
|
21
|
-
* edge-cached; origin renders only the hole).
|
|
3
|
+
* edge-cached; origin renders only the hole). A bare `stitch` attribute (older builds) reads as
|
|
4
|
+
* `'serve'`. */
|
|
22
5
|
export type StitchMode = "serve" | "edge";
|
|
23
6
|
export interface StitchHole {
|
|
24
7
|
/** Absolute span of the WHOLE region block (open tag through matching close). */
|
|
@@ -54,8 +37,11 @@ export declare function esi_rewrite(html: string, page_path?: string): string;
|
|
|
54
37
|
/** Find every TOP-LEVEL stitch-marked region block, balanced across nested regions. */
|
|
55
38
|
export declare function find_stitch_holes(html: string): StitchHole[];
|
|
56
39
|
/**
|
|
57
|
-
* Splice fresh renders into every stitch hole
|
|
58
|
-
*
|
|
59
|
-
*
|
|
40
|
+
* Splice fresh renders into every stitch hole (`stitch: 'serve'`): on each serve of a stored page
|
|
41
|
+
* the handle renders its holes for this visitor and splices them into the stored bytes. `render`
|
|
42
|
+
* resolves an endpoint to HTML or null; null or a throw keeps that hole's block verbatim, so its
|
|
43
|
+
* fallback ships and the client fetches it as an unstitched hole would. Holes render in parallel.
|
|
44
|
+
* A filled hole becomes a `display:contents` div, so the regions inside it are top-level and wake
|
|
45
|
+
* on their own.
|
|
60
46
|
*/
|
|
61
47
|
export declare function stitch_html(html: string, render: (endpoint: string) => Promise<string | null>): Promise<string>;
|
package/dist/stitch.js
CHANGED
|
@@ -1,37 +1,12 @@
|
|
|
1
1
|
import { escapeAttr } from "@ogygia/core/plugin/utils";
|
|
2
|
+
import { next_open_or_close } from "@ogygia/core/internal/server";
|
|
2
3
|
// src/stitch.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* A `with { render: 'deferred', stitch: 'serve' }` hole ships in the stored shell as a normal
|
|
7
|
-
* signed hole PLUS a `stitch` attribute. On every serve of that entry the handle renders the
|
|
8
|
-
* hole server-side RIGHT THEN (the visitor's cookies ride the request) and SPLICES the HTML
|
|
9
|
-
* into the stored bytes — one response, personalized first paint, view-source included.
|
|
10
|
-
*
|
|
11
|
-
* Laws: fail-open PER HOLE (a failed/timed-out render keeps the fallback + the client fetch —
|
|
12
|
-
* a slow upstream never blocks the page); the spliced wrapper becomes a neutral
|
|
13
|
-
* `display:contents` div so nested islands inside the stitched HTML are TOP-LEVEL regions that
|
|
14
|
-
* wake themselves (no region ancestor to ride or wait on); stitched entries are per-visitor —
|
|
15
|
-
* the capture stamps them `private, no-store` and mints no validators.
|
|
16
|
-
*
|
|
17
|
-
* Pure string functions (unit-tested); the handle threads its own `#render_capability`.
|
|
18
|
-
*/
|
|
19
|
-
/** Cheap presence probe — the capture uses it to flag entries; serves use it to skip the scan. */
|
|
20
|
-
const STITCH_HOLE_RE = /<ogygia-region\b[^>]*\bstitch\b[^>]*>/;
|
|
21
|
-
const REGION_TOKEN_RE = /<ogygia-region\b[^>]*>|<\/ogygia-region>/g;
|
|
22
|
-
const STITCH_ATTR_RE = /\bstitch\b/;
|
|
23
|
-
/** `stitch="edge"` — the ESI mode. Anything else that carries `stitch` (`"serve"`, or the bare
|
|
24
|
-
* attribute an older build emitted) is the origin-serve mode. */
|
|
25
|
-
const STITCH_EDGE_RE = /\bstitch="edge"/;
|
|
26
|
-
const ENDPOINT_ATTR_RE = /\bendpoint="([^"]*)"/;
|
|
4
|
+
const STITCH_ANY_CASE = /stitch/i;
|
|
5
|
+
const REGION = "ogygia-region";
|
|
27
6
|
const AMP_RE = /&/g;
|
|
28
7
|
/** Which stitch modes a stored page carries. One `'serve'` hole makes the WHOLE page per-visitor
|
|
29
8
|
* (serve taints); a page whose stitch holes are all `'edge'` stays edge-cacheable. */
|
|
30
9
|
function stitch_modes(html) {
|
|
31
|
-
if (!STITCH_HOLE_RE.test(html)) return {
|
|
32
|
-
serve: false,
|
|
33
|
-
edge: false
|
|
34
|
-
};
|
|
35
10
|
const holes = find_stitch_holes(html);
|
|
36
11
|
return {
|
|
37
12
|
serve: holes.some((h) => h.mode === "serve"),
|
|
@@ -57,70 +32,71 @@ function stitch_modes(html) {
|
|
|
57
32
|
function esi_rewrite(html, page_path = "/") {
|
|
58
33
|
const holes = find_stitch_holes(html).filter((h) => h.mode === "edge");
|
|
59
34
|
if (!holes.length) return html;
|
|
60
|
-
|
|
35
|
+
const out = [];
|
|
61
36
|
let last = 0;
|
|
62
37
|
for (const h of holes) {
|
|
63
|
-
const abs = new URL(h.endpoint,
|
|
38
|
+
const abs = new URL(h.endpoint, `http://o${page_path}`);
|
|
64
39
|
const src = escapeAttr(abs.pathname + abs.search);
|
|
65
|
-
out
|
|
66
|
-
out += `<esi:remove>${html.slice(h.start, h.end)}</esi:remove><esi:include src="${src}" onerror="continue"/>`;
|
|
40
|
+
out.push(html.slice(last, h.start), `<esi:remove>${html.slice(h.start, h.end)}</esi:remove><esi:include src="${src}" onerror="continue"/>`);
|
|
67
41
|
last = h.end;
|
|
68
42
|
}
|
|
69
|
-
out
|
|
70
|
-
return out;
|
|
43
|
+
out.push(html.slice(last));
|
|
44
|
+
return out.join("");
|
|
71
45
|
}
|
|
72
46
|
/** Find every TOP-LEVEL stitch-marked region block, balanced across nested regions. */
|
|
73
47
|
function find_stitch_holes(html) {
|
|
74
48
|
const holes = [];
|
|
49
|
+
if (!STITCH_ANY_CASE.test(html)) return holes;
|
|
75
50
|
let depth = 0;
|
|
76
51
|
let open = null;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const token = m[0];
|
|
81
|
-
if (token[1] === "/") {
|
|
82
|
-
depth--;
|
|
83
|
-
if (depth === 0 && open) {
|
|
84
|
-
if (STITCH_ATTR_RE.test(open.tag)) {
|
|
85
|
-
const endpoint = ENDPOINT_ATTR_RE.exec(open.tag)?.[1]?.replace(AMP_RE, "&") ?? "";
|
|
86
|
-
if (endpoint) holes.push({
|
|
87
|
-
start: open.start,
|
|
88
|
-
end: m.index + token.length,
|
|
89
|
-
endpoint,
|
|
90
|
-
mode: STITCH_EDGE_RE.test(open.tag) ? "edge" : "serve"
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
open = null;
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
if (depth === 0) open = {
|
|
97
|
-
start: m.index,
|
|
98
|
-
tag: token
|
|
99
|
-
};
|
|
52
|
+
for (let t = next_open_or_close(html, REGION, 0); t !== null;) {
|
|
53
|
+
if ("tag" in t) {
|
|
54
|
+
if (depth === 0) open = t.tag;
|
|
100
55
|
depth++;
|
|
56
|
+
t = next_open_or_close(html, REGION, t.tag.next);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const end = t.end;
|
|
60
|
+
t = next_open_or_close(html, REGION, end);
|
|
61
|
+
if (depth === 0) continue;
|
|
62
|
+
depth--;
|
|
63
|
+
if (depth === 0 && open !== null) {
|
|
64
|
+
const stitch = open.attr("stitch");
|
|
65
|
+
if (stitch !== void 0) {
|
|
66
|
+
const endpoint = open.attr("endpoint")?.replace(AMP_RE, "&") ?? "";
|
|
67
|
+
if (endpoint) holes.push({
|
|
68
|
+
start: open.start,
|
|
69
|
+
end,
|
|
70
|
+
endpoint,
|
|
71
|
+
mode: stitch === "edge" ? "edge" : "serve"
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
open = null;
|
|
101
75
|
}
|
|
102
76
|
}
|
|
103
77
|
return holes;
|
|
104
78
|
}
|
|
105
79
|
/**
|
|
106
|
-
* Splice fresh renders into every stitch hole
|
|
107
|
-
*
|
|
108
|
-
*
|
|
80
|
+
* Splice fresh renders into every stitch hole (`stitch: 'serve'`): on each serve of a stored page
|
|
81
|
+
* the handle renders its holes for this visitor and splices them into the stored bytes. `render`
|
|
82
|
+
* resolves an endpoint to HTML or null; null or a throw keeps that hole's block verbatim, so its
|
|
83
|
+
* fallback ships and the client fetches it as an unstitched hole would. Holes render in parallel.
|
|
84
|
+
* A filled hole becomes a `display:contents` div, so the regions inside it are top-level and wake
|
|
85
|
+
* on their own.
|
|
109
86
|
*/
|
|
110
87
|
async function stitch_html(html, render) {
|
|
111
88
|
const holes = find_stitch_holes(html);
|
|
112
89
|
if (!holes.length) return html;
|
|
113
90
|
const rendered = await Promise.all(holes.map((h) => render(h.endpoint).catch(() => null)));
|
|
114
|
-
|
|
91
|
+
const out = [];
|
|
115
92
|
let last = 0;
|
|
116
93
|
for (let i = 0; i < holes.length; i++) {
|
|
117
94
|
const fill = rendered[i];
|
|
118
95
|
if (fill === null) continue;
|
|
119
|
-
out
|
|
120
|
-
out += `<div style="display:contents" data-og-stitched="">${fill}</div>`;
|
|
96
|
+
out.push(html.slice(last, holes[i].start), `<div style="display:contents" data-og-stitched="">${fill}</div>`);
|
|
121
97
|
last = holes[i].end;
|
|
122
98
|
}
|
|
123
|
-
out
|
|
124
|
-
return out;
|
|
99
|
+
out.push(html.slice(last));
|
|
100
|
+
return out.join("");
|
|
125
101
|
}
|
|
126
|
-
export {
|
|
102
|
+
export { esi_rewrite, find_stitch_holes, stitch_html, stitch_modes };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* postrender — pages stored at request time. Shared contracts.
|
|
3
|
-
*
|
|
4
|
-
* The store holds THE RENDER: a full response (html + headers) or a permanent redirect,
|
|
5
|
-
* keyed by URL pathname. Two tiny adapter contracts (see internal/notes/freeze.md):
|
|
6
|
-
* every store impl passes ONE shared contract spec; every edge adapter speaks its CDN's
|
|
7
|
-
* real purge API. Fan-out (allSettled across edges) lives in ogygia, never in adapters.
|
|
8
|
-
*
|
|
9
|
-
* Bulk eviction is PREFIX-shaped, not locale-shaped: the key is the URL, so any URL subtree
|
|
10
|
-
* (`/fr/fr/` — a locale; `/docs/` — a section) evicts with one call. Locale was the app
|
|
11
|
-
* motivating case, never the contract.
|
|
12
|
-
*/
|
|
13
1
|
/** A stored page: a finished page response, or a permanent redirect. */
|
|
14
2
|
export type PostrenderEntry = {
|
|
15
3
|
kind: "page";
|
|
@@ -22,6 +10,9 @@ export type PostrenderEntry = {
|
|
|
22
10
|
headers: Record<string, string>;
|
|
23
11
|
/** Epoch ms at store time — the read side's TTL backstop (a page older than the TTL is a miss). */
|
|
24
12
|
created: number;
|
|
13
|
+
/** The build that stored it (Kit's `version.name`): it is served to that build only, and is
|
|
14
|
+
* a miss for any other (absent: stored before builds were recorded, a miss too). */
|
|
15
|
+
build?: string;
|
|
25
16
|
/** The shell contains serve-time STITCH holes: every serve re-renders them with the
|
|
26
17
|
* visitor's request and splices — per-visitor responses, `private, no-store`, no
|
|
27
18
|
* validators. See postrender/stitch.ts. */
|
|
@@ -31,6 +22,8 @@ export type PostrenderEntry = {
|
|
|
31
22
|
status: 301 | 308;
|
|
32
23
|
location: string;
|
|
33
24
|
created: number;
|
|
25
|
+
/** The build that stored it: as a page's. */
|
|
26
|
+
build?: string;
|
|
34
27
|
};
|
|
35
28
|
/** Options `put` receives beyond the entry. */
|
|
36
29
|
export interface PostrenderPutOptions {
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
import { PostrenderServerOptions } from "./server.js";
|
|
2
|
-
import { OgygiaPlugin } from "@ogygia/
|
|
2
|
+
import { OgygiaPlugin } from "@ogygia/compiler/vite";
|
|
3
3
|
export interface PostrenderOptions {
|
|
4
4
|
/** TTL backstop in seconds (default 86400, clamped to [1, 86400]): nothing is stale forever. */
|
|
5
5
|
ttl?: number;
|
|
6
6
|
/** The app-wide opt-in (default true). */
|
|
7
7
|
default?: boolean;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* `postrender()`: pages stored at request time, listed beside `ogygia()` in vite.config. The
|
|
11
|
+
* switch and serializable policy, nothing else: a page whose render observed no personalization is
|
|
12
|
+
* stored whole on its first request and served as bytes until `postrender.invalidate(url)` /
|
|
13
|
+
* `postrender.invalidateWhere({ prefix })` or the TTL backstop. Live adapters (a store client, CDN
|
|
14
|
+
* purge credentials) are the argument of `postrender.handle({ store, edge })` in hooks.server.ts.
|
|
15
|
+
*
|
|
16
|
+
* `default` sets the app-wide opt-in: `true` (the default) stores every eligible page, and a route
|
|
17
|
+
* opts out in Kit's own config (`export const config = { postrender: false }`). `false` makes the
|
|
18
|
+
* app opt-in: nothing is stored unless a page or a layout above it sets
|
|
19
|
+
* `config = { postrender: true }`. `config` merges from the layouts into the page, the page's own
|
|
20
|
+
* key winning, as Kit merges it.
|
|
21
|
+
*
|
|
22
|
+
* Needs `dataShake()` from `@ogygia/data-shake/vite` listed beside it: a deferred region whose code reads
|
|
23
|
+
* no `page.data` then skips the page look-up, which would render the stored page again.
|
|
24
|
+
*/
|
|
9
25
|
export declare function postrender(options?: PostrenderOptions): OgygiaPlugin<PostrenderServerOptions>;
|
package/dist/vite.js
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import { definePlugin } from "@ogygia/
|
|
1
|
+
import { definePlugin } from "@ogygia/compiler/vite";
|
|
2
2
|
// src/vite.ts
|
|
3
3
|
/**
|
|
4
|
-
* `postrender()
|
|
5
|
-
*
|
|
4
|
+
* `postrender()`: pages stored at request time, listed beside `ogygia()` in vite.config. The
|
|
5
|
+
* switch and serializable policy, nothing else: a page whose render observed no personalization is
|
|
6
6
|
* stored whole on its first request and served as bytes until `postrender.invalidate(url)` /
|
|
7
|
-
* `postrender.invalidateWhere({ prefix })` or the TTL backstop. Live adapters (a
|
|
8
|
-
* purge
|
|
9
|
-
* `@ogygia/postrender`.
|
|
7
|
+
* `postrender.invalidateWhere({ prefix })` or the TTL backstop. Live adapters (a store client, CDN
|
|
8
|
+
* purge credentials) are the argument of `postrender.handle({ store, edge })` in hooks.server.ts.
|
|
10
9
|
*
|
|
11
|
-
* `default` sets the app-wide opt-in: `true` (the default)
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
10
|
+
* `default` sets the app-wide opt-in: `true` (the default) stores every eligible page, and a route
|
|
11
|
+
* opts out in Kit's own config (`export const config = { postrender: false }`). `false` makes the
|
|
12
|
+
* app opt-in: nothing is stored unless a page or a layout above it sets
|
|
13
|
+
* `config = { postrender: true }`. `config` merges from the layouts into the page, the page's own
|
|
14
|
+
* key winning, as Kit merges it.
|
|
15
|
+
*
|
|
16
|
+
* Needs `dataShake()` from `@ogygia/data-shake/vite` listed beside it: a deferred region whose code reads
|
|
17
|
+
* no `page.data` then skips the page look-up, which would render the stored page again.
|
|
16
18
|
*/
|
|
17
19
|
function postrender(options = {}) {
|
|
18
20
|
return definePlugin({
|
|
@@ -22,6 +24,11 @@ function postrender(options = {}) {
|
|
|
22
24
|
ttl: Math.min(86400, Math.max(1, Math.floor(options.ttl ?? 86400))),
|
|
23
25
|
default: options.default ?? true
|
|
24
26
|
},
|
|
27
|
+
needs: [{
|
|
28
|
+
name: "@ogygia/data-shake",
|
|
29
|
+
call: "dataShake()",
|
|
30
|
+
from: "@ogygia/data-shake/vite"
|
|
31
|
+
}],
|
|
25
32
|
build(b) {
|
|
26
33
|
b.routeConfig("postrender");
|
|
27
34
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ogygia/postrender",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Prerender at request time for ogygia: a page whose render read nothing personal is stored on its first request and served as bytes",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
@@ -41,13 +41,15 @@
|
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@ogygia/machine": "^0.
|
|
44
|
+
"@ogygia/machine": "^0.14.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
+
"@ogygia/compiler": "0.15.0",
|
|
47
48
|
"@ogygia/config": "0.0.0",
|
|
48
|
-
"@ogygia/core": "0.
|
|
49
|
-
"@ogygia/
|
|
50
|
-
"@ogygia/
|
|
49
|
+
"@ogygia/core": "0.15.0",
|
|
50
|
+
"@ogygia/data-shake": "0.15.0",
|
|
51
|
+
"@ogygia/flags": "0.15.0",
|
|
52
|
+
"@ogygia/router": "0.15.0",
|
|
51
53
|
"@ogygia/tooling": "0.0.0",
|
|
52
54
|
"@playwright/test": "1.62.1",
|
|
53
55
|
"@sveltejs/kit": "^2.70.2",
|
|
@@ -58,7 +60,8 @@
|
|
|
58
60
|
"vitest": "^4.1.12"
|
|
59
61
|
},
|
|
60
62
|
"peerDependencies": {
|
|
61
|
-
"@ogygia/
|
|
63
|
+
"@ogygia/compiler": "^0.15.0",
|
|
64
|
+
"@ogygia/core": "^0.15.0",
|
|
62
65
|
"@sveltejs/kit": ">=2.70.2 <4"
|
|
63
66
|
},
|
|
64
67
|
"scripts": {
|