@heyplaybook/hydrogen 0.7.0 → 0.7.1

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/index.d.ts CHANGED
@@ -520,6 +520,23 @@ interface SsrDoc {
520
520
  interface SsrPayload {
521
521
  doc: SsrDoc;
522
522
  snapshots: Record<string, SsrSnapshot>;
523
+ /** 0.7.1: the arm a valid `?pb=` preview code names (only when one was sent). */
524
+ preview?: SsrPreview;
525
+ }
526
+ /** 0.7.1: a `?pb=` preview code's arm, as the platform resolved it (shop-scoped, unexpired). */
527
+ interface SsrPreview {
528
+ code: string;
529
+ experimentId: string;
530
+ armId: string;
531
+ experienceId: string | null;
532
+ testName: string;
533
+ armLabel: string;
534
+ pages: string[];
535
+ expiresAt: string;
536
+ arm: SsrArm;
537
+ snapshot: SsrSnapshot | null;
538
+ /** Non-null = the platform can't server-render this arm (the browser preview covers it). */
539
+ why: string | null;
523
540
  }
524
541
  declare function ssrUrl(platformUrl: string, shopDomain: string): string;
525
542
  /** The shop's SSR doc, or null when the read isn't healthy (then: cloak only). Never throws. */
@@ -539,14 +556,20 @@ interface SsrDecision {
539
556
  experimentId: string;
540
557
  armId: string;
541
558
  experienceId: string | null;
542
- /** The visitor id the roll used ('' for a prior / settled arm). */
543
- idSource: 'prior' | 'settled' | 'roll';
559
+ /** How the arm was chosen. `preview` = a `?pb=` code named it (0.7.1). */
560
+ idSource: 'prior' | 'settled' | 'roll' | 'preview';
544
561
  arm: SsrArm;
545
562
  snapshot: SsrSnapshot | null;
546
563
  page: {
547
564
  t: string;
548
565
  h: string;
549
566
  };
567
+ /** 0.7.1: set for a preview-code render — nothing is assigned or recorded. */
568
+ preview?: {
569
+ code: string;
570
+ testName: string;
571
+ armLabel: string;
572
+ };
550
573
  }
551
574
  type SsrOutcome = {
552
575
  decision: SsrDecision;
package/dist/index.js CHANGED
@@ -1086,7 +1086,9 @@ function sanitizeSsrPayload(json) {
1086
1086
  if (!doc || typeof doc !== "object") return null;
1087
1087
  if (!doc.keys || typeof doc.keys !== "object" || !Array.isArray(doc.prefixes) || !Array.isArray(doc.exps)) return null;
1088
1088
  const snapshots = j.snapshots && typeof j.snapshots === "object" ? j.snapshots : {};
1089
- return { doc, snapshots };
1089
+ const p = j.preview;
1090
+ const preview = p && typeof p === "object" && typeof p.experimentId === "string" && typeof p.armId === "string" && Array.isArray(p.pages) && p.arm ? p : void 0;
1091
+ return { doc, snapshots, ...preview ? { preview } : {} };
1090
1092
  }
1091
1093
  var ssrFetcher = createCachedJsonFetcher({ label: "ssr doc", sanitize: sanitizeSsrPayload });
1092
1094
  var HEALTHY2 = /* @__PURE__ */ new Set(["memory", "stale", "fetched"]);
@@ -1102,6 +1104,54 @@ async function getSsrPayload(args) {
1102
1104
  return null;
1103
1105
  }
1104
1106
  }
1107
+ var PREVIEW_CODE_RE = /^[0-9A-HJKMNP-TV-Z]{4,8}$/;
1108
+ function previewCodeFromRequestUrl(url) {
1109
+ const raw = url.searchParams.get("pb");
1110
+ if (!raw) return null;
1111
+ const code = raw.trim().toUpperCase();
1112
+ return PREVIEW_CODE_RE.test(code) ? code : null;
1113
+ }
1114
+ var PREVIEW_FETCH_BUDGET_MS = 1500;
1115
+ async function getSsrPreviewPayload(args) {
1116
+ const controller = new AbortController();
1117
+ const timer = setTimeout(() => controller.abort(), PREVIEW_FETCH_BUDGET_MS);
1118
+ try {
1119
+ const url = new URL(ssrUrl(args.platformUrl, args.shopDomain));
1120
+ url.searchParams.set("pb", args.code);
1121
+ const res = await (args.fetchImpl ?? fetch)(url.toString(), {
1122
+ headers: { accept: "application/json" },
1123
+ signal: controller.signal
1124
+ });
1125
+ if (!res.ok) return null;
1126
+ return sanitizeSsrPayload(await res.json());
1127
+ } catch {
1128
+ return null;
1129
+ } finally {
1130
+ clearTimeout(timer);
1131
+ }
1132
+ }
1133
+ function decidePreviewSsr(payload, pageKey, matchesPage) {
1134
+ const p = payload.preview;
1135
+ if (!p) return { skip: "preview-unresolved" };
1136
+ if (p.why) return { skip: `preview-${p.why}` };
1137
+ if (!pageKey || !matchesPage(p.pages)) return { skip: "preview-not-this-page" };
1138
+ if (p.experienceId && (!p.snapshot || p.snapshot.v !== SSR_SNAPSHOT_VERSION || !p.snapshot.nodes?.length)) {
1139
+ return { skip: "preview-no-snapshot" };
1140
+ }
1141
+ const [t, h = ""] = pageKey.split(":");
1142
+ return {
1143
+ decision: {
1144
+ experimentId: p.experimentId,
1145
+ armId: p.armId,
1146
+ experienceId: p.experienceId,
1147
+ idSource: "preview",
1148
+ arm: p.arm,
1149
+ snapshot: p.snapshot,
1150
+ page: { t, h },
1151
+ preview: { code: p.code, testName: p.testName, armLabel: p.armLabel }
1152
+ }
1153
+ };
1154
+ }
1105
1155
  function fnv32a(s2) {
1106
1156
  let h = 2166136261;
1107
1157
  for (let i = 0; i < s2.length; i++) {
@@ -1211,7 +1261,9 @@ function ssrStateScript(d) {
1211
1261
  reason: d.experienceId ? null : "control",
1212
1262
  idSource: d.idSource,
1213
1263
  placedAt: null,
1214
- server: true
1264
+ server: true,
1265
+ // A preview render: the SDK keeps it without resolving or recording.
1266
+ ...d.preview ? { preview: d.preview } : {}
1215
1267
  };
1216
1268
  return `window.__pbSsr=${JSON.stringify(state).replace(/</g, "\\u003c")};`;
1217
1269
  }
@@ -1348,7 +1400,23 @@ function createPlaybookContext({
1348
1400
  const ssrFor = (cloak) => {
1349
1401
  if (!ssrPromise) {
1350
1402
  ssrPromise = (async () => {
1351
- if (!cloak || ssrSlots.length === 0) return { ssr: null, skip: null };
1403
+ if (ssrSlots.length === 0) return { ssr: null, skip: null };
1404
+ const url = new URL(request.url);
1405
+ const code = request.method === "GET" && !url.pathname.endsWith(".data") ? previewCodeFromRequestUrl(url) : null;
1406
+ if (code && shopId) {
1407
+ const shopDomain = normalizeDomain(shopDomainOption ?? str(e.PUBLIC_PLAYBOOK_SHOP_DOMAIN) ?? url.hostname);
1408
+ if (!shopDomain) return { ssr: null, skip: "preview-no-shop" };
1409
+ const payload2 = await getSsrPreviewPayload({ platformUrl, shopDomain, code, fetchImpl });
1410
+ if (!payload2) return { ssr: null, skip: "preview-unavailable" };
1411
+ const pageKey = targetPageKey(url.pathname);
1412
+ const out2 = decidePreviewSsr(payload2, pageKey, (pages) => matchTargetedPage(pages, url.pathname) !== null);
1413
+ if ("skip" in out2) return { ssr: null, skip: out2.skip };
1414
+ const d2 = out2.decision;
1415
+ if (d2.experienceId && !ssrSlots.some((s2) => slotMatches(d2, s2))) return { ssr: null, skip: "preview-no-slot" };
1416
+ ssrRendered = true;
1417
+ return { ssr: d2, skip: null };
1418
+ }
1419
+ if (!cloak) return { ssr: null, skip: null };
1352
1420
  const payload = await getSsrPayload({ platformUrl, shopDomain: cloak.shopDomain, waitUntil, fetchImpl });
1353
1421
  if (!payload) return { ssr: null, skip: "doc-unavailable" };
1354
1422
  const cookies = parseCookies(request.headers.get("cookie"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heyplaybook/hydrogen",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Playbook store-edit tests for Shopify Hydrogen storefronts: server-side assignment, consent-gated visitor cookie, in-view exposures and cart attribution.",
5
5
  "license": "MIT",
6
6
  "author": "Pack Digital Inc.",