@decocms/tanstack 8.5.2 → 8.7.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 (86) 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 +28 -9
  7. package/dist/draft.d.ts.map +1 -1
  8. package/dist/draft.js +30 -11
  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/react/DecoDocument.d.ts.map +1 -1
  15. package/dist/react/DecoDocument.js +4 -4
  16. package/dist/react/DecoDocument.js.map +1 -1
  17. package/dist/react/pageValues.d.ts +3 -0
  18. package/dist/react/pageValues.d.ts.map +1 -1
  19. package/dist/react/pageValues.js +13 -4
  20. package/dist/react/pageValues.js.map +1 -1
  21. package/dist/scope/server.d.ts +6 -1
  22. package/dist/scope/server.d.ts.map +1 -1
  23. package/dist/scope/server.js.map +1 -1
  24. package/dist/server/index.d.ts +37 -8
  25. package/dist/server/index.d.ts.map +1 -1
  26. package/dist/server/index.js +45 -9
  27. package/dist/server/index.js.map +1 -1
  28. package/dist/shared.d.ts +5 -5
  29. package/dist/shared.js +5 -5
  30. package/dist/start/index.d.ts +3 -3
  31. package/dist/start/index.js +3 -3
  32. package/dist/visitor.d.ts +14 -0
  33. package/dist/visitor.d.ts.map +1 -0
  34. package/dist/visitor.js +14 -0
  35. package/dist/visitor.js.map +1 -0
  36. package/dist/worker/assignments.d.ts +44 -0
  37. package/dist/worker/assignments.d.ts.map +1 -0
  38. package/dist/worker/assignments.js +114 -0
  39. package/dist/worker/assignments.js.map +1 -0
  40. package/dist/worker/cache.d.ts +35 -0
  41. package/dist/worker/cache.d.ts.map +1 -0
  42. package/dist/worker/cache.js +45 -0
  43. package/dist/worker/cache.js.map +1 -0
  44. package/dist/worker/cookies.d.ts +1 -5
  45. package/dist/worker/cookies.d.ts.map +1 -1
  46. package/dist/worker/cookies.js +1 -26
  47. package/dist/worker/cookies.js.map +1 -1
  48. package/dist/worker/drafts.d.ts +54 -46
  49. package/dist/worker/drafts.d.ts.map +1 -1
  50. package/dist/worker/drafts.js +88 -88
  51. package/dist/worker/drafts.js.map +1 -1
  52. package/dist/worker/index.d.ts +82 -51
  53. package/dist/worker/index.d.ts.map +1 -1
  54. package/dist/worker/index.js +197 -372
  55. package/dist/worker/index.js.map +1 -1
  56. package/dist/worker/segment.d.ts +7 -6
  57. package/dist/worker/segment.d.ts.map +1 -1
  58. package/dist/worker/segment.js +6 -5
  59. package/dist/worker/segment.js.map +1 -1
  60. package/dist/worker/serverFn.d.ts +12 -0
  61. package/dist/worker/serverFn.d.ts.map +1 -0
  62. package/dist/worker/serverFn.js +44 -0
  63. package/dist/worker/serverFn.js.map +1 -0
  64. package/package.json +3 -3
  65. package/src/assignments.ts +77 -0
  66. package/src/draft.ts +34 -11
  67. package/src/index.ts +3 -1
  68. package/src/react/DecoDocument.tsx +20 -4
  69. package/src/react/pageValues.ts +12 -4
  70. package/src/scope/server.ts +6 -1
  71. package/src/server/index.ts +57 -12
  72. package/src/shared.ts +5 -5
  73. package/src/start/index.ts +3 -3
  74. package/src/visitor.ts +14 -0
  75. package/src/worker/assignments.ts +146 -0
  76. package/src/worker/cache.ts +61 -0
  77. package/src/worker/cookies.ts +1 -27
  78. package/src/worker/drafts.ts +113 -98
  79. package/src/worker/index.ts +276 -436
  80. package/src/worker/segment.ts +7 -7
  81. package/src/worker/serverFn.ts +36 -0
  82. package/dist/worker/cacheProfiles.d.ts +0 -43
  83. package/dist/worker/cacheProfiles.d.ts.map +0 -1
  84. package/dist/worker/cacheProfiles.js +0 -162
  85. package/dist/worker/cacheProfiles.js.map +0 -1
  86. package/src/worker/cacheProfiles.ts +0 -180
