@ogygia/postrender 0.12.0 → 0.14.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/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
- * postrender — SERVE-TIME STITCHING (the zero-flash upgrade over `render: 'live'`).
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 = /&amp;/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
- let out = "";
35
+ const out = [];
61
36
  let last = 0;
62
37
  for (const h of holes) {
63
- const abs = new URL(h.endpoint, "http://o" + page_path);
38
+ const abs = new URL(h.endpoint, `http://o${page_path}`);
64
39
  const src = escapeAttr(abs.pathname + abs.search);
65
- out += html.slice(last, h.start);
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 += html.slice(last);
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
- REGION_TOKEN_RE.lastIndex = 0;
78
- let m;
79
- while (m = REGION_TOKEN_RE.exec(html)) {
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. `render` resolves an endpoint to HTML or null;
107
- * null (failure/timeout) keeps the original block VERBATIM — the fallback ships and the client
108
- * fetches exactly as an unstitched hole would (fail-open). Holes render in PARALLEL.
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
- let out = "";
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 += html.slice(last, holes[i].start);
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 += html.slice(last);
124
- return out;
99
+ out.push(html.slice(last));
100
+ return out.join("");
125
101
  }
126
- export { STITCH_HOLE_RE, esi_rewrite, find_stitch_holes, stitch_html, stitch_modes };
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/core/vite";
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/core/vite";
1
+ import { definePlugin } from "@ogygia/compiler/vite";
2
2
  // src/vite.ts
3
3
  /**
4
- * `postrender()` — pages stored at request time, listed beside `ogygia()` in vite.config. The
5
- * SWITCH + serializable policy, nothing else: a page whose render observed no personalization is
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 valkey client, CDN
8
- * purge creds) are the argument of `postrender.handle({ store, edge })` in hooks.server.ts, from
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) keeps the auto behaviour — every eligible
12
- * page is stored, observed purity refuses the rest, and a route opts OUT in Kit's own config:
13
- * `export const config = { postrender: false }`. `false` flips the app to OPT-IN: nothing is stored
14
- * unless a page or a layout above it sets `config = { postrender: true }`. Kit merges `config` from
15
- * the layouts into the page (the page's own key wins), and so does the build that reads it.
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.12.0",
3
+ "version": "0.14.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",
@@ -31,15 +31,25 @@
31
31
  "./vite": {
32
32
  "types": "./dist/vite.d.ts",
33
33
  "default": "./dist/vite.js"
34
+ },
35
+ "./internal/messages": {
36
+ "types": "./dist/messages.d.ts",
37
+ "default": "./dist/messages.js"
34
38
  }
35
39
  },
36
40
  "publishConfig": {
37
41
  "access": "public"
38
42
  },
43
+ "dependencies": {
44
+ "@ogygia/machine": "^0.14.0"
45
+ },
39
46
  "devDependencies": {
40
- "@ogygia/core": "0.12.0",
41
- "@ogygia/flags": "0.12.0",
42
- "@ogygia/router": "0.12.0",
47
+ "@ogygia/compiler": "0.14.0",
48
+ "@ogygia/config": "0.0.0",
49
+ "@ogygia/core": "0.14.0",
50
+ "@ogygia/data-shake": "0.14.0",
51
+ "@ogygia/flags": "0.14.0",
52
+ "@ogygia/router": "0.14.0",
43
53
  "@ogygia/tooling": "0.0.0",
44
54
  "@playwright/test": "1.62.1",
45
55
  "@sveltejs/kit": "^2.70.2",
@@ -50,7 +60,8 @@
50
60
  "vitest": "^4.1.12"
51
61
  },
52
62
  "peerDependencies": {
53
- "@ogygia/core": "^0.12.0",
63
+ "@ogygia/compiler": "^0.14.0",
64
+ "@ogygia/core": "^0.14.0",
54
65
  "@sveltejs/kit": ">=2.70.2 <4"
55
66
  },
56
67
  "scripts": {