@decocms/tanstack 8.5.1 → 8.6.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.
Files changed (76) hide show
  1. package/README.md +35 -8
  2. package/dist/assignments.d.ts +31 -0
  3. package/dist/assignments.d.ts.map +1 -0
  4. package/dist/assignments.js +61 -0
  5. package/dist/assignments.js.map +1 -0
  6. package/dist/draft.d.ts +9 -6
  7. package/dist/draft.d.ts.map +1 -1
  8. package/dist/draft.js +10 -7
  9. package/dist/draft.js.map +1 -1
  10. package/dist/index.d.ts +3 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/scope/server.d.ts +1 -1
  15. package/dist/scope/server.d.ts.map +1 -1
  16. package/dist/server/index.d.ts +35 -8
  17. package/dist/server/index.d.ts.map +1 -1
  18. package/dist/server/index.js +41 -9
  19. package/dist/server/index.js.map +1 -1
  20. package/dist/shared.d.ts +5 -5
  21. package/dist/shared.js +5 -5
  22. package/dist/start/index.d.ts +3 -3
  23. package/dist/start/index.js +3 -3
  24. package/dist/visitor.d.ts +14 -0
  25. package/dist/visitor.d.ts.map +1 -0
  26. package/dist/visitor.js +14 -0
  27. package/dist/visitor.js.map +1 -0
  28. package/dist/worker/assignments.d.ts +44 -0
  29. package/dist/worker/assignments.d.ts.map +1 -0
  30. package/dist/worker/assignments.js +114 -0
  31. package/dist/worker/assignments.js.map +1 -0
  32. package/dist/worker/cache.d.ts +33 -0
  33. package/dist/worker/cache.d.ts.map +1 -0
  34. package/dist/worker/cache.js +41 -0
  35. package/dist/worker/cache.js.map +1 -0
  36. package/dist/worker/cookies.d.ts +1 -5
  37. package/dist/worker/cookies.d.ts.map +1 -1
  38. package/dist/worker/cookies.js +1 -26
  39. package/dist/worker/cookies.js.map +1 -1
  40. package/dist/worker/drafts.d.ts +27 -48
  41. package/dist/worker/drafts.d.ts.map +1 -1
  42. package/dist/worker/drafts.js +33 -88
  43. package/dist/worker/drafts.js.map +1 -1
  44. package/dist/worker/index.d.ts +77 -51
  45. package/dist/worker/index.d.ts.map +1 -1
  46. package/dist/worker/index.js +141 -360
  47. package/dist/worker/index.js.map +1 -1
  48. package/dist/worker/segment.d.ts +7 -6
  49. package/dist/worker/segment.d.ts.map +1 -1
  50. package/dist/worker/segment.js +6 -5
  51. package/dist/worker/segment.js.map +1 -1
  52. package/dist/worker/serverFn.d.ts +12 -0
  53. package/dist/worker/serverFn.d.ts.map +1 -0
  54. package/dist/worker/serverFn.js +44 -0
  55. package/dist/worker/serverFn.js.map +1 -0
  56. package/package.json +3 -3
  57. package/src/assignments.ts +77 -0
  58. package/src/draft.ts +10 -7
  59. package/src/index.ts +3 -1
  60. package/src/scope/server.ts +1 -1
  61. package/src/server/index.ts +51 -12
  62. package/src/shared.ts +5 -5
  63. package/src/start/index.ts +3 -3
  64. package/src/visitor.ts +14 -0
  65. package/src/worker/assignments.ts +146 -0
  66. package/src/worker/cache.ts +57 -0
  67. package/src/worker/cookies.ts +1 -27
  68. package/src/worker/drafts.ts +38 -98
  69. package/src/worker/index.ts +213 -425
  70. package/src/worker/segment.ts +7 -7
  71. package/src/worker/serverFn.ts +36 -0
  72. package/dist/worker/cacheProfiles.d.ts +0 -43
  73. package/dist/worker/cacheProfiles.d.ts.map +0 -1
  74. package/dist/worker/cacheProfiles.js +0 -162
  75. package/dist/worker/cacheProfiles.js.map +0 -1
  76. package/src/worker/cacheProfiles.ts +0 -180
package/README.md CHANGED
@@ -7,7 +7,7 @@ Peers: `@decocms/blocks` (same version), `@tanstack/react-start`, `@tanstack/rea
7
7
  ## The worker
8
8
 
9
9
  ```ts
10
- // src/worker-entry.ts (wrangler.jsonc "main")
10
+ // src/worker-entry.ts (wrangler.jsonc "main"), without a page cache
11
11
  import { createDecoWorker } from "@decocms/tanstack/worker";
12
12
  import handler, { createServerEntry } from "@tanstack/react-start/server-entry";
13
13
  import { cms } from "./cms";
@@ -15,26 +15,53 @@ import { cms } from "./cms";
15
15
  export default createDecoWorker({
16
16
  serverEntry: createServerEntry({ fetch: handler.fetch }),
17
17
  cms,
18
- site: "my-store",
19
- buildHash: __BUILD_HASH__,
20
18
  dev: import.meta.env.DEV,
21
19
  });