@@ -1,59 +1,67 @@
1
1
  /**
2
- * Which hosts may render drafts: v7's allowlist (`@decocms/blocks` 7.x
3
- * `cms/draftSource.ts`: `readAllowedHosts`, `isDraftHostAllowed`,
4
- * `isDraftPreviewEnabled`), ported as is. v7 read its override from the
5
- * `DECO_ALLOWED_PREVIEW_HOSTS` variable; here it is the `previewHosts` option.
6
- *
7
- * - The site's own preview hosts: the ones it declares, in the `CMS` block or
8
- * as `createCMS({ preview: { hosts } })` (matched as `cms.draftPointer`
9
- * matches them). A site that declares none has none, as in v7: blocks'
10
- * own default (every host) doesn't count as a declaration.
11
- * - The hosts Deco runs the site on, named after the site and always added:
12
- * `<site>.deco.site` and `<site>.deco-cx.workers.dev`, exact.
13
- * - `previewHosts: [...]` replaces the site's own list (the inferred hosts are
14
- * still added); `previewHosts: "none"` turns previews off on every host.
15
- *
16
- * Hosts compare exactly, lowercased, port included (`localhost:5173`).
2
+ * Which hosts may render drafts: the ones the CMS allows (`cms.readVersion`):
3
+ * the addresses Studio writes into the current release's metadata, plus
4
+ * `createCMS`'s `preview.hosts`; every host while the site has published no
5
+ * list, or with no `site` and no `preview.hosts`. The site declares nothing
6
+ * here (PO 2026-10-22).
17
7
  *
18
8
  * The draft itself is the capability: a host check only bounds where a draft
19
9
  * link works. A request the check refuses gets the release, never an error.
10
+ *
11
+ * What a link carries is a version, as `cms.readVersion` reads it: a draft
12
+ * pointer, or a published release's commit SHA (Studio's "Preview this
13
+ * version"), with the preview's moment and switched tests and campaigns
14
+ * (`__at`, `__test`, `__campaign`; `parseVersion` from `@decocms/blocks`).
15
+ * Both pass the same host check: a visitor on the live site never picks a
16
+ * version.
20
17
  */
21
- import { type CMS } from "@decocms/blocks";
22
- export interface PreviewPolicy {
23
- /** The site's name: `<site>.deco.site` and `<site>.deco-cx.workers.dev` may preview. */
24
- site?: string;
25
- /** Replaces the `CMS` block's preview hosts; `"none"` turns previews off everywhere. */
26
- previewHosts?: readonly string[] | "none";
27
- }
28
- /** The hosts Deco runs `site` on, which may always preview (unless previews are off). */
29
- export declare function inferredPreviewHosts(site: string | undefined): string[];
30
- /**
31
- * Whether `host` may preview without asking the CMS: `true` (an override or
32
- * inferred host), `false` (previews off, or the override leaves it out), or
33
- * `"cms"` when only the site's declared preview hosts can say (none declared:
34
- * `false`, see {@link declaresPreviewHosts}).
35
- */
36
- export declare function previewHostDecision(host: string, policy: PreviewPolicy): boolean | "cms";
18
+ import { type CMS, type RequestVersion } from "@decocms/blocks";
37
19
  /**
38
- * Whether the site declares its own preview hosts: in the `CMS` block, or as
39
- * `createCMS`'s `preview.hosts`. When it declares none, `cms.settings()`
40
- * reports blocks' default, every host (`["*"]`), which v7 never had: there,
41
- * a site without a list previewed only on its Deco hosts. The saved `CMS`
42
- * block tells an explicit `["*"]` from that default.
20
+ * What a request's Studio link asks for, as `@decocms/blocks` reads it:
21
+ * `?__draft=` first, then the draft cookie; `null` for `?__draft=off` and
22
+ * for a value that doesn't parse.
43
23
  */
44
- export declare function declaresPreviewHosts(cms: Pick<CMS, "settings" | "forRelease">): Promise<boolean>;
24
+ export declare function readDraftVersion(request: Request): RequestVersion | null;
45
25
  /**
46
- * The pointer a request carries, as `@decocms/blocks` reads it: `?__draft=`
47
- * first, then the draft cookie; `null` for `?__draft=off` and for a value
48
- * that doesn't parse.
26
+ * Whether a request carries a draft signal at all: `?__draft=` with any value
27
+ * (`off` and a value that doesn't parse too) or the draft cookie. Such a
28
+ * request is a preview: it never goes through a page cache and its response
29
+ * is never cached (`draftResponseHeaders`), whatever the host check decides.
49
30
  */
