@heyplaybook/hydrogen 0.2.0 → 0.2.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/README.md CHANGED
@@ -62,7 +62,10 @@ for (const cookie of playbook.setCookieHeaders) {
62
62
  ```
63
63
 
64
64
  `createPlaybook` waits at most 300 ms for the test rules (1.5 s on a cold start), then
65
- uses the last good copy, or control for everyone. Rules are cached for 60 seconds. To
65
+ uses the last good copy, or control for everyone. Rules are cached for 60 seconds. If
66
+ the rules fetch fails or hangs, `createPlaybook` stops calling Playbook for 30 seconds
67
+ and renders straight away with the last good copy (or control), so an outage costs
68
+ at most one 1.5 s wait every 30 seconds per worker. To
66
69
  start the rules fetch earlier, call `prefetchRuleset({platformUrl, shopId, waitUntil})`
67
70
  at the top of your request handler.
68
71
 
package/dist/index.d.ts CHANGED
@@ -76,7 +76,7 @@ interface PlaybookState {
76
76
  now: string;
77
77
  rulesetVersion: string | null;
78
78
  /** Where this render's ruleset came from; null when disabled. */
79
- rulesetSource: 'memory' | 'stale' | 'fetched' | 'last-good' | 'cold-fallback' | null;
79
+ rulesetSource: 'memory' | 'stale' | 'fetched' | 'last-good' | 'cold-fallback' | 'backoff' | null;
80
80
  /**
81
81
  * The verified `pb_preview` token, or null. Only present for a merchant's own
82
82
  * verified preview link; lets the client carry `?pb_preview=` across in-app
@@ -149,10 +149,15 @@ declare function createPlaybook({ shopId, platformUrl, request, waitUntil, fetch
149
149
  *
150
150
  * - Fresh for 60s; served stale for a further 300s while one background
151
151
  * refresh runs (stale-while-revalidate).
152
- * - Past that, a render waits at most RENDER_BUDGET_MS for a fresh copy, then
153
- * falls back to the last good ruleset; with none, the caller gets `null`
154
- * and everyone sees control.
152
+ * - Past that, a render waits at most RENDER_BUDGET_MS (COLD_RENDER_BUDGET_MS
153
+ * with nothing cached) for a fresh copy, then falls back to the last good
154
+ * ruleset; with none, the caller gets `null` and everyone sees control.
155
155
  * - A failed or malformed fetch never replaces a good cached copy.
156
+ * - A failure (timeout, network error, non-2xx, malformed body) — or a render
157
+ * that gave up waiting on its budget — opens a FAILURE_BACKOFF_MS window per
158
+ * shop. Inside it no render touches the network or waits: each one gets the
159
+ * last good copy, or control. The first render after the window retries.
160
+ * This keeps a hanging platform from costing every page request its budget.
156
161
  */
157
162
 
158
163
  /**
@@ -163,8 +168,10 @@ declare function createPlaybook({ shopId, platformUrl, request, waitUntil, fetch
163
168
  * - `last-good`: network missed the budget; served the last good copy.
164
169
  * - `cold-fallback`: nothing in memory and the network missed even the
165
170
  * cold budget (or failed) — everyone gets control for this render.
171
+ * - `backoff`: a recent fetch failed, so this render skipped the network and
172
+ * got the last good copy (or `null` — control — with none).
166
173
  */
167
- type RulesetSource = 'memory' | 'stale' | 'fetched' | 'last-good' | 'cold-fallback';
174
+ type RulesetSource = 'memory' | 'stale' | 'fetched' | 'last-good' | 'cold-fallback' | 'backoff';
168
175
  declare function getRulesetWithSource({ platformUrl, shopId, waitUntil, fetchImpl, }: {
169
176
  platformUrl: string;
170
177
  shopId: string;
package/dist/index.js CHANGED
@@ -411,6 +411,7 @@ async function verifyPreviewToken({
411
411
  var FRESH_MS = 6e4;
412
412
  var STALE_MS = 3e5;
413
413
  var RENDER_BUDGET_MS = 300;
414
+ var FAILURE_BACKOFF_MS = 3e4;
414
415
  var FETCH_TIMEOUT_MS = 5e3;
415
416
  var memory = /* @__PURE__ */ new Map();
416
417
  function rulesUrl(platformUrl, shopId) {
@@ -424,6 +425,9 @@ function isRuleset(value) {
424
425
  (t) => t && typeof t.key === "string" && typeof t.status === "string" && Array.isArray(t.arms) && t.arms.every((a) => a && typeof a.key === "string")
425
426
  );
426
427
  }
428
+ function inBackoff(entry, now) {
429
+ return entry.failedAt > 0 && now - entry.failedAt < FAILURE_BACKOFF_MS;
430
+ }
427
431
  function refresh(key, url, entry, fetchImpl) {
428
432
  if (entry.inflight) return entry.inflight;
429
433
  const controller = new AbortController();
@@ -437,8 +441,10 @@ function refresh(key, url, entry, fetchImpl) {
437
441
  if (!isRuleset(json)) throw new Error("ruleset malformed");
438
442
  entry.ruleset = json;
439
443
  entry.fetchedAt = Date.now();
444
+ entry.failedAt = 0;
440
445
  return json;
441
446
  }).catch((error) => {
447
+ entry.failedAt = Date.now();
442
448
  console.warn("[playbook] ruleset fetch failed", String(error));
443
449
  return entry.ruleset;
444
450
  }).finally(() => {
@@ -449,6 +455,7 @@ function refresh(key, url, entry, fetchImpl) {
449
455
  return entry.inflight;
450
456
  }
451
457
  var COLD_RENDER_BUDGET_MS = 1500;
458
+ var BUDGET_MISSED = /* @__PURE__ */ Symbol("budget-missed");
452
459
  async function getRulesetWithSource({
453
460
  platformUrl,
454
461
  shopId,
@@ -460,12 +467,17 @@ async function getRulesetWithSource({
460
467
  const entry = memory.get(key) ?? {
461
468
  ruleset: null,
462
469
  fetchedAt: 0,
470
+ failedAt: 0,
463
471
  inflight: null
464
472
  };
465
- const age = Date.now() - entry.fetchedAt;
473
+ const now = Date.now();
474
+ const age = now - entry.fetchedAt;
466
475
  if (entry.ruleset && age < FRESH_MS) {
467
476
  return { ruleset: entry.ruleset, source: "memory" };
468
477
  }
478
+ if (inBackoff(entry, now)) {
479
+ return { ruleset: entry.ruleset, source: "backoff" };
480
+ }
469
481
  if (entry.ruleset && age < FRESH_MS + STALE_MS) {
470
482
  const p2 = refresh(key, url, entry, fetchImpl);
471
483
  waitUntil?.(p2);
@@ -477,14 +489,17 @@ async function getRulesetWithSource({
477
489
  let timer;
478
490
  const budget = new Promise((resolve) => {
479
491
  timer = setTimeout(
480
- () => resolve(null),
492
+ () => resolve(BUDGET_MISSED),
481
493
  lastGood ? RENDER_BUDGET_MS : COLD_RENDER_BUDGET_MS
482
494
  );
483
495
  });
484
496
  const result = await Promise.race([p, budget]);
485
497
  clearTimeout(timer);
486
- if (result && result !== lastGood)
498
+ if (result === BUDGET_MISSED) {
499
+ if (entry.inflight && entry.failedAt === 0) entry.failedAt = Date.now();
500
+ } else if (result && result !== lastGood) {
487
501
  return { ruleset: result, source: "fetched" };
502
+ }
488
503
  if (lastGood) return { ruleset: lastGood, source: "last-good" };
489
504
  return { ruleset: null, source: "cold-fallback" };
490
505
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heyplaybook/hydrogen",
3
- "version": "0.2.0",
3
+ "version": "0.2.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.",