22
20
  ```
23
21
 
24
- Always on: `/deco/_liveness`; 404 for v7's admin paths; Studio may frame the site (`frame-ancestors`, no `X-Frame-Options`) and the security headers; drafts (the host allowlist, the draft cookie, never cached, `noindex`); the CMS redirects; an edge cache keyed on the build, the content revision, the device and your `segment`, with stale-while-revalidate and stale-if-error; CDN caching of client-side navigation data under a checked `__cseg` token; one `Set-Cookie` per cookie; `x-page-status`; and `http.server.request.duration` for every request, in the CMS's telemetry through `recordServerRequest` from `@decocms/blocks/metrics` (nothing without a `token` or an `endpoint`, and then the page template is never looked up), labelled `http.route` (the content's page template, `redirect` for every CMS redirect, a fixed one such as `/_serverFn/*` or `/assets/*`, otherwise `unmatched`), `http.request.method`, `status_class`, `cache` (`HIT`, `MISS`, `BYPASS` or `DRAFT`) and `device`.
22
+ Always on: `/deco/_liveness`; 404 for v7's admin paths; Studio may frame the site (`frame-ancestors`, no `X-Frame-Options`) and the security headers; drafts (the CMS's preview hosts, the draft cookie, `no-store`, `noindex`); the CMS redirects; one `Set-Cookie` per cookie; `x-page-status`; `CDN-Cache-Control: no-store` on every response that names none; and `http.server.request.duration` for every request, in the CMS's telemetry through `recordServerRequest` from `@decocms/blocks/metrics` (nothing without a `token` or an `endpoint`, and then the page template is never looked up), labelled `http.route` (the content's page template, `redirect` for every CMS redirect, a fixed one such as `/_serverFn/*` or `/assets/*`, otherwise `unmatched`), `http.request.method`, `status_class`, `cache` (`DRAFT` for a preview; otherwise the `X-Cache` your `pageCache` answered with, `HIT` for `HIT`, `STALE-HIT` and `STALE-ERROR`, `MISS`, else `BYPASS`) and `device`.
25
23
 
26
- Options, each for a value a site needs to change: `csp`, `segment`, `cacheProfiles`, `safeCookies`, `purgeToken`, `preload`, `previewHosts`, `routes` and `onCache`.
24
+ Options, each for a value a site needs to change: `csp`, `pageCache`, `preload` and `routes`.
25
+
26
+ The worker caches no page: the page cache is the site's, as on Next.js. Without `pageCache` every request renders. [The page cache](#the-page-cache) below links a complete worker entry with one.
27
27
 
28
28
  ### Draft hosts
29
29
 
30
- A draft link (`?__draft=`) previews where v7 allowed it: on the preview hosts the site declares (the `CMS` block's `preview.hosts`, or `createCMS`'s) and always on the hosts Deco runs the site on, `<site>.deco.site` and `<site>.deco-cx.workers.dev`. A site that declares no hosts previews only on those two, not on every host. `previewHosts: [...]` replaces the declared list with exact hosts (`["localhost:5173"]` for local development), as v7's `DECO_ALLOWED_PREVIEW_HOSTS` did; `previewHosts: "none"` turns previews off on every host. The worker passes the accepted draft to the app in the `x-deco-draft` request header: read it with `contentClient(cms)` from `@decocms/tanstack/server`.
30
+ A draft link (`?__draft=`) previews on the hosts the CMS allows, which `cms.readVersion` checks: the addresses Studio writes into the current release's metadata (`previewHosts` in `latest.json`, so `createCMS` needs the site's `site` for them), plus `createCMS({ preview: { hosts } })`. The site lists nothing else. With no list, every host previews: a site with no `latest.json` on the CDN yet, or one published before Studio wrote `previewHosts` (its next publish writes the list), and content with no `site` unless `preview.hosts` lists hosts (such as `["localhost"]`). If `latest.json` can't be read (a network error, a 5xx, a malformed file), previews stay closed, besides `preview.hosts`, until a read succeeds. A link on any other host gets the release, never an error. The same link can carry a published release's commit SHA instead of a draft (Studio's "Preview this version"), and either one can carry a preview time (`__at`) and tests and campaigns switched on or off (`__test`, `__campaign`), which `cms.readVersion` reads as the client's `date`, `experiments` and `campaigns`; all of it passes the same host check, so a visitor on the live site never picks a version. The worker passes the accepted version, alone, to the app in the `x-deco-version` request header (`requestVersion(request)` reads it in a `routes` handler).
31
+
32
+ ### The visitor, the assignments and the client
33
+
34
+ The worker decides once per request, at the edge. On every page and server-function request it reads the visitor with `readVisitor` from `@decocms/blocks/experiments` (the `deco_segment` cookie, or a new visitor), then each test and campaign on or off for it: `readAssignments(cms, { visitor, ...version })`, which resolves the saved `experiments` and `campaigns` blocks through `cms.client({ visitor, version, date, experiments, campaigns })` with the Studio link's version, moment and switched tests and campaigns. It hands the visitor to the app on `x-deco-visitor` and the decisions on `x-deco-assignments`, removing any copy sent from outside, and the decisions to the site's `pageCache` as `page.assignments`, for its key. It sends the visitor's cookie on a first visit only (a visitor with no id yet), never from a preview.
35
+
36
+ `contentClient(cms)` from `@decocms/tanstack/server` builds `cms.client({ version, experiments, campaigns })` from the headers, every test and campaign as the worker decided it, so the page renders exactly what the cache keyed on and decides nothing again; `requestVersion()`, `requestVisitor()` and `requestAssignments()` return them for a site's own code. They trust those headers: behind `createDecoWorker` they hold what the worker decided, since it removes any copy sent from outside. A request that reaches the app without the worker (another entry, or an app that doesn't use `createDecoWorker`) is read the same way, with whatever `x-deco-version`, `x-deco-visitor` and `x-deco-assignments` it arrives with; with none, its visitor decides, and without one every test is off. Which requests go through the worker, and how the others are protected, is the site's call.
37
+
38
+ ### The page cache
39
+
40
+ `pageCache(page)` is the site's own cache around the app. The worker calls it for every page and server-function request `isCacheable` accepts (a `GET` of the current release, outside `vite dev`, without `Authorization`, not `/_build`: never a preview, a draft or an old release), with `page.request` (as the app gets it), `page.url`, `page.visitor` (the visitor the page renders for, a first visit's new one included), `page.assignments` (each test and campaign on or off, as the page renders them), `page.device`, `env`, `ctx` and `page.render(scope?)`, which renders the page. Answer from your cache, or call `render()`; set `X-Cache` on the answer for the request metric. A page request with `Authorization` never reaches it and answers `Cache-Control: private, no-cache, no-store, must-revalidate`, whatever the app set, so no shared cache keeps one caller's page.
41
+
42
+ The visitor's first-visit cookie is never in what `render()` returns: the worker adds it to whatever `pageCache` answers, a cached copy included. So a stored copy never carries a visitor's cookie, and a new visitor served from the cache still gets theirs. Never store a response that sets any other cookie.
43
+
44
+ What the key needs, from `@decocms/tanstack/worker`, the same names as `@decocms/nextjs/proxy`'s where both have them:
45
+
46
+ - `assignmentsKey(page.assignments)`: the tests on and the campaigns running, as one string (`x=<tests>|c=<campaigns>`, `""` for none), never the visitor's id. Visitors on the same side of every test share one entry, the other side gets its own, and a campaign starting or ending moves every page to new entries. `liveAssignmentKeys(cms)` lists every key a live request can carry now (each subset of the saved tests, with the running campaigns and with none), for a purge.
47
+ - `cacheVersion(cms, buildHash)`: `<build>.<content revision>`, so a deploy or a published release gets new entries at once.
48
+ - `detectDevice(userAgent)` (v7's classes: `mobile`, `tablet`, `desktop`), `segmentKey(segment)` (a segment's parts as one stable string) and `segmentToken(segment, version)`: the `__cseg` token. Pass it as `render({ csegToken })` and `DecoDocument` publishes it; `decoServerFnFetch` sends it back on the page's navigation data (`CSEG_PARAM`), and your cache may let the CDN keep that response when the token matches the request's own.
49
+ - `isCacheable(request, { version, dev })`, `requestVersion(request)`, `isServerFnPath(pathname)`, `serverFnPagePath(url)` (the page a server-function page load asks for, from its payload) and `DEGRADED_HEADER` (a page whose loader failed: never store it).
50
+
51
+ A complete worker entry with a page cache is [`examples/tanstack-worker-smoke/src/worker-entry.ts`](https://github.com/decocms/blocks/blob/main/examples/tanstack-worker-smoke/src/worker-entry.ts): copy it to your `src/worker-entry.ts` and change what your site needs. It is v7's edge cache on the Cloudflare Cache API, and every part of it is yours:
52
+
53
+ - **The key**: the path (tracking parameters and `__cseg` dropped), the device and your segment's own parts (`buildSegment`, v7's: a region, a sales channel), `assignmentsKey(page.assignments)` and `cacheVersion(cms, __BUILD_HASH__)`. A signed-in shopper (`loggedIn: true` from `buildSegment`), a private response and a degraded one are never stored.
54
+ - **The profiles**: v7's cache profiles per URL (`PROFILES`), each with how long a page stays fresh, how long it is served stale while it revalidates, and how long when the app fails.
55
+ - **The purge**: v7's `POST /_cache/purge` with `Authorization: Bearer <PURGE_TOKEN>` and a body of `{ "paths": ["/", "/shoes"] }`, optionally with `"segments": [{ "device": "desktop", "salesChannel": "2" }]` (your own parts). It drops each exact path (`/shoes` never drops `/shoes?page=2`) under the current version, for each segment (the devices when the body names none), each assignments key, as a navigation and as a server-function fetch, and answers `{ "purged": [...], "total": n }`. The Cache API is per data center, so a purge reaches the copies of the data center that serves it.
56
+
57
+ `__BUILD_HASH__` is the build id `decoTanstack()` defines (declare it once: `declare const __BUILD_HASH__: string;`), and `PURGE_TOKEN` a Worker secret. The example runs that file as its worker entry, with its tests (`worker-entry.test.ts` next to it).
31
58
 
32
59
  ### Names and Studio's origins
33
60
 
34
- The root export holds what the worker, `/server` and `DecoDocument` share, for a site's own code: the draft names (`DRAFT_PARAM` `__draft`, `DRAFT_COOKIE` `__deco_draft`, `DRAFT_HEADER` `x-deco-draft`), `draftResponseHeaders(pointer)` (`no-store, private`, `Vary: Cookie`, `noindex`), and Studio's origins: `STUDIO_ORIGINS`, `STUDIO_FRAME_ANCESTORS` (what the worker's `frame-ancestors` starts from) and `isStudioOrigin`. `DecoDocument` passes `STUDIO_ORIGINS` to the Studio bridge.
61
+ The root export holds what the worker, `/server` and `DecoDocument` share, for a site's own code: the draft names (`DRAFT_PARAM` `__draft`, `DRAFT_COOKIE` `__deco_draft`), the headers the worker hands the app (`VERSION_HEADER` `x-deco-version`, `VISITOR_HEADER` `x-deco-visitor`, `ASSIGNMENTS_HEADER` `x-deco-assignments`), `draftResponseHeaders(version)` (`no-store, private`, `Vary: Cookie`, `noindex`), and Studio's origins: `STUDIO_ORIGINS`, `STUDIO_FRAME_ANCESTORS` (what the worker's `frame-ancestors` starts from) and `isStudioOrigin`. `DecoDocument` passes `STUDIO_ORIGINS` to the Studio bridge.
35
62
 
36
63
  ```ts
37
- import { DRAFT_HEADER, STUDIO_ORIGINS } from "@decocms/tanstack";
64
+ import { STUDIO_ORIGINS, VERSION_HEADER } from "@decocms/tanstack";
38
65
  ```
39
66
 
40
67
  ## The app
@@ -0,0 +1,31 @@
1
+ /**
2
+ * How the worker hands the app what it decided: each test of the saved
3
+ * `experiments` block on or off for the request's visitor and each campaign
4
+ * of the saved `campaigns` block running or not, as one request header. The
5
+ * worker writes it (`./worker/index.ts`); the app reads it (`contentClient`
6
+ * from `@decocms/tanstack/server`), so the page builds
7
+ * `cms.client({ version, experiments, campaigns })` and decides nothing
8
+ * again. Type imports only, no state: safe in every bundle.
9
+ */
10
+ import type { Assignments } from "@decocms/blocks/experiments";
11
+ /**
12
+ * The request header carrying the decided tests and campaigns
13
+ * (`serializeAssignments`). The worker removes any copy that arrives from
14
+ * outside, so a client can't send one itself.
15
+ */
16
+ export declare const ASSIGNMENTS_HEADER = "x-deco-assignments";
17
+ /** The tests and campaigns of one request, every name on or off. */
18
+ export interface DecidedAssignments {
19
+ /** Each test of the saved `experiments` block: on or off. */
20
+ experiments: Readonly<Assignments>;
21
+ /** Each campaign of the saved `campaigns` block: running or not. */
22
+ campaigns: Readonly<Assignments>;
23
+ }
24
+ /**
25
+ * The tests and campaigns as one header value, every name with its state:
26
+ * `x=+on,-off|c=+running,-not` (names URL-encoded and sorted), `-` for none.
27
+ */
28
+ export declare function serializeAssignments(assignments: DecidedAssignments): string;
29
+ /** What `serializeAssignments` wrote; `null` for anything else (absent, malformed, spelled another way). */
30
+ export declare function parseAssignments(value: string | null | undefined): DecidedAssignments | null;
31
+ //# sourceMappingURL=assignments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assignments.d.ts","sourceRoot":"","sources":["../src/assignments.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE/D;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,uBAAuB,CAAC;AAEvD,oEAAoE;AACpE,MAAM,WAAW,kBAAkB;IACjC,6DAA6D;IAC7D,WAAW,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IACnC,oEAAoE;IACpE,SAAS,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;CAClC;AAeD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,kBAAkB,GAAG,MAAM,CAO5E;AAED,4GAA4G;AAC5G,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,kBAAkB,GAAG,IAAI,CAuB5F"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * The request header carrying the decided tests and campaigns
3
+ * (`serializeAssignments`). The worker removes any copy that arrives from
4
+ * outside, so a client can't send one itself.
5
+ */
6
+ export const ASSIGNMENTS_HEADER = "x-deco-assignments";
7
+ /** Plain code-unit order, the same on every server and locale. */
8
+ const byCodeUnit = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
9
+ const record = () => Object.create(null);
10
+ /** `+name` on, `-name` off, names URL-encoded and sorted. */
11
+ function encodeStates(states) {
12
+ return Object.keys(states)
13
+ .sort(byCodeUnit)
14
+ .map((name) => `${states[name] === true ? "+" : "-"}${encodeURIComponent(name)}`)
15
+ .join(",");
16
+ }
17
+ /**
18
+ * The tests and campaigns as one header value, every name with its state:
19
+ * `x=+on,-off|c=+running,-not` (names URL-encoded and sorted), `-` for none.
20
+ */
21
+ export function serializeAssignments(assignments) {
22
+ const parts = [];
23
+ const tests = encodeStates(assignments.experiments);
24
+ const campaigns = encodeStates(assignments.campaigns);
25
+ if (tests)
26
+ parts.push(`x=${tests}`);
27
+ if (campaigns)
28
+ parts.push(`c=${campaigns}`);
29
+ return parts.length === 0 ? "-" : parts.join("|");
30
+ }
31
+ /** What `serializeAssignments` wrote; `null` for anything else (absent, malformed, spelled another way). */
32
+ export function parseAssignments(value) {
33
+ if (typeof value !== "string" || value === "")
34
+ return null;
35
+ const parsed = { experiments: record(), campaigns: record() };
36
+ if (value === "-")
37
+ return parsed;
38
+ const seen = new Set();
39
+ try {
40
+ for (const part of value.split("|")) {
41
+ const eq = part.indexOf("=");
42
+ const tag = part.slice(0, eq);
43
+ const list = part.slice(eq + 1);
44
+ if (eq <= 0 || list === "" || seen.has(tag) || (tag !== "x" && tag !== "c"))
45
+ return null;
46
+ seen.add(tag);
47
+ const target = (tag === "x" ? parsed.experiments : parsed.campaigns);
48
+ for (const entry of list.split(",")) {
49
+ const state = entry[0];
50
+ if (state !== "+" && state !== "-")
51
+ return null;
52
+ target[decodeURIComponent(entry.slice(1))] = state === "+";
53
+ }
54
+ }
55
+ }
56
+ catch {
57
+ return null;
58
+ }
59
+ return serializeAssignments(parsed) === value ? parsed : null;
60
+ }
61
+ //# sourceMappingURL=assignments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assignments.js","sourceRoot":"","sources":["../src/assignments.ts"],"names":[],"mappings":"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"}
package/dist/draft.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * The draft names and headers of a Deco TanStack site: the `?__draft=` link,
3
3
  * the cookie that keeps a preview going, the request header the worker hands