50
- export declare function readDraftPointer(request: Request): string | null;
31
+ export declare function hasDraftSignal(request: Pick<Request, "url" | "headers">): boolean;
51
32
  export interface DraftDecision {
52
- /** The draft this request renders, after the host check; `null` for the release. */
53
- pointer: string | null;
54
- /** The `Set-Cookie` the response carries: storing a draft link's pointer, or ending a preview. */
33
+ /**
34
+ * What this request's Studio link asks for, after the host check: the
35
+ * version (a draft pointer, or a release's commit SHA) and the preview's
36
+ * moment and switched tests and campaigns, in the shape of
37
+ * `cms.client()`'s options; `null` for the current release.
38
+ */
39
+ version: RequestVersion | null;
40
+ /**
41
+ * The `Set-Cookie` the response carries: storing a link's version (the
42
+ * worker sends it only with a response below 500), or ending a preview.
43
+ */
55
44
  setCookie: string | null;
45
+ /**
46
+ * The link named a draft that can't be read (deleted, or its host
47
+ * failing): the request renders the release, the cookie is expired when
48
+ * the draft is gone (a failure that may pass leaves it), and the page says so calmly (`DraftBadge`'s `unavailable`), never a 500.
49
+ */
50
+ unavailable?: true;
51
+ /**
52
+ * The release check made for this draft request (`cms.checkRelease()`):
53
+ * the host check, the read below and the page's draft reads all wait for
54
+ * it, so the request reads `latest.json` once.
55
+ */
56
+ releaseCheck?: string;
56
57
  }
57
- /** The draft a request renders, and the cookie its response sets, under `policy`. */
58
- export declare function decideDraft(cms: Pick<CMS, "draftPointer" | "settings" | "forRelease">, request: Request, policy: PreviewPolicy): Promise<DraftDecision>;
58
+ /**
59
+ * The version a request renders, and the cookie its response sets. A draft
60
+ * request first checks for the latest release, once (`cms.checkRelease()`),
61
+ * and the host check and every draft read share that check. A draft
62
+ * pointer is read once before the page renders (`forDraft(...).revision()`,
63
+ * the same read the page makes, shared while in flight): a draft that can't
64
+ * be read gives the release and `unavailable`, never an error page.
65
+ */
66
+ export declare function decideDraft(cms: Pick<CMS, "readVersion" | "forDraft" | "checkRelease">, request: Request): Promise<DraftDecision>;
59
67
  //# sourceMappingURL=drafts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"drafts.d.ts","sourceRoot":"","sources":["../../src/worker/drafts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,KAAK,GAAG,EAAqB,MAAM,iBAAiB,CAAC;AAG9D,MAAM,WAAW,aAAa;IAC5B,wFAAwF;IACxF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wFAAwF;IACxF,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC;CAC3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;;OAKG;IACH,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;IAC/B;;;OAGG;IACH,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB;;;;OAIG;IACH,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,aAAa,GAAG,UAAU,GAAG,cAAc,CAAC,EAC3D,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,aAAa,CAAC,CA6BxB"}
@@ -1,75 +1,25 @@
1
1
  /**
2
- * Which hosts may render drafts: v7's allowlist (`@decocms/blocks` 7.x
3
- * `cms/draftSource.ts`: `readAllowedHosts`, `isDraftHostAllowed`,
4
- * `isDraftPreviewEnabled`), ported as is. v7 read its override from the
5
- * `DECO_ALLOWED_PREVIEW_HOSTS` variable; here it is the `previewHosts` option.
6
- *
7
- * - The site's own preview hosts: the ones it declares, in the `CMS` block or
8
- * as `createCMS({ preview: { hosts } })` (matched as `cms.draftPointer`
9
- * matches them). A site that declares none has none, as in v7: blocks'
10
- * own default (every host) doesn't count as a declaration.
11
- * - The hosts Deco runs the site on, named after the site and always added:
12
- * `<site>.deco.site` and `<site>.deco-cx.workers.dev`, exact.
13
- * - `previewHosts: [...]` replaces the site's own list (the inferred hosts are
14
- * still added); `previewHosts: "none"` turns previews off on every host.
15
- *
16
- * Hosts compare exactly, lowercased, port included (`localhost:5173`).
2
+ * Which hosts may render drafts: the ones the CMS allows (`cms.readVersion`):
3
+ * the addresses Studio writes into the current release's metadata, plus
4
+ * `createCMS`'s `preview.hosts`; every host while the site has published no
5
+ * list, or with no `site` and no `preview.hosts`. The site declares nothing
6
+ * here (PO 2026-10-22).
17
7
  *
18
8
  * The draft itself is the capability: a host check only bounds where a draft
19
9
  * link works. A request the check refuses gets the release, never an error.
10
+ *
11
+ * What a link carries is a version, as `cms.readVersion` reads it: a draft
12
+ * pointer, or a published release's commit SHA (Studio's "Preview this
13
+ * version"), with the preview's moment and switched tests and campaigns
14
+ * (`__at`, `__test`, `__campaign`; `parseVersion` from `@decocms/blocks`).
15
+ * Both pass the same host check: a visitor on the live site never picks a
16
+ * version.
20
17
  */
21
- import { parseDraftPointer } from "@decocms/blocks";
18
+ import { parseDraftPointer, parseVersion } from "@decocms/blocks";
22
19
  import { DRAFT_COOKIE, DRAFT_PARAM } from "../draft.js";
23
- /** The hosts Deco runs `site` on, which may always preview (unless previews are off). */
24
- export function inferredPreviewHosts(site) {
25
- const s = (site ?? "").trim().toLowerCase();
26
- return s ? [`${s}.deco.site`, `${s}.deco-cx.workers.dev`] : [];
27
- }
28
- /**
29
- * Whether `host` may preview without asking the CMS: `true` (an override or
30
- * inferred host), `false` (previews off, or the override leaves it out), or
31
- * `"cms"` when only the site's declared preview hosts can say (none declared:
32
- * `false`, see {@link declaresPreviewHosts}).
33
- */
34
- export function previewHostDecision(host, policy) {
35
- const override = policy.previewHosts === "none"
36
- ? ["none"]
37
- : (policy.previewHosts ?? []).map((h) => h.trim().toLowerCase()).filter(Boolean);
38
- if (override.includes("none"))
39
- return false;
40
- const h = host.trim().toLowerCase();
41
- if (h === "")
42
- return false;
43
- if (inferredPreviewHosts(policy.site).includes(h))
44
- return true;
45
- if (policy.previewHosts !== undefined)
46
- return override.includes(h);
47
- return "cms";
48
- }
49
- /**
50
- * Whether the site declares its own preview hosts: in the `CMS` block, or as
51
- * `createCMS`'s `preview.hosts`. When it declares none, `cms.settings()`
52
- * reports blocks' default, every host (`["*"]`), which v7 never had: there,
53
- * a site without a list previewed only on its Deco hosts. The saved `CMS`
54
- * block tells an explicit `["*"]` from that default.
55
- */
56
- export async function declaresPreviewHosts(cms) {
57
- const { hosts } = (await cms.settings()).preview;
58
- if (hosts.length !== 1 || hosts[0] !== "*")
59
- return true;
60
- try {
61
- const [block] = await cms.forRelease().resolve("CMS", { run: false });
62
- const preview = isRecord(block) ? block.preview : undefined;
63
- return isRecord(preview) && ("hosts" in preview || "__resolveType" in preview);
64
- }
65
- catch {
66
- return false;
67
- }
68
- }
69
- function isRecord(value) {
70
- return typeof value === "object" && value !== null && !Array.isArray(value);
71
- }
72
20
  const DRAFT_OFF = "off";
21
+ /** A draft pointer's version that names no draft: the release, nothing fetched. */
22
+ const LOCAL_VERSION = "local";
73
23
  function draftParam(url) {
74
24
  return url.searchParams.get(DRAFT_PARAM);
75
25
  }
@@ -90,18 +40,33 @@ function readCookie(header, name) {
90
40
  return null;
91
41
  }
92
42
  /**
93
- * The pointer a request carries, as `@decocms/blocks` reads it: `?__draft=`
94
- * first, then the draft cookie; `null` for `?__draft=off` and for a value
95
- * that doesn't parse.
43
+ * What a request's Studio link asks for, as `@decocms/blocks` reads it:
44
+ * `?__draft=` first, then the draft cookie; `null` for `?__draft=off` and
45
+ * for a value that doesn't parse.
96
46
  */