4
- * the app, and the response headers of a page rendered from a draft. The
4
+ * the app the version on, and the response headers of a page rendered from a draft. The
5
5
  * worker reads and writes them (`./worker/drafts.ts`); the app reads the
6
6
  * header (`@decocms/tanstack/server`). No imports, no state: safe in every
7
7
  * bundle.
@@ -19,15 +19,18 @@ export declare const DRAFT_PARAM = "__draft";
19
19
  export declare const DRAFT_COOKIE = "__deco_draft";
20
20
  /**
21
21
  * The request header the worker sets on the request it hands the app once it
22
- * accepted the draft: the pointer, already checked. The worker removes any
23
- * copy that arrives from outside, so a client can't send one itself.
22
+ * accepted a Studio link: the version (a draft pointer or a release's commit
23
+ * SHA), already checked, without the preview's moment and switched tests
24
+ * (those are decided in the worker: `ASSIGNMENTS_HEADER`). The worker
25
+ * removes any copy that arrives from outside, so a client can't send one
26
+ * itself.
24
27
  */
25
- export declare const DRAFT_HEADER = "x-deco-draft";
28
+ export declare const VERSION_HEADER = "x-deco-version";
26
29
  /**
27
30
  * The response headers of a page rendered from a draft: never stored by a
28
31
  * browser or a CDN (`Cache-Control: no-store, private`, `Vary: Cookie`) and
29
- * never indexed (`X-Robots-Tag: noindex, nofollow`). `{}` when `pointer` is
32
+ * never indexed (`X-Robots-Tag: noindex, nofollow`). `{}` when `version` is
30
33
  * `null`, so the result of the draft decision applies as is.
31
34
  */
32
- export declare function draftResponseHeaders(pointer: string | null | undefined): Record<string, string>;
35
+ export declare function draftResponseHeaders(version: string | null | undefined): Record<string, string>;
33
36
  //# sourceMappingURL=draft.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"draft.d.ts","sourceRoot":"","sources":["../src/draft.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;GAIG;AACH,eAAO,MAAM,WAAW,YAAY,CAAC;AAErC;;;GAGG;AACH,eAAO,MAAM,YAAY,iBAAiB,CAAC;AAE3C;;;;GAIG;AACH,eAAO,MAAM,YAAY,iBAAiB,CAAC;AAE3C;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAO/F"}
1
+ {"version":3,"file":"draft.d.ts","sourceRoot":"","sources":["../src/draft.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;GAIG;AACH,eAAO,MAAM,WAAW,YAAY,CAAC;AAErC;;;GAGG;AACH,eAAO,MAAM,YAAY,iBAAiB,CAAC;AAE3C;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,mBAAmB,CAAC;AAE/C;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAO/F"}
package/dist/draft.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * The draft names and headers of a Deco TanStack site: the `?__draft=` link,
3
3
  * the cookie that keeps a preview going, the request header the worker hands
4
- * the app, and the response headers of a page rendered from a draft. The
4
+ * the app the version on, and the response headers of a page rendered from a draft. The
5
5
  * worker reads and writes them (`./worker/drafts.ts`); the app reads the
6
6
  * header (`@decocms/tanstack/server`). No imports, no state: safe in every
7
7
  * bundle.
@@ -19,18 +19,21 @@ export const DRAFT_PARAM = "__draft";
19
19
  export const DRAFT_COOKIE = "__deco_draft";
20
20
  /**
21
21
  * The request header the worker sets on the request it hands the app once it
22
- * accepted the draft: the pointer, already checked. The worker removes any
23
- * copy that arrives from outside, so a client can't send one itself.
22
+ * accepted a Studio link: the version (a draft pointer or a release's commit
23
+ * SHA), already checked, without the preview's moment and switched tests
24
+ * (those are decided in the worker: `ASSIGNMENTS_HEADER`). The worker
25
+ * removes any copy that arrives from outside, so a client can't send one
26
+ * itself.
24
27
  */
25
- export const DRAFT_HEADER = "x-deco-draft";
28
+ export const VERSION_HEADER = "x-deco-version";
26
29
  /**
27
30
  * The response headers of a page rendered from a draft: never stored by a
28
31
  * browser or a CDN (`Cache-Control: no-store, private`, `Vary: Cookie`) and
29
- * never indexed (`X-Robots-Tag: noindex, nofollow`). `{}` when `pointer` is
32
+ * never indexed (`X-Robots-Tag: noindex, nofollow`). `{}` when `version` is
30
33
  * `null`, so the result of the draft decision applies as is.
31
34
  */
32
- export function draftResponseHeaders(pointer) {
33
- if (!pointer)
35
+ export function draftResponseHeaders(version) {
36
+ if (!version)
34
37
  return {};
35
38
  return {
36
39
  "Cache-Control": "no-store, private",
package/dist/draft.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"draft.js","sourceRoot":"","sources":["../src/draft.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,SAAS,CAAC;AAErC;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAC;AAE3C;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAkC;IACrE,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IACxB,OAAO;QACL,eAAe,EAAE,mBAAmB;QACpC,IAAI,EAAE,QAAQ;QACd,cAAc,EAAE,mBAAmB;KACpC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"draft.js","sourceRoot":"","sources":["../src/draft.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,SAAS,CAAC;AAErC;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAC;AAE3C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAkC;IACrE,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IACxB,OAAO;QACL,eAAe,EAAE,mBAAmB;QACpC,IAAI,EAAE,QAAQ;QACd,cAAc,EAAE,mBAAmB;KACpC,CAAC;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -4,6 +4,8 @@
4
4
  * that sets its own headers, a CSP it writes itself). No imports, no state:
5
5
  * safe in every bundle.
6
6
  */
7
- export { DRAFT_COOKIE, DRAFT_HEADER, DRAFT_PARAM, draftResponseHeaders } from "./draft.ts";
7
+ export { ASSIGNMENTS_HEADER } from "./assignments.ts";
8
+ export { DRAFT_COOKIE, DRAFT_PARAM, draftResponseHeaders, VERSION_HEADER } from "./draft.ts";
8
9
  export { isStudioOrigin, STUDIO_FRAME_ANCESTORS, STUDIO_ORIGINS } from "./studio.ts";
10
+ export { VISITOR_HEADER } from "./visitor.ts";
9
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAC3F,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC7F,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACrF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -4,6 +4,8 @@
4
4
  * that sets its own headers, a CSP it writes itself). No imports, no state:
5
5
  * safe in every bundle.
6
6
  */
7
- export { DRAFT_COOKIE, DRAFT_HEADER, DRAFT_PARAM, draftResponseHeaders } from "./draft.js";
7
+ export { ASSIGNMENTS_HEADER } from "./assignments.js";
8
+ export { DRAFT_COOKIE, DRAFT_PARAM, draftResponseHeaders, VERSION_HEADER } from "./draft.js";
8
9
  export { isStudioOrigin, STUDIO_FRAME_ANCESTORS, STUDIO_ORIGINS } from "./studio.js";
10
+ export { VISITOR_HEADER } from "./visitor.js";
9
11
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAC3F,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC7F,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACrF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC"}
@@ -1,5 +1,5 @@
1
1
  export interface RequestScope {
2
- /** The CDN segment token for this request's page, or undefined when it may not be cached. */
2
+ /** The CDN segment token for this request's page (the site's `pageCache` passes it to `render`), or undefined. */
3
3
  csegToken?: string;
4
4
  /** The draft this request renders (after the worker's host check). */
5
5
  draft?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/scope/server.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,YAAY;IAC3B,6FAA6F;IAC7F,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,sEAAsE;IACtE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAQD,qDAAqD;AACrD,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAEjE;AAED,4FAA4F;AAC5F,wBAAgB,YAAY,IAAI,YAAY,GAAG,SAAS,CAEvD;AAED,4EAA4E;AAC5E,eAAO,MAAM,oBAAoB,EAA4B,qBAAqB,GAAG,cAAc,CAAC"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/scope/server.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,YAAY;IAC3B,kHAAkH;IAClH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,sEAAsE;IACtE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAQD,qDAAqD;AACrD,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAEjE;AAED,4FAA4F;AAC5F,wBAAgB,YAAY,IAAI,YAAY,GAAG,SAAS,CAEvD;AAED,4EAA4E;AAC5E,eAAO,MAAM,oBAAoB,EAA4B,qBAAqB,GAAG,cAAc,CAAC"}
@@ -2,23 +2,50 @@
2
2
  * Server-only helpers for a site's server functions and page loader. Never
3
3
  * import this from a component that ships to the browser.
4
4
  *
5
- * The draft a request renders is decided once, by `createDecoWorker` (its host
6
- * allowlist), and handed to the app in the `x-deco-draft` request header; the
7
- * worker removes any copy that arrives from outside. These read that header.
5
+ * What a request reads is decided once, by `createDecoWorker`, and handed to
6
+ * the app in request headers the worker removes when they arrive from
7
+ * outside: the version on `x-deco-version` (a Studio link's draft or old
8
+ * release, after the host allowlist), the visitor on `x-deco-visitor`
9
+ * (`readVisitor` from `@decocms/blocks/experiments`) and each test and
10
+ * campaign on or off on `x-deco-assignments`. These read them, and
11
+ * `contentClient` builds `cms.client({ version, experiments, campaigns })`
12
+ * from them: the page decides nothing again.
8
13
  */
9
14
  import type { Client, CMS } from "@decocms/blocks";
10
- /** The draft `request` renders (checked by the worker), or `null` for the release. */
11
- export declare function requestDraft(request?: Request): string | null;
15
+ import { type Visitor } from "@decocms/blocks/experiments";
16
+ import { type DecidedAssignments } from "../assignments.ts";
12
17
  /**
13
- * The content client for a request: its draft's, or the release's. Without
14
- * `request`, the request being served (`getRequest()`).
18
+ * The version `request` renders (a Studio link's draft pointer or release
19
+ * SHA, checked by the worker), or `null` for the current release.
20
+ */
21
+ export declare function requestVersion(request?: Request): string | null;
22
+ /**
23
+ * The visitor the worker read for `request`, or `null` when there is none (a
24
+ * request that didn't come through `createDecoWorker`).
25
+ */
26
+ export declare function requestVisitor(request?: Request): Visitor | null;
27
+ /**
28
+ * Each test and campaign on or off as the worker decided them for `request`,
29
+ * or `null` when it decided none (a request that didn't come through
30
+ * `createDecoWorker`).
31
+ */
32
+ export declare function requestAssignments(request?: Request): DecidedAssignments | null;
33
+ /**
34
+ * The content client for a request: `cms.client({ version, experiments,
35
+ * campaigns })` with what the worker decided, so the page renders the tests
36
+ * and campaigns the worker keyed its cache on and a preview reads its draft
37
+ * or old release. The visitor rides along (`client.visitor`) but decides
38
+ * nothing the worker already did; without the worker's decisions (a request
39
+ * that didn't come through it) the visitor decides. Create one per request.
40
+ * Without `request`, the request being served (`getRequest()`).
15
41
  */
16
42
  export declare function contentClient(cms: CMS, request?: Request): Client;
17
43
  /** True when the browser called a server function over HTTP; false while a document renders. */
18
44
  export declare function isServerFnCall(request?: Request): boolean;
19
45
  /**
20
46
  * Marks a server function's response as per-visitor (`Cache-Control: private,
21
- * no-store`): the worker never stores it and the CDN never keeps it. Only when
47
+ * no-store`): a site's page cache never stores it (the example worker entry the README links) and
48
+ * the CDN never keeps it. Only when
22
49
  * the browser called the function over HTTP; while a document renders the
23
50
  * function is a plain call and the header would land on the page.
24
51
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAInD,sFAAsF;AACtF,wBAAgB,YAAY,CAAC,OAAO,GAAE,OAAsB,GAAG,MAAM,GAAG,IAAI,CAE3E;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,OAAsB,GAAG,MAAM,CAG/E;AAED,gGAAgG;AAChG,wBAAgB,cAAc,CAAC,OAAO,GAAE,OAAsB,GAAG,OAAO,CAEvE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,IAAI,IAAI,CAGlC;AAED,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAgB,KAAK,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEzE,OAAO,EAAsB,KAAK,kBAAkB,EAAoB,MAAM,mBAAmB,CAAC;AAIlG;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,OAAsB,GAAG,MAAM,GAAG,IAAI,CAE7E;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,OAAsB,GAAG,OAAO,GAAG,IAAI,CAE9E;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,OAAsB,GAAG,kBAAkB,GAAG,IAAI,CAE7F;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,OAAsB,GAAG,MAAM,CAQ/E;AAED,gGAAgG;AAChG,wBAAgB,cAAc,CAAC,OAAO,GAAE,OAAsB,GAAG,OAAO,CAEvE;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,IAAI,IAAI,CAGlC;AAED,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
@@ -1,16 +1,47 @@
1
+ import { parseVisitor } from "@decocms/blocks/experiments";
1
2
  import { getRequest, setResponseHeader } from "@tanstack/react-start/server";
2
- import { DRAFT_HEADER } from "../draft.js";
3
- /** The draft `request` renders (checked by the worker), or `null` for the release. */
4
- export function requestDraft(request = getRequest()) {
5
- return request.headers.get(DRAFT_HEADER) || null;
3
+ import { ASSIGNMENTS_HEADER, parseAssignments } from "../assignments.js";
4
+ import { VERSION_HEADER } from "../draft.js";
5
+ import { VISITOR_HEADER } from "../visitor.js";
6
+ /**
7
+ * The version `request` renders (a Studio link's draft pointer or release
8
+ * SHA, checked by the worker), or `null` for the current release.
9
+ */
10
+ export function requestVersion(request = getRequest()) {
11
+ return request.headers.get(VERSION_HEADER) || null;
12
+ }
13
+ /**
14
+ * The visitor the worker read for `request`, or `null` when there is none (a
15
+ * request that didn't come through `createDecoWorker`).
16
+ */
17
+ export function requestVisitor(request = getRequest()) {
18
+ return parseVisitor(request.headers.get(VISITOR_HEADER));
19
+ }
20
+ /**
21
+ * Each test and campaign on or off as the worker decided them for `request`,
22
+ * or `null` when it decided none (a request that didn't come through
23
+ * `createDecoWorker`).
24
+ */
25
+ export function requestAssignments(request = getRequest()) {
26
+ return parseAssignments(request.headers.get(ASSIGNMENTS_HEADER));
6
27
  }
7
28
  /**
8
- * The content client for a request: its draft's, or the release's. Without
9
- * `request`, the request being served (`getRequest()`).
29
+ * The content client for a request: `cms.client({ version, experiments,
30
+ * campaigns })` with what the worker decided, so the page renders the tests
31
+ * and campaigns the worker keyed its cache on and a preview reads its draft
32
+ * or old release. The visitor rides along (`client.visitor`) but decides
33
+ * nothing the worker already did; without the worker's decisions (a request
34
+ * that didn't come through it) the visitor decides. Create one per request.
35
+ * Without `request`, the request being served (`getRequest()`).
10
36
  */
11
37
  export function contentClient(cms, request = getRequest()) {
12
- const pointer = requestDraft(request);
13
- return pointer ? cms.forDraft(pointer) : cms.forRelease();
38
+ const decided = requestAssignments(request);
39
+ return cms.client({
40
+ version: requestVersion(request),
41
+ visitor: requestVisitor(request),
42
+ experiments: decided?.experiments,
43
+ campaigns: decided?.campaigns,
44
+ });
14
45
  }
15
46
  /** True when the browser called a server function over HTTP; false while a document renders. */
16
47
  export function isServerFnCall(request = getRequest()) {
@@ -18,7 +49,8 @@ export function isServerFnCall(request = getRequest()) {
18
49
  }
19
50
  /**
20
51
  * Marks a server function's response as per-visitor (`Cache-Control: private,
21
- * no-store`): the worker never stores it and the CDN never keeps it. Only when
52
+ * no-store`): a site's page cache never stores it (the example worker entry the README links) and
53
+ * the CDN never keeps it. Only when
22
54
  * the browser called the function over HTTP; while a document renders the
23
55
  * function is a plain call and the header would land on the page.
24
56
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,sFAAsF;AACtF,MAAM,UAAU,YAAY,CAAC,OAAO,GAAY,UAAU,EAAE;IAC1D,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AACnD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,GAAQ,EAAE,OAAO,GAAY,UAAU,EAAE;IACrE,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IACtC,OAAO,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC;AAC5D,CAAC;AAED,gGAAgG;AAChG,MAAM,UAAU,cAAc,CAAC,OAAO,GAAY,UAAU,EAAE;IAC5D,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW;IACzB,IAAI,CAAC,cAAc,EAAE;QAAE,OAAO;IAC9B,iBAAiB,CAAC,eAAe,EAAE,mBAAmB,CAAC,CAAC;AAC1D,CAAC;AAED,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,YAAY,EAAgB,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAA2B,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,OAAO,GAAY,UAAU,EAAE;IAC5D,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,IAAI,CAAC;AACrD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,OAAO,GAAY,UAAU,EAAE;IAC5D,OAAO,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC;AAC3D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAO,GAAY,UAAU,EAAE;IAChE,OAAO,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC;AACnE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAC,GAAQ,EAAE,OAAO,GAAY,UAAU,EAAE;IACrE,MAAM,OAAO,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAC5C,OAAO,GAAG,CAAC,MAAM,CAAC;QAChB,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC;QAChC,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC;QAChC,WAAW,EAAE,OAAO,EAAE,WAAW;QACjC,SAAS,EAAE,OAAO,EAAE,SAAS;KAC9B,CAAC,CAAC;AACL,CAAC;AAED,gGAAgG;AAChG,MAAM,UAAU,cAAc,CAAC,OAAO,GAAY,UAAU,EAAE;IAC5D,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;AACjE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW;IACzB,IAAI,CAAC,cAAc,EAAE;QAAE,OAAO;IAC9B,iBAAiB,CAAC,eAAe,EAAE,mBAAmB,CAAC,CAAC;AAC1D,CAAC;AAED,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
package/dist/shared.d.ts CHANGED
@@ -4,8 +4,8 @@
4
4
  */
5
5
  /**
6
6
  * The query parameter `decoServerFnFetch` adds to a server-function URL: the
7
- * page's CDN segment token, which the worker checks before it lets the CDN
8
- * cache the response. `__d` is avoided: v7 used it as a debug flag.
7
+ * page's CDN segment token, which a site's page cache checks before it lets
8
+ * the CDN cache the response. `__d` is avoided: v7 used it as a debug flag.
9
9
  */
10
10
  export declare const CSEG_PARAM = "__cseg";
11
11
  /** The `window` property the page publishes the token on. */
@@ -18,9 +18,9 @@ export declare const CSEG_GLOBAL = "__DECO_CSEG";
18
18
  */
19
19
  export declare const PAGE_STATUS_HEADER = "x-page-status";
20
20
  /**
21
- * The response header a page sets when a loader it depends on failed: the
22
- * worker never stores that response, and serves a stale copy instead when it
23
- * has one.
21
+ * The response header a page sets when a loader it depends on failed: a
22
+ * site's page cache never stores that response, and serves a stale copy
23
+ * instead when it has one (the example worker entry the README links).
24
24
  */
25
25
  export declare const DEGRADED_HEADER = "x-deco-degraded";
26
26
  //# sourceMappingURL=shared.d.ts.map
package/dist/shared.js CHANGED
@@ -4,8 +4,8 @@
4
4
  */
5
5
  /**
6
6
  * The query parameter `decoServerFnFetch` adds to a server-function URL: the
7
- * page's CDN segment token, which the worker checks before it lets the CDN
8
- * cache the response. `__d` is avoided: v7 used it as a debug flag.
7
+ * page's CDN segment token, which a site's page cache checks before it lets
8
+ * the CDN cache the response. `__d` is avoided: v7 used it as a debug flag.
9
9
  */
10
10
  export const CSEG_PARAM = "__cseg";
11
11
  /** The `window` property the page publishes the token on. */
@@ -18,9 +18,9 @@ export const CSEG_GLOBAL = "__DECO_CSEG";
18
18
  */
19
19
  export const PAGE_STATUS_HEADER = "x-page-status";
20
20
  /**
21
- * The response header a page sets when a loader it depends on failed: the
22
- * worker never stores that response, and serves a stale copy instead when it
23
- * has one.
21
+ * The response header a page sets when a loader it depends on failed: a
22
+ * site's page cache never stores that response, and serves a stale copy
23
+ * instead when it has one (the example worker entry the README links).
24
24
  */
25
25
  export const DEGRADED_HEADER = "x-deco-degraded";
26
26
  //# sourceMappingURL=shared.js.map
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * `fetch` for server-function calls: adds the CDN segment token the page
3
3
  * published (`__cseg`), so the CDN can cache client-side navigation data. The
4
- * token is a hint: the worker recomputes it from the request and keeps
5
- * `CDN-Cache-Control: no-store` unless they match. With no token on the page,
6
- * a plain `fetch`.
4
+ * token is a hint: the site's page cache (the example worker entry the README links)
5
+ * recomputes it from the request and lets the CDN keep the response only when
6
+ * they match. With no token on the page, a plain `fetch`.
7
7
  */
8
8
  export declare const decoServerFnFetch: typeof fetch;
9
9
  //# sourceMappingURL=index.d.ts.map
@@ -16,9 +16,9 @@ function publishedToken() {
16
16
  /**
17
17
  * `fetch` for server-function calls: adds the CDN segment token the page
18
18
  * published (`__cseg`), so the CDN can cache client-side navigation data. The
19
- * token is a hint: the worker recomputes it from the request and keeps
20
- * `CDN-Cache-Control: no-store` unless they match. With no token on the page,
21
- * a plain `fetch`.
19
+ * token is a hint: the site's page cache (the example worker entry the README links)
20
+ * recomputes it from the request and lets the CDN keep the response only when
21
+ * they match. With no token on the page, a plain `fetch`.
22
22
  */
23
23
  export const decoServerFnFetch = (input, init) => {
24
24
  // TanStack's server-function fetcher passes the URL as a string; anything else goes through as is.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * How the worker hands the app the visitor: the request header it sets on a
3
+ * page or server-function request once it read the visitor (`readVisitor`
4
+ * from `@decocms/blocks/experiments`, a new one on a first visit). The worker
5
+ * writes it (`./worker/index.ts`); the app reads it (`contentClient` from
6
+ * `@decocms/tanstack/server`). No imports, no state: safe in every bundle.
7
+ */
8
+ /**
9
+ * The request header carrying the visitor, in its stored form (the
10
+ * `deco_segment` cookie's value, `serializeVisitor`). The worker removes any
11
+ * copy that arrives from outside, so a client can't send one itself.
12
+ */
13
+ export declare const VISITOR_HEADER = "x-deco-visitor";
14
+ //# sourceMappingURL=visitor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../src/visitor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;GAIG;AACH,eAAO,MAAM,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * How the worker hands the app the visitor: the request header it sets on a
3
+ * page or server-function request once it read the visitor (`readVisitor`
4
+ * from `@decocms/blocks/experiments`, a new one on a first visit). The worker
5
+ * writes it (`./worker/index.ts`); the app reads it (`contentClient` from
6
+ * `@decocms/tanstack/server`). No imports, no state: safe in every bundle.
7
+ */
8
+ /**
9
+ * The request header carrying the visitor, in its stored form (the
10
+ * `deco_segment` cookie's value, `serializeVisitor`). The worker removes any
11
+ * copy that arrives from outside, so a client can't send one itself.
12
+ */
13
+ export const VISITOR_HEADER = "x-deco-visitor";
14
+ //# sourceMappingURL=visitor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor.js","sourceRoot":"","sources":["../src/visitor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC"}