97
- export function readDraftPointer(request) {
47
+ export function readDraftVersion(request) {
98
48
  const param = draftParam(new URL(request.url));
99
49
  if (param === DRAFT_OFF)
100
50
  return null;
101
- if (param && parseDraftPointer(param) !== null)
102
- return param;
103
- const cookie = readCookie(request.headers.get("cookie"), DRAFT_COOKIE);
104
- return cookie && parseDraftPointer(cookie) !== null ? cookie : null;
51
+ const fromLink = param ? parseVersion(param) : null;
52
+ if (fromLink !== null)
53
+ return fromLink;
54
+ return parseVersion(readCookie(request.headers.get("cookie"), DRAFT_COOKIE));
55
+ }
56
+ /**
57
+ * Whether a request carries a draft signal at all: `?__draft=` with any value
58
+ * (`off` and a value that doesn't parse too) or the draft cookie. Such a
59
+ * request is a preview: it never goes through a page cache and its response
60
+ * is never cached (`draftResponseHeaders`), whatever the host check decides.
61
+ */
62
+ export function hasDraftSignal(request) {
63
+ return (draftParam(new URL(request.url)) !== null ||
64
+ readCookie(request.headers.get("cookie"), DRAFT_COOKIE) !== null);
65
+ }
66
+ /** Whether a version names a saved draft (a pointer, not a release's SHA and not `local`). */
67
+ function namesDraft(version) {
68
+ const parsed = parseDraftPointer(version);
69
+ return parsed !== null && parsed.version !== LOCAL_VERSION;
105
70
  }
106
71
  function serializeCookie(value, extra) {
107
72
  // The same cookie `cms.draftCookie` writes (a test holds the two together).
@@ -115,26 +80,61 @@ function serializeCookie(value, extra) {
115
80
  "Partitioned",
116
81
  ].join("; ");
117
82
  }
118
- /** The draft a request renders, and the cookie its response sets, under `policy`. */
119
- export async function decideDraft(cms, request, policy) {
83
+ /**
84
+ * The version a request renders, and the cookie its response sets. A draft
85
+ * request first checks for the latest release, once (`cms.checkRelease()`),
86
+ * and the host check and every draft read share that check. A draft
87
+ * pointer is read once before the page renders (`forDraft(...).revision()`,
88
+ * the same read the page makes, shared while in flight): a draft that can't
89
+ * be read gives the release and `unavailable`, never an error page.
90
+ */
91
+ export async function decideDraft(cms, request) {
120
92
  const url = new URL(request.url);
121
93
  const param = draftParam(url);
122
94
  // Ending a preview is allowed on any host, so a cookie set earlier always goes.
123
95
  if (param === DRAFT_OFF)
124
- return { pointer: null, setCookie: serializeCookie("", ["Max-Age=0"]) };
125
- const pointer = readDraftPointer(request);
126
- if (pointer === null)
127
- return { pointer: null, setCookie: null };
128
- const decision = previewHostDecision(url.host, policy);
129
- const allowed = decision === "cms"
130
- ? (await declaresPreviewHosts(cms)) && (await cms.draftPointer(request)) !== null
131
- : decision;
132
- if (!allowed)
133
- return { pointer: null, setCookie: null };
134
- const fromLink = param !== null && param === pointer;
96
+ return { version: null, setCookie: expiredCookie() };
97
+ const version = readDraftVersion(request);
98
+ if (version === null)
99
+ return { version: null, setCookie: null };
100
+ // One release check per draft request, shared below and by the page.
101
+ const releaseCheck = namesDraft(version.version) ? await cms.checkRelease() : undefined;
102
+ if ((await cms.readVersion(request, { releaseCheck })) === null) {
103
+ return { version: null, setCookie: null };
104
+ }
105
+ if (releaseCheck !== undefined) {
106
+ try {
107
+ await cms.forDraft(version.version, { releaseCheck }).revision();
108
+ }
109
+ catch (error) {
110
+ // Gone (deleted): the preview ends. Any other failure (the drafts' host
111
+ // down, a timeout) leaves the cookie alone, so the next request tries again.
112
+ const setCookie = isDraftGone(error) ? expiredCookie() : null;
113
+ return { version: null, setCookie, unavailable: true };
114
+ }
115
+ }
116
+ // The link's own value goes in the cookie, its preview parameters with it.
117
+ const fromLink = param !== null && parseVersion(param) !== null;
135
118
  return {
136
- pointer,
137
- setCookie: fromLink ? serializeCookie(encodeURIComponent(pointer), []) : null,
119
+ version,
120
+ setCookie: fromLink ? serializeCookie(encodeURIComponent(param), []) : null,
121
+ ...(releaseCheck === undefined ? {} : { releaseCheck }),
138
122
  };
139
123
  }
124
+ /**
125
+ * Whether a draft read failed because the drafts' host no longer has the
126
+ * draft (`404`/`410`), as opposed to a failure that may pass (a `5xx`, a
127
+ * timeout, a network error). Reads the error and its causes.
128
+ */
129
+ function isDraftGone(error) {
130
+ for (let e = error, depth = 0; e instanceof Error && depth < 5; e = e.cause, depth++) {
131
+ if (/draft changes: HTTP (404|410)\b/.test(e.message))
132
+ return true;
133
+ }
134
+ return false;
135
+ }
136
+ /** The `Set-Cookie` that ends a preview. */
137
+ function expiredCookie() {
138
+ return serializeCookie("", ["Max-Age=0"]);
139
+ }
140
140
  //# sourceMappingURL=drafts.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"drafts.js","sourceRoot":"","sources":["../../src/worker/drafts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAY,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AASxD,yFAAyF;AACzF,MAAM,UAAU,oBAAoB,CAAC,IAAwB;IAC3D,MAAM,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC5C,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,YAAY,EAAE,GAAG,CAAC,sBA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;AACf,CAAC;AAED,4CAA4C;AAC5C,SAAS,aAAa;IACpB,OAAO,eAAe,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;AAC5C,CAAC"}
@@ -5,29 +5,56 @@
5
5
  *
6
6
  * - `/deco/_liveness` answers `OK`; v7's admin paths (`/deco/*`, `/live/*`,
7
7
  * `/.decofile`) answer 404.
8
- * - Drafts: the host allowlist (`./drafts.ts`), the draft cookie, the
9
- * `x-deco-draft` request header the page reads, and on the response
10
- * `no-store`, `Vary: Cookie` and `noindex`. Only a real draft skips the cache.
8
+ * - Versions: the host allowlist (`./drafts.ts`) for a Studio link's version
9
+ * (a draft, or a published release by its commit SHA, with the preview's
10
+ * moment and switched tests and campaigns), the draft cookie, the
11
+ * `x-deco-version` request header the page reads. A preview (any request
12
+ * with `?__draft=`, `off` included, or the draft cookie) never goes through
13
+ * `pageCache` and answers `Cache-Control: max-age=0, no-cache,
14
+ * must-revalidate`, `Vary: Cookie` and `noindex`. A link's cookie is set
15
+ * only with a response below 500; a link to a draft that can't be read
16
+ * anymore renders the release, expires the cookie and tells the page
17
+ * (`draftUnavailable`), never a 500.
11
18
  * - The CMS redirects, before any route.
12
- * - An edge cache (the Cloudflare Cache API) keyed by URL, device, the site's
13
- * segment parts and `<build>.<content revision>`, with stale-while-revalidate
14
- * and stale-if-error; logged-in, private and degraded responses never stored.
15
- * - CDN caching of client-side navigation data whose `__cseg` token checks out;
16
- * every other response tells the CDN `no-store`, at one exit.
19
+ * - No page cache: caching is the site's (`pageCache`, the README's
20
+ * worker-entry recipe), with the helpers it needs exported here
21
+ * (`assignmentsKey`, `cacheVersion`, `isCacheable`, …).
22
+ * The worker calls `pageCache` only for a request `isCacheable` accepts,
23
+ * never for a preview (any draft signal), and adds the visitor's cookie after it, so a stored
24
+ * copy never carries one. A page request with `Authorization` is never
25
+ * handed to it and answers `private, no-cache, no-store`. A response with no
26
+ * `CDN-Cache-Control` tells the CDN `no-store`, at one exit.
17
27
  * - Security headers and `frame-ancestors` for Studio; immutable fingerprinted
18
28
  * assets; one `Set-Cookie` per cookie; `x-page-status`.
29
+ * - The visitor: on a page or server-function request, `readVisitor` from
30
+ * `@decocms/blocks/experiments` (the `deco_segment` cookie, or a new
31
+ * visitor), handed to the app on `x-deco-visitor`; the cookie is sent on a
32
+ * first visit only, never from a preview.
33
+ * - What the request's content gives it, decided once per request: each test
34
+ * and campaign on or off (`readAssignments`: `cms.client({ visitor,
35
+ * version, date, experiments, campaigns })` with the visitor and the Studio
36
+ * link's version, moment and switched tests and campaigns), handed to the
37
+ * site's `pageCache` (`page.assignments`, for its key) and to the app on
38
+ * `x-deco-assignments` (`contentClient` builds
39
+ * `cms.client({ version, experiments, campaigns })` from it, deciding
40
+ * nothing again).
19
41
  * - `http.server.request.duration` for every request, to the CMS's telemetry
20
42
  * (`recordServerRequest` from `@decocms/blocks/metrics`), labelled with the route's template, the
21
- * method, the status class, the cache decision and the device; nothing
22
- * when the CMS has no telemetry.
43
+ * method, the status class, the cache decision (the `X-Cache` the site's
44
+ * `pageCache` answered with) and the device; nothing when the CMS has no
45
+ * telemetry.
23
46
  */
24
47
  import { type CMS } from "@decocms/blocks";
25
- import { type CacheProfileName, type CacheProfileOverrides } from "./cacheProfiles.ts";
48
+ import { type Visitor } from "@decocms/blocks/experiments";
49
+ import { type RequestAssignments } from "./assignments.ts";
26
50
  import { type CspOptions } from "./security.ts";
27
- export type { CacheProfile, CacheProfileName, CacheProfileOverrides, CacheWindow, } from "./cacheProfiles.ts";
28
- export { DEFAULT_CACHE_PROFILES } from "./cacheProfiles.ts";
51
+ import { type Device } from "./segment.ts";
52
+ export { CSEG_PARAM, DEGRADED_HEADER } from "../shared.ts";
53
+ export { assignmentsKey, liveAssignmentKeys, type RequestAssignments, readAssignments, } from "./assignments.ts";
54
+ export { cacheVersion, isCacheable, requestVersion } from "./cache.ts";
29
55
  export type { CspOptions } from "./security.ts";
30
- export type { Device, Segment } from "./segment.ts";
56
+ export { type Device, detectDevice, type Segment, segmentKey, segmentToken } from "./segment.ts";
57
+ export { isServerFnPath, serverFnPagePath } from "./serverFn.ts";
31
58
  /** The part of Cloudflare's `ExecutionContext` the worker uses. */
32
59
  export interface ExecutionContextLike {
33
60
  waitUntil(promise: Promise<unknown>): void;
@@ -40,42 +67,56 @@ export interface ExecutionContextLike {
40
67
  export interface ServerEntry {
41
68
  fetch(request: Request, env?: any, ctx?: any): Response | Promise<Response>;
42
69
  }
43
- /** What the edge cache did with a request (`X-Cache`). */
44
- export type CacheDecision = "HIT" | "STALE-HIT" | "STALE-ERROR" | "MISS" | "BYPASS";
45
- /** One page or server-function request, for `onCache`. */
46
- export interface CacheEvent {
47
- /** The page type (cache profile) the URL maps to. */
48
- profile: CacheProfileName;
49
- decision: CacheDecision;
50
- /** Milliseconds from the request to the response's headers. */
51
- ms: number;
52
- status: number;
70
+ /**
71
+ * A page or server-function request the site's `pageCache` may answer from
72
+ * its own cache: always one `isCacheable` accepts (a GET of the current
73
+ * release, never a preview).
74
+ */
75
+ export interface PageRequest<Env = unknown> {
76
+ /** The request as the app gets it: the visitor and the assignments on their headers. */
77
+ request: Request;
78
+ url: URL;
79
+ /** The visitor the worker read (`readVisitor`). */
80
+ visitor: Visitor;
81
+ /**
82
+ * Each test and campaign on or off for this request, as the worker decided
83
+ * them and the page renders them: key the cache on `assignmentsKey(page.assignments)`.
84
+ */
85
+ assignments: RequestAssignments;
86
+ /** The request's device class, from its user agent. */
87
+ device: Device;
88
+ env: Env;
89
+ ctx: ExecutionContextLike;
90
+ /**
91
+ * Renders the page: the app's response, with `x-page-status` applied and
92
+ * without the visitor's cookie (the worker adds it to whatever `pageCache`
93
+ * answers). `csegToken` is the `__cseg` token `DecoDocument` publishes and
94
+ * `decoServerFnFetch` sends back on the page's navigation data.
95
+ */
96
+ render(scope?: {
97
+ csegToken?: string;
98
+ }): Promise<Response>;
53
99
  }
100
+ /**
101
+ * The site's page cache: answers a page from its own cache, or calls
102
+ * `page.render()`. Set `X-Cache` (`HIT`, `STALE-HIT`, `STALE-ERROR`, `MISS`)
103
+ * on the answer and the request's metric counts it.
104
+ */
105
+ export type PageCache<Env = unknown> = (page: PageRequest<Env>) => Promise<Response>;
54
106
  export interface DecoWorkerOptions<Env = unknown> {
55
107
  serverEntry: ServerEntry;
56
- /** The site's CMS: drafts, the content revision in the cache key, CMS redirects. */
108
+ /** The site's CMS: drafts, the CMS redirects, the page templates of the request metric. */
57
109
  cms: CMS;
58
- /** The site's name: `<site>.deco.site` and `<site>.deco-cx.workers.dev` may preview drafts. */
59
- site: string;
60
- /** The build's id, in every cache key: the commit sha (`decoTanstack()` defines `__BUILD_HASH__`). */
61
- buildHash: string;
62
- /** Local development (`import.meta.env.DEV`): no edge cache, `no-cache` pages, loopback may frame the site. */
110
+ /** Local development (`import.meta.env.DEV`): `pageCache` is never called, loopback may frame the site. */
63
111
  dev?: boolean;
64
112
  /** The site's Content-Security-Policy. Studio's `frame-ancestors` is always added. */
65
113
  csp?: CspOptions;
66
114
  /**
67
- * More cache-key parts for a request (a region, a sales channel, an A/B
68
- * cohort), merged over `{ device }` (`mobile`, `tablet` or `desktop`; return
69
- * your own `device` to merge classes). Return `"private"` for a request that
70
- * must never share a cached response (a signed-in shopper).
115
+ * The site's own page cache (the example worker entry the README links), called for
116
+ * every page and server-function request `isCacheable` accepts. Without
117
+ * it nothing is cached: every request renders.
71
118
  */
72
- segment?: (request: Request) => Record<string, string | undefined> | "private";
73
- /** Changes to the default cache profiles (`DEFAULT_CACHE_PROFILES`). */
74
- cacheProfiles?: CacheProfileOverrides;
75
- /** Cookies a cached response may set (stripped from the stored copy). Default: `_deco_bucket`. */
76
- safeCookies?: readonly string[];
77
- /** Turns on `POST /_cache/purge` with `Authorization: Bearer <token>`. */
78
- purgeToken?: (env: Env) => string | undefined;
119
+ pageCache?: PageCache<Env>;
79
120
  /**
80
121
  * `Link: rel=preload` hints on every HTML response the app renders. A
81
122
  * `routes` response carries none: it is another origin's page (a proxied
@@ -85,14 +126,6 @@ export interface DecoWorkerOptions<Env = unknown> {
85
126
  styles?: readonly string[];
86
127
  fonts?: readonly string[];
87
128
  };
88
- /**
89
- * v7's `DECO_ALLOWED_PREVIEW_HOSTS`: replaces the site's declared preview
90
- * hosts (the `CMS` block's), exact hosts with their port
91
- * (`["localhost:5173"]` for local development); the site's Deco hosts stay
92
- * allowed. `"none"` turns draft previews off on every host. Without it and
93
- * without hosts in the `CMS` block, drafts preview only on the Deco hosts.
94
- */
95
- previewHosts?: readonly string[] | "none";
96
129
  /**
97
130
  * The escape hatch, after the CMS redirects and before anything else: answer
98
131
  * a request yourself (a commerce proxy, a sitemap, an A/B route) or return
@@ -101,8 +134,6 @@ export interface DecoWorkerOptions<Env = unknown> {
101
134
  * but without the `preload` hints.
102
135
  */
103
136
  routes?: (request: Request, env: Env, ctx: ExecutionContextLike) => Response | null | undefined | Promise<Response | null | undefined>;
104
- /** Called once per page or server-function request (D3: no exporter is built in). */
105
- onCache?: (event: CacheEvent) => void;
106
137
  }
107
138
  export interface DecoWorker<Env> {
108
139
  fetch(request: Request, env: Env, ctx: ExecutionContextLike): Promise<Response>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/worker/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,KAAK,GAAG,EAAyC,MAAM,iBAAiB,CAAC;AAKlF,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAO3B,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,KAAK,UAAU,EAAuB,MAAM,eAAe,CAAC;AAGrE,YAAY,EACV,YAAY,EACZ,gBAAgB,EAChB,qBAAqB,EACrB,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,YAAY,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAChD,YAAY,EAAE,MAAM,EA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