@heyplaybook/hydrogen 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -0
- package/dist/index.d.ts +201 -27
- package/dist/index.js +295 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -206,6 +206,97 @@ The manifest is cached per isolate exactly like the ruleset: 60 s fresh, 300 s
|
|
|
206
206
|
stale, a 300 ms / 1.5 s render budget, a 30 s backoff after a failure. The kill
|
|
207
207
|
switch and a missing shop id turn it off with everything else.
|
|
208
208
|
|
|
209
|
+
### Server-side render: named slots and section slots (0.8.0)
|
|
210
|
+
|
|
211
|
+
The cloak hides the page until the browser has painted the test. With slots, the
|
|
212
|
+
store server renders the visitor's arm straight into the HTML instead: no hide, no
|
|
213
|
+
swap. Render a slot once per spot; any test a designer places at that slot is then
|
|
214
|
+
server-rendered, with no code change per test.
|
|
215
|
+
|
|
216
|
+
**Named slots** — one per spot, from a fixed vocabulary:
|
|
217
|
+
|
|
218
|
+
| Name | Page | Where |
|
|
219
|
+
|---|---|---|
|
|
220
|
+
| `buy-column-top` | product | first thing in the buy column, above the title / breadcrumbs |
|
|
221
|
+
| `after-add-to-cart` | product | right after the Add to Cart block, outside your button group |
|
|
222
|
+
| `under-size` | product | under the Size option's label, above its buttons |
|
|
223
|
+
| `collection-grid-top` | collection | right before the product grid |
|
|
224
|
+
| `after-home-hero` | home | right after the hero section |
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
import {PlaybookSlot} from '@heyplaybook/hydrogen';
|
|
228
|
+
|
|
229
|
+
const {playbookSsr} = useRootLoaderData(); // the root loader's `ssr`
|
|
230
|
+
|
|
231
|
+
<AddToCart … />
|
|
232
|
+
<PlaybookSlot ssr={playbookSsr} name="after-add-to-cart" />
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Section slots** — wrap your section renderer and every CMS section gets a slot
|
|
236
|
+
before and after it (`section:<id>:before` / `section:<id>:after`), so home and
|
|
237
|
+
landing pages need no names agreed in advance. With Pack, replace
|
|
238
|
+
`<RenderSections content={page} />` with:
|
|
239
|
+
|
|
240
|
+
```tsx
|
|
241
|
+
import {useSections} from '@pack/react';
|
|
242
|
+
import {PlaybookSections} from '@heyplaybook/hydrogen';
|
|
243
|
+
|
|
244
|
+
const sections = useSections({content: page}); // what RenderSections renders
|
|
245
|
+
<PlaybookSections ssr={playbookSsr} sections={sections} />
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
The section id is each element's React key (`useSections` keys by section id).
|
|
249
|
+
Pass `sectionId={(el, i) => …}` if your elements are keyed differently.
|
|
250
|
+
|
|
251
|
+
**Declare what you render** in `server.ts`, so the server only drops the cloak for
|
|
252
|
+
a test it will actually render:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
import {createPlaybookContext, SECTION_SLOTS} from '@heyplaybook/hydrogen';
|
|
256
|
+
|
|
257
|
+
createPlaybookContext({
|
|
258
|
+
// …
|
|
259
|
+
ssrSlots: [
|
|
260
|
+
'buy-column-top',
|
|
261
|
+
'after-add-to-cart',
|
|
262
|
+
'under-size',
|
|
263
|
+
'collection-grid-top',
|
|
264
|
+
'after-home-hero',
|
|
265
|
+
SECTION_SLOTS, // you render <PlaybookSections>
|
|
266
|
+
],
|
|
267
|
+
});
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Root head, once:
|
|
271
|
+
|
|
272
|
+
```tsx
|
|
273
|
+
<PlaybookSsrHead ssr={data.ssr} nonce={nonce} />
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
How it behaves:
|
|
277
|
+
|
|
278
|
+
- **Matching.** A test's anchor carries a slot key. The server matches it first,
|
|
279
|
+
then falls back to the 0.7 exact-selector match, so `{anchor, position}` slots
|
|
280
|
+
keep working unchanged.
|
|
281
|
+
- **The marker.** Every slot renders an inert `<template data-pb-slot="<key>">`
|
|
282
|
+
(display none, no box, no layout effect). The browser path anchors to it
|
|
283
|
+
(`[data-pb-slot="<key>"]`), so a test at a slot also renders correctly for a
|
|
284
|
+
visitor the server didn't decide for.
|
|
285
|
+
- **Placement.** The design renders right after the marker (right before it for a
|
|
286
|
+
`before` anchor), exactly where the browser path would put it.
|
|
287
|
+
- **Slot report.** After hydration `<PlaybookRoot>` reports which markers a page
|
|
288
|
+
renders, so Playbook learns your slot list and its designer offers those slots as
|
|
289
|
+
placements. Sampled (1 in 10 page loads), at most once a day per browser per page
|
|
290
|
+
type, no shopper data. `slotReport={false}` turns it off.
|
|
291
|
+
- **Fallback.** Anything the server can't render (a test at a plain selector, a
|
|
292
|
+
slot this page doesn't declare, more than one test on the page, a non-html
|
|
293
|
+
design) keeps the cloak, as before.
|
|
294
|
+
|
|
295
|
+
Render each named slot only where it belongs, and only once per page. A declared
|
|
296
|
+
slot that a page doesn't actually render (e.g. `under-size` on a product with no
|
|
297
|
+
Size option) costs that page the cloak: the browser then places the design late, at
|
|
298
|
+
the test's fallback spot. Wrap such a slot in the same condition as its spot.
|
|
299
|
+
|
|
209
300
|
### Cart attribute at creation
|
|
210
301
|
|
|
211
302
|
Writing `_pb_vid` after the fact can race other attribute writers (Intelligems writes when
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import * as react from 'react';
|
|
3
|
-
import { ReactNode, Component } from 'react';
|
|
3
|
+
import { ReactNode, ReactElement, Component } from 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Analytics consent — vendored from the Playbook platform's shared consent module (the
|
|
@@ -479,6 +479,45 @@ declare function playbookHeadSnippet(cloak: PlaybookCloakConfig | null | undefin
|
|
|
479
479
|
/** The same nodes as an HTML string, for a non-React head. '' when untargeted. */
|
|
480
480
|
declare function playbookHeadSnippetHtml(cloak: PlaybookCloakConfig | null | undefined, options?: PlaybookHeadSnippetOptions): string;
|
|
481
481
|
|
|
482
|
+
/**
|
|
483
|
+
* 0.8.0: the server-render slot vocabulary — a VENDORED copy of the
|
|
484
|
+
* platform's `shared/src/editor-protocol/ssr-slots.ts` (the package can't
|
|
485
|
+
* import the monorepo's internal packages). `__tests__/slots.test.ts` fails if the two drift, so
|
|
486
|
+
* a name added on one side must be added on the other in the same change.
|
|
487
|
+
*
|
|
488
|
+
* Two kinds of slot:
|
|
489
|
+
*
|
|
490
|
+
* - NAMED: `<PlaybookSlot name="after-add-to-cart" />`, one per spot, from
|
|
491
|
+
* the list below.
|
|
492
|
+
* - SECTION: `<PlaybookSections>` renders `section:<id>:before` and
|
|
493
|
+
* `section:<id>:after` around each CMS section (home, landing pages).
|
|
494
|
+
*
|
|
495
|
+
* Every slot renders an inert `<template data-pb-slot="<key>">` marker, so a
|
|
496
|
+
* slot is also a CSS anchor (`[data-pb-slot="<key>"]`) for the browser path,
|
|
497
|
+
* and the package can report which slots a page renders.
|
|
498
|
+
*/
|
|
499
|
+
declare const SSR_SLOT_ATTR = "data-pb-slot";
|
|
500
|
+
/** The named slots. Extend in step with the platform's list. */
|
|
501
|
+
declare const SSR_SLOT_NAMES: readonly ["buy-column-top", "after-add-to-cart", "under-size", "collection-grid-top", "after-home-hero"];
|
|
502
|
+
type PlaybookSlotName = (typeof SSR_SLOT_NAMES)[number];
|
|
503
|
+
type SectionSlotSide = 'before' | 'after';
|
|
504
|
+
type SectionSlotKey = `section:${string}:${SectionSlotSide}`;
|
|
505
|
+
type PlaybookSlotKey = PlaybookSlotName | SectionSlotKey;
|
|
506
|
+
/**
|
|
507
|
+
* Declare in `createPlaybookContext({ssrSlots})` that the store wraps its
|
|
508
|
+
* sections in `<PlaybookSections>`: a test anchored to any section slot is
|
|
509
|
+
* then server-rendered.
|
|
510
|
+
*/
|
|
511
|
+
declare const SECTION_SLOTS: "section:*";
|
|
512
|
+
/** A named slot or a well-formed section slot key. */
|
|
513
|
+
declare function isSlotKey(v: unknown): v is PlaybookSlotKey;
|
|
514
|
+
/** `section:<id>:before|after`, or null for an id that isn't selector-safe. */
|
|
515
|
+
declare function sectionSlotKey(id: unknown, side: SectionSlotSide): SectionSlotKey | null;
|
|
516
|
+
/** The CSS selector of a slot's marker. */
|
|
517
|
+
declare function slotSelector(key: PlaybookSlotKey): string;
|
|
518
|
+
/** The slot a selector names when it is exactly a slot marker selector, else null. */
|
|
519
|
+
declare function slotFromSelector(selector: unknown): PlaybookSlotKey | null;
|
|
520
|
+
|
|
482
521
|
interface SsrSnapshotNode {
|
|
483
522
|
id: string;
|
|
484
523
|
html: string;
|
|
@@ -495,10 +534,16 @@ interface SsrArm {
|
|
|
495
534
|
w: number;
|
|
496
535
|
c: 0 | 1;
|
|
497
536
|
x: string | null;
|
|
537
|
+
/**
|
|
538
|
+
* Anchor: selector + position (+ fallback). 0.8.0: `slot` = the named or
|
|
539
|
+
* section slot the arm mounts at (platform `ExperienceAnchor.slot`); the
|
|
540
|
+
* slot match comes first, the exact selector is the 0.7 fallback.
|
|
541
|
+
*/
|
|
498
542
|
a: {
|
|
499
543
|
s: string;
|
|
500
544
|
p: 'before' | 'after' | 'prepend' | 'append';
|
|
501
545
|
f?: string;
|
|
546
|
+
slot?: string;
|
|
502
547
|
} | null;
|
|
503
548
|
hide: string[];
|
|
504
549
|
}
|
|
@@ -520,6 +565,23 @@ interface SsrDoc {
|
|
|
520
565
|
interface SsrPayload {
|
|
521
566
|
doc: SsrDoc;
|
|
522
567
|
snapshots: Record<string, SsrSnapshot>;
|
|
568
|
+
/** 0.7.1: the arm a valid `?pb=` preview code names (only when one was sent). */
|
|
569
|
+
preview?: SsrPreview;
|
|
570
|
+
}
|
|
571
|
+
/** 0.7.1: a `?pb=` preview code's arm, as the platform resolved it (shop-scoped, unexpired). */
|
|
572
|
+
interface SsrPreview {
|
|
573
|
+
code: string;
|
|
574
|
+
experimentId: string;
|
|
575
|
+
armId: string;
|
|
576
|
+
experienceId: string | null;
|
|
577
|
+
testName: string;
|
|
578
|
+
armLabel: string;
|
|
579
|
+
pages: string[];
|
|
580
|
+
expiresAt: string;
|
|
581
|
+
arm: SsrArm;
|
|
582
|
+
snapshot: SsrSnapshot | null;
|
|
583
|
+
/** Non-null = the platform can't server-render this arm (the browser preview covers it). */
|
|
584
|
+
why: string | null;
|
|
523
585
|
}
|
|
524
586
|
declare function ssrUrl(platformUrl: string, shopDomain: string): string;
|
|
525
587
|
/** The shop's SSR doc, or null when the read isn't healthy (then: cloak only). Never throws. */
|
|
@@ -539,14 +601,27 @@ interface SsrDecision {
|
|
|
539
601
|
experimentId: string;
|
|
540
602
|
armId: string;
|
|
541
603
|
experienceId: string | null;
|
|
542
|
-
/**
|
|
543
|
-
idSource: 'prior' | 'settled' | 'roll';
|
|
604
|
+
/** How the arm was chosen. `preview` = a `?pb=` code named it (0.7.1). */
|
|
605
|
+
idSource: 'prior' | 'settled' | 'roll' | 'preview';
|
|
544
606
|
arm: SsrArm;
|
|
545
607
|
snapshot: SsrSnapshot | null;
|
|
546
608
|
page: {
|
|
547
609
|
t: string;
|
|
548
610
|
h: string;
|
|
549
611
|
};
|
|
612
|
+
/** 0.7.1: set for a preview-code render — nothing is assigned or recorded. */
|
|
613
|
+
preview?: {
|
|
614
|
+
code: string;
|
|
615
|
+
testName: string;
|
|
616
|
+
armLabel: string;
|
|
617
|
+
};
|
|
618
|
+
/**
|
|
619
|
+
* 0.8.0: which kind of declared slot serves the arm (`loaderData()` sets it):
|
|
620
|
+
* `slot` = a named / section slot (by slot key), `anchor` = a 0.7 selector
|
|
621
|
+
* slot. Only that kind of `<PlaybookSlot>` renders the design, so a store
|
|
622
|
+
* that has both for one spot never renders it twice.
|
|
623
|
+
*/
|
|
624
|
+
matchedBy?: 'slot' | 'anchor';
|
|
550
625
|
}
|
|
551
626
|
type SsrOutcome = {
|
|
552
627
|
decision: SsrDecision;
|
|
@@ -581,11 +656,32 @@ declare function ssrHostInnerHtml(snapshot: SsrSnapshot, page: {
|
|
|
581
656
|
t: string;
|
|
582
657
|
h: string;
|
|
583
658
|
}): string;
|
|
584
|
-
/**
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
659
|
+
/**
|
|
660
|
+
* 0.8.0: the slot an arm's anchor serves — its `slot`, else the slot its
|
|
661
|
+
* selector names when the selector is exactly a marker
|
|
662
|
+
* (`[data-pb-slot="…"]`). Null for a plain CSS anchor.
|
|
663
|
+
*/
|
|
664
|
+
declare function armSlot(a: SsrArm['a'] | null | undefined): PlaybookSlotKey | null;
|
|
665
|
+
/**
|
|
666
|
+
* A slot the store renders, as `<PlaybookSlot>` props or a
|
|
667
|
+
* `createPlaybookContext({ssrSlots})` entry:
|
|
668
|
+
*
|
|
669
|
+
* - `{name}` — a named slot or a section slot key; `{name: SECTION_SLOTS}`
|
|
670
|
+
* = any section slot (`<PlaybookSections>`);
|
|
671
|
+
* - `{anchor, position}` — the 0.7 exact-selector slot.
|
|
672
|
+
*/
|
|
673
|
+
interface SsrSlotRef {
|
|
674
|
+
name?: string;
|
|
675
|
+
anchor?: string;
|
|
676
|
+
position?: 'before' | 'after' | 'prepend' | 'append';
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Does this slot serve the decision's arm? By SLOT KEY first (a named or
|
|
680
|
+
* section slot: the arm's `slot`, or the slot its selector names), then the
|
|
681
|
+
* 0.7 exact selector + position match, so a store's selector-keyed slots
|
|
682
|
+
* keep working unchanged.
|
|
683
|
+
*/
|
|
684
|
+
declare function slotMatches(d: SsrDecision, slot: SsrSlotRef): boolean;
|
|
589
685
|
|
|
590
686
|
/** The env keys the kit reads (all optional). */
|
|
591
687
|
interface PlaybookEnv {
|
|
@@ -628,17 +724,19 @@ interface CreatePlaybookContextOptions {
|
|
|
628
724
|
*/
|
|
629
725
|
cloak?: boolean;
|
|
630
726
|
/**
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
* the
|
|
636
|
-
*
|
|
727
|
+
* The slots this store renders. On a page an all-traffic test targets,
|
|
728
|
+
* `loaderData()` picks the visitor's arm on the server and returns it as
|
|
729
|
+
* `ssr` when one of these slots serves the arm (or the arm is the original
|
|
730
|
+
* page); the cloak then stays off for that page. Empty/absent = no server
|
|
731
|
+
* render, the 0.6.0 cloak only.
|
|
732
|
+
*
|
|
733
|
+
* - 0.8.0 a slot NAME (`'after-add-to-cart'`, the `<PlaybookSlot name>`s
|
|
734
|
+
* the store renders) or `SECTION_SLOTS` (the store wraps its sections
|
|
735
|
+
* in `<PlaybookSections>`). Matched by the arm's slot key.
|
|
736
|
+
* - 0.7.0 `{anchor, position}`, exactly as the `<PlaybookSlot>` props.
|
|
737
|
+
* Matched by exact selector, after the slot key.
|
|
637
738
|
*/
|
|
638
|
-
ssrSlots?: ReadonlyArray<
|
|
639
|
-
anchor: string;
|
|
640
|
-
position?: 'before' | 'after' | 'prepend' | 'append';
|
|
641
|
-
}>;
|
|
739
|
+
ssrSlots?: ReadonlyArray<string | SsrSlotRef>;
|
|
642
740
|
/**
|
|
643
741
|
* 0.6.0: the storefront DOMAIN the targeting manifest is keyed by (its
|
|
644
742
|
* `?shop=`; the Playbook UUID doesn't work there). Overrides
|
|
@@ -799,18 +897,84 @@ declare function PlaybookSsrHead({ ssr, nonce }: {
|
|
|
799
897
|
ssr: SsrDecision | null | undefined;
|
|
800
898
|
nonce?: string;
|
|
801
899
|
}): react_jsx_runtime.JSX.Element | null;
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
900
|
+
type PlaybookSlotProps = {
|
|
901
|
+
ssr: SsrDecision | null | undefined;
|
|
902
|
+
/**
|
|
903
|
+
* 0.8.0: the slot's name (`after-add-to-cart`, …) or a section slot key
|
|
904
|
+
* (`section:<id>:after`; `<PlaybookSections>` renders those for you).
|
|
905
|
+
*/
|
|
906
|
+
name: PlaybookSlotKey;
|
|
907
|
+
anchor?: never;
|
|
908
|
+
position?: never;
|
|
909
|
+
} | {
|
|
808
910
|
ssr: SsrDecision | null | undefined;
|
|
809
|
-
|
|
911
|
+
name?: never;
|
|
912
|
+
/** 0.7: the CSS selector the test anchors to, exactly as Playbook stores it. */
|
|
810
913
|
anchor: string;
|
|
811
914
|
position?: 'before' | 'after' | 'prepend' | 'append';
|
|
915
|
+
};
|
|
916
|
+
/**
|
|
917
|
+
* The server-rendered arm, placed by the store where the test mounts.
|
|
918
|
+
*
|
|
919
|
+
* A NAMED slot always renders an inert marker, `<template data-pb-slot>`
|
|
920
|
+
* (no box, nothing in layout), so the page says which slots it has: the
|
|
921
|
+
* browser path anchors to it (`[data-pb-slot="<name>"]`) and the package
|
|
922
|
+
* reports it to Playbook. When the decision's arm mounts at this slot the
|
|
923
|
+
* design renders beside the marker (before it for a `before` anchor, else
|
|
924
|
+
* after), exactly where the browser path would put it.
|
|
925
|
+
*
|
|
926
|
+
* A 0.7 SELECTOR slot renders only when the arm targets exactly this anchor +
|
|
927
|
+
* position; otherwise nothing (and the SDK renders as usual).
|
|
928
|
+
*
|
|
929
|
+
* Slot key first: when `loaderData()` matched the arm to a declared named /
|
|
930
|
+
* section slot (`ssr.matchedBy === 'slot'`), only that slot renders it, so a
|
|
931
|
+
* store keeping an old selector slot at the same spot never renders it twice.
|
|
932
|
+
*/
|
|
933
|
+
declare function PlaybookSlot(props: PlaybookSlotProps): react_jsx_runtime.JSX.Element | null;
|
|
934
|
+
/**
|
|
935
|
+
* 0.8.0 SECTION slots: renders the store's sections with a slot before and
|
|
936
|
+
* after each one (`section:<id>:before|after`), so a test can mount between
|
|
937
|
+
* any two sections of a home or landing page with no name agreed in advance.
|
|
938
|
+
*
|
|
939
|
+
* // Pack: same output as <RenderSections content={page} />, plus the slots.
|
|
940
|
+
* const sections = useSections({content: page});
|
|
941
|
+
* <PlaybookSections ssr={playbookSsr} sections={sections} />
|
|
942
|
+
*
|
|
943
|
+
* The id is each element's React key (`useSections` keys by section id), or
|
|
944
|
+
* `sectionId(element, index)` when the store keys them differently. A section
|
|
945
|
+
* without a usable id renders as is, with no slots. Declare `SECTION_SLOTS`
|
|
946
|
+
* in `createPlaybookContext({ssrSlots})`.
|
|
947
|
+
*/
|
|
948
|
+
declare function PlaybookSections({ ssr, sections, sectionId, }: {
|
|
949
|
+
ssr: SsrDecision | null | undefined;
|
|
950
|
+
sections: ReadonlyArray<ReactNode> | null | undefined;
|
|
951
|
+
sectionId?: (element: ReactElement, index: number) => string | null | undefined;
|
|
812
952
|
}): react_jsx_runtime.JSX.Element | null;
|
|
813
953
|
|
|
954
|
+
declare const SLOT_REPORT_URL = "/apps/playbook/api/storefront/ssr/slots";
|
|
955
|
+
declare const SLOT_REPORT_SAMPLE_RATE = 0.1;
|
|
956
|
+
/** The slot keys the markup under `root` renders, in page order, de-duplicated and capped. */
|
|
957
|
+
declare function renderedSlotKeys(root: ParentNode): string[];
|
|
958
|
+
interface StorageLike {
|
|
959
|
+
getItem(key: string): string | null;
|
|
960
|
+
setItem(key: string, value: string): void;
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Read this page's markers and report them when the dedupe + sample allow.
|
|
964
|
+
* Returns whether it sent. Browser only; never throws.
|
|
965
|
+
*/
|
|
966
|
+
declare function reportPageSlots(input: {
|
|
967
|
+
shopId: string | null | undefined;
|
|
968
|
+
pathname: string;
|
|
969
|
+
url?: string;
|
|
970
|
+
sampleRate?: number;
|
|
971
|
+
fetchImpl?: typeof fetch;
|
|
972
|
+
root?: ParentNode;
|
|
973
|
+
storage?: StorageLike | null;
|
|
974
|
+
random?: () => number;
|
|
975
|
+
now?: () => number;
|
|
976
|
+
}): boolean;
|
|
977
|
+
|
|
814
978
|
/**
|
|
815
979
|
* Store-specific "why not in test" text for the QA panel. Return a string to
|
|
816
980
|
* show it (it wins over the generic reason), or null to fall back to the
|
|
@@ -967,6 +1131,16 @@ interface PlaybookRootProps {
|
|
|
967
1131
|
previewIgnoresTargeting?: boolean;
|
|
968
1132
|
/** Observe cart-sync decisions (debug/logging). */
|
|
969
1133
|
onCartSyncEvent?: (event: CartSyncEvent) => void;
|
|
1134
|
+
/**
|
|
1135
|
+
* 0.8.0: report which server-render slots each page renders (its
|
|
1136
|
+
* `<template data-pb-slot>` markers) so Playbook learns the store's slot
|
|
1137
|
+
* list. Sampled, deduped per browser per day, no shopper data (see
|
|
1138
|
+
* `./slot-report`). Default on; `false` turns it off.
|
|
1139
|
+
*/
|
|
1140
|
+
slotReport?: boolean | {
|
|
1141
|
+
sampleRate?: number;
|
|
1142
|
+
url?: string;
|
|
1143
|
+
};
|
|
970
1144
|
exposureUrl?: string;
|
|
971
1145
|
eventUrl?: string;
|
|
972
1146
|
supportedEvents?: string[];
|
|
@@ -982,7 +1156,7 @@ declare function visitorInRunningTest(state: PlaybookState | null | undefined):
|
|
|
982
1156
|
declare function PlaybookAAExposure({ testKey }: {
|
|
983
1157
|
testKey?: string;
|
|
984
1158
|
}): react_jsx_runtime.JSX.Element;
|
|
985
|
-
declare function PlaybookRoot({ state, location, navigate, cart, consent, storeContext, editorPreview, aaSentinel, debug, dataLayer, previewIgnoresTargeting, onCartSyncEvent, exposureUrl, eventUrl, supportedEvents, children, }: PlaybookRootProps): react_jsx_runtime.JSX.Element;
|
|
1159
|
+
declare function PlaybookRoot({ state, location, navigate, cart, consent, storeContext, editorPreview, aaSentinel, debug, dataLayer, previewIgnoresTargeting, onCartSyncEvent, exposureUrl, eventUrl, supportedEvents, slotReport, children, }: PlaybookRootProps): react_jsx_runtime.JSX.Element;
|
|
986
1160
|
|
|
987
1161
|
interface Props {
|
|
988
1162
|
/** For the console warning only. */
|
|
@@ -1351,4 +1525,4 @@ declare function inNamespace(hashValue: string, namespace: [string, number, numb
|
|
|
1351
1525
|
declare function chooseVariation(n: number, ranges: VariationRange[]): number;
|
|
1352
1526
|
declare function getBucketRanges(numVariations: number, coverage: number | undefined, weights?: number[]): VariationRange[];
|
|
1353
1527
|
|
|
1354
|
-
export { AA_SITEWIDE_TEST, type AddToCartSource, type AddedLineInput, CART_SYNC_IDLE_MS, CART_VISITOR_ATTRIBUTE, CLOAK_MAX_MS, CLOAK_STYLE_ID, CONSENT_LOADED_EVENTS, CONTROL_ARM, type CartAttribute, type CartAttributeReader, type CartAttributeWriter, type CartLineSnapshot, type CartSyncEvent, type CartSyncSnapshot, type CartWriteResult, type ConsentState, type CreatePlaybookContextOptions, type CreatePlaybookOptions, DEFAULT_DATALAYER_EVENT, DEFAULT_EVENT_URL, DEFAULT_EXPERIENCE_SDK_URL, DEFAULT_EXPOSURE_URL, DEFAULT_PLATFORM_URL, DEFAULT_PROXY_ENDPOINT, DEFAULT_SDK_URL, DEFAULT_SUPPORTED_EVENTS, EXPERIENCE_SDK_SCRIPT_ID, EXPERIENCE_VISITOR_CART_ATTRIBUTE, EXPOSURE_CART_ATTRIBUTE, EXPOSURE_COOKIE, type ExperienceAttributionConsent, type ExperienceAttributionInput, type HeadSnippetParts, LEGACY_VISITOR_COOKIE, MAX_WRITES_PER_CART, PlaybookAAExposure, type PlaybookArm, type PlaybookAssignReason, type PlaybookAssignment, PlaybookBoundary, type PlaybookCartAdapter, type PlaybookClientConsent, type PlaybookCloakConfig, PlaybookCloakHead, type PlaybookConsentAdapter, type PlaybookDataLayerOption, type PlaybookDebugExplain, type PlaybookDebugOptions, PlaybookDebugPanel, type PlaybookDevice, type PlaybookEnv, PlaybookExposure, PlaybookHead, type PlaybookHeadProps, type PlaybookHeadSnippetOptions, type PlaybookLoaderData, type PlaybookPreview, PlaybookProvider, type PlaybookProxyOptions, type PlaybookRequestContext, PlaybookRoot, type PlaybookRootProps, type PlaybookRuleset, type PlaybookServerConsent, type PlaybookServerResult, PlaybookSlot, PlaybookSsrHead, type PlaybookState, type PlaybookStoreContext, type PlaybookTestRule, type PlaybookTestStatus, type PlaybookVariant, type PlaybookVisitorType, type RulesetSource, SESSION_VISITOR_COOKIE, STANDARD_EVENTS, type SsrDecision, type SsrOutcome, type SsrPayload, type StandardEvent, TRUEVAULT_CONSENT_COOKIE, type TrackScope, type TrackedEntry, type TrueVaultConsentOptions, VISITOR_COOKIE, addedLinesFrom, analyticsConsentFromCookie, assignTest, bucketIndex, buildHeadSnippet, cartHasPlaybookAttribute, chooseVariation, consentFromAnalytics, createCartAttributeReader, createCartAttributeSync, createCartAttributeWriter, createPlaybook, createPlaybookContext, createPlaybookProxyHandler, createTrueVaultConsent, debugPanelAllowed, decideSsr, declareConsentRequired, detectDevice, detectGeo, experienceVisitorIdFromCookies, getBucketRanges, getConsentState, getEqualWeights, getServerTrackedLog, getSessionContext, getSsrPayload, getTargetedPages, getTrackedLog, hasPlaybookParams, hasSentExposure, hash, inNamespace, isAnalyticsConsentGranted, isKillSwitchOn, isPreviewHost, manifestUrl, matchTargetedPage, matchesPathPrefix, mergeAttributes, normalizeExposureList, onConsentChange, onConsentLoaded, playbookBackendUrl, playbookCartAttribute, playbookHeadSnippet, playbookHeadSnippetHtml, playbookTrack, prefetchRuleset, reconcileExperienceCartAttributes, reconcilePlaybookCartAttribute, revealScript, sanitizeManifestPages, sanitizeRuleset, setNextAddSource, shopifyCustomerPrivacyConsent, slotMatches, ssrHostInnerHtml, ssrUrl, subscribeTrackedLog, syncVisitorId, takeAddSource, targetPageKey, trackAddToCart, trackAddToCartLines, trueVaultDecision, truevaultConsent, useAnalyticsAllowed, usePageEngagement, usePlaybookConsent, usePlaybookExposure, usePlaybookInteraction, usePlaybookState, usePlaybookVariant, usePreviewParamRedirect, visitorInRunningTest, visitorSeedScript, waitForConsentAPI, withPlaybook, withPlaybookCartAttribute };
|
|
1528
|
+
export { AA_SITEWIDE_TEST, type AddToCartSource, type AddedLineInput, CART_SYNC_IDLE_MS, CART_VISITOR_ATTRIBUTE, CLOAK_MAX_MS, CLOAK_STYLE_ID, CONSENT_LOADED_EVENTS, CONTROL_ARM, type CartAttribute, type CartAttributeReader, type CartAttributeWriter, type CartLineSnapshot, type CartSyncEvent, type CartSyncSnapshot, type CartWriteResult, type ConsentState, type CreatePlaybookContextOptions, type CreatePlaybookOptions, DEFAULT_DATALAYER_EVENT, DEFAULT_EVENT_URL, DEFAULT_EXPERIENCE_SDK_URL, DEFAULT_EXPOSURE_URL, DEFAULT_PLATFORM_URL, DEFAULT_PROXY_ENDPOINT, DEFAULT_SDK_URL, DEFAULT_SUPPORTED_EVENTS, EXPERIENCE_SDK_SCRIPT_ID, EXPERIENCE_VISITOR_CART_ATTRIBUTE, EXPOSURE_CART_ATTRIBUTE, EXPOSURE_COOKIE, type ExperienceAttributionConsent, type ExperienceAttributionInput, type HeadSnippetParts, LEGACY_VISITOR_COOKIE, MAX_WRITES_PER_CART, PlaybookAAExposure, type PlaybookArm, type PlaybookAssignReason, type PlaybookAssignment, PlaybookBoundary, type PlaybookCartAdapter, type PlaybookClientConsent, type PlaybookCloakConfig, PlaybookCloakHead, type PlaybookConsentAdapter, type PlaybookDataLayerOption, type PlaybookDebugExplain, type PlaybookDebugOptions, PlaybookDebugPanel, type PlaybookDevice, type PlaybookEnv, PlaybookExposure, PlaybookHead, type PlaybookHeadProps, type PlaybookHeadSnippetOptions, type PlaybookLoaderData, type PlaybookPreview, PlaybookProvider, type PlaybookProxyOptions, type PlaybookRequestContext, PlaybookRoot, type PlaybookRootProps, type PlaybookRuleset, PlaybookSections, type PlaybookServerConsent, type PlaybookServerResult, PlaybookSlot, type PlaybookSlotKey, type PlaybookSlotName, type PlaybookSlotProps, PlaybookSsrHead, type PlaybookState, type PlaybookStoreContext, type PlaybookTestRule, type PlaybookTestStatus, type PlaybookVariant, type PlaybookVisitorType, type RulesetSource, SECTION_SLOTS, SESSION_VISITOR_COOKIE, SLOT_REPORT_SAMPLE_RATE, SLOT_REPORT_URL, SSR_SLOT_ATTR, SSR_SLOT_NAMES, STANDARD_EVENTS, type SectionSlotKey, type SsrDecision, type SsrOutcome, type SsrPayload, type SsrSlotRef, type StandardEvent, TRUEVAULT_CONSENT_COOKIE, type TrackScope, type TrackedEntry, type TrueVaultConsentOptions, VISITOR_COOKIE, addedLinesFrom, analyticsConsentFromCookie, armSlot, assignTest, bucketIndex, buildHeadSnippet, cartHasPlaybookAttribute, chooseVariation, consentFromAnalytics, createCartAttributeReader, createCartAttributeSync, createCartAttributeWriter, createPlaybook, createPlaybookContext, createPlaybookProxyHandler, createTrueVaultConsent, debugPanelAllowed, decideSsr, declareConsentRequired, detectDevice, detectGeo, experienceVisitorIdFromCookies, getBucketRanges, getConsentState, getEqualWeights, getServerTrackedLog, getSessionContext, getSsrPayload, getTargetedPages, getTrackedLog, hasPlaybookParams, hasSentExposure, hash, inNamespace, isAnalyticsConsentGranted, isKillSwitchOn, isPreviewHost, isSlotKey, manifestUrl, matchTargetedPage, matchesPathPrefix, mergeAttributes, normalizeExposureList, onConsentChange, onConsentLoaded, playbookBackendUrl, playbookCartAttribute, playbookHeadSnippet, playbookHeadSnippetHtml, playbookTrack, prefetchRuleset, reconcileExperienceCartAttributes, reconcilePlaybookCartAttribute, renderedSlotKeys, reportPageSlots, revealScript, sanitizeManifestPages, sanitizeRuleset, sectionSlotKey, setNextAddSource, shopifyCustomerPrivacyConsent, slotFromSelector, slotMatches, slotSelector, ssrHostInnerHtml, ssrUrl, subscribeTrackedLog, syncVisitorId, takeAddSource, targetPageKey, trackAddToCart, trackAddToCartLines, trueVaultDecision, truevaultConsent, useAnalyticsAllowed, usePageEngagement, usePlaybookConsent, usePlaybookExposure, usePlaybookInteraction, usePlaybookState, usePlaybookVariant, usePreviewParamRedirect, visitorInRunningTest, visitorSeedScript, waitForConsentAPI, withPlaybook, withPlaybookCartAttribute };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
2
|
-
import { memo, createContext, Component, useContext, useMemo, useCallback, useState, useEffect, useSyncExternalStore, useRef } from 'react';
|
|
2
|
+
import { memo, createContext, isValidElement, Fragment as Fragment$1, Component, useContext, useMemo, useCallback, useState, useEffect, useSyncExternalStore, useRef } from 'react';
|
|
3
3
|
|
|
4
4
|
// src/growthbook.ts
|
|
5
5
|
function hashFnv32a(str2) {
|
|
@@ -1072,6 +1072,44 @@ function hasOwnedPreviewParams(search) {
|
|
|
1072
1072
|
return OWNED_PARAM_RE.test(search || "");
|
|
1073
1073
|
}
|
|
1074
1074
|
|
|
1075
|
+
// src/slots.ts
|
|
1076
|
+
var SSR_SLOT_ATTR = "data-pb-slot";
|
|
1077
|
+
var SSR_SLOT_NAMES = [
|
|
1078
|
+
"buy-column-top",
|
|
1079
|
+
"after-add-to-cart",
|
|
1080
|
+
"under-size",
|
|
1081
|
+
"collection-grid-top",
|
|
1082
|
+
"after-home-hero"
|
|
1083
|
+
];
|
|
1084
|
+
var SECTION_ID_RE = /^[A-Za-z0-9_-]{1,64}$/;
|
|
1085
|
+
var SECTION_KEY_RE = /^section:([A-Za-z0-9_-]{1,64}):(before|after)$/;
|
|
1086
|
+
var SLOT_KEY_MAX = 80;
|
|
1087
|
+
var SECTION_SLOTS = "section:*";
|
|
1088
|
+
function isSlotName(v) {
|
|
1089
|
+
return typeof v === "string" && SSR_SLOT_NAMES.includes(v);
|
|
1090
|
+
}
|
|
1091
|
+
function isSlotKey(v) {
|
|
1092
|
+
if (typeof v !== "string" || !v || v.length > SLOT_KEY_MAX) return false;
|
|
1093
|
+
return isSlotName(v) || SECTION_KEY_RE.test(v);
|
|
1094
|
+
}
|
|
1095
|
+
function isSectionSlotKey(v) {
|
|
1096
|
+
return typeof v === "string" && SECTION_KEY_RE.test(v);
|
|
1097
|
+
}
|
|
1098
|
+
function sectionSlotKey(id, side) {
|
|
1099
|
+
const v = typeof id === "string" || typeof id === "number" ? String(id).trim() : "";
|
|
1100
|
+
return SECTION_ID_RE.test(v) ? `section:${v}:${side}` : null;
|
|
1101
|
+
}
|
|
1102
|
+
function slotSelector(key) {
|
|
1103
|
+
return `[${SSR_SLOT_ATTR}="${key}"]`;
|
|
1104
|
+
}
|
|
1105
|
+
function slotFromSelector(selector) {
|
|
1106
|
+
if (typeof selector !== "string") return null;
|
|
1107
|
+
const m = /^\s*(?:template)?\[data-pb-slot=(?:"([^"]*)"|'([^']*)')\]\s*$/.exec(selector);
|
|
1108
|
+
if (!m) return null;
|
|
1109
|
+
const key = m[1] ?? m[2];
|
|
1110
|
+
return isSlotKey(key) ? key : null;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1075
1113
|
// src/ssr.ts
|
|
1076
1114
|
var SSR_SNAPSHOT_VERSION = 1;
|
|
1077
1115
|
function ssrUrl(platformUrl, shopDomain) {
|
|
@@ -1086,7 +1124,9 @@ function sanitizeSsrPayload(json) {
|
|
|
1086
1124
|
if (!doc || typeof doc !== "object") return null;
|
|
1087
1125
|
if (!doc.keys || typeof doc.keys !== "object" || !Array.isArray(doc.prefixes) || !Array.isArray(doc.exps)) return null;
|
|
1088
1126
|
const snapshots = j.snapshots && typeof j.snapshots === "object" ? j.snapshots : {};
|
|
1089
|
-
|
|
1127
|
+
const p = j.preview;
|
|
1128
|
+
const preview = p && typeof p === "object" && typeof p.experimentId === "string" && typeof p.armId === "string" && Array.isArray(p.pages) && p.arm ? p : void 0;
|
|
1129
|
+
return { doc, snapshots, ...preview ? { preview } : {} };
|
|
1090
1130
|
}
|
|
1091
1131
|
var ssrFetcher = createCachedJsonFetcher({ label: "ssr doc", sanitize: sanitizeSsrPayload });
|
|
1092
1132
|
var HEALTHY2 = /* @__PURE__ */ new Set(["memory", "stale", "fetched"]);
|
|
@@ -1102,6 +1142,54 @@ async function getSsrPayload(args) {
|
|
|
1102
1142
|
return null;
|
|
1103
1143
|
}
|
|
1104
1144
|
}
|
|
1145
|
+
var PREVIEW_CODE_RE = /^[0-9A-HJKMNP-TV-Z]{4,8}$/;
|
|
1146
|
+
function previewCodeFromRequestUrl(url) {
|
|
1147
|
+
const raw = url.searchParams.get("pb");
|
|
1148
|
+
if (!raw) return null;
|
|
1149
|
+
const code = raw.trim().toUpperCase();
|
|
1150
|
+
return PREVIEW_CODE_RE.test(code) ? code : null;
|
|
1151
|
+
}
|
|
1152
|
+
var PREVIEW_FETCH_BUDGET_MS = 1500;
|
|
1153
|
+
async function getSsrPreviewPayload(args) {
|
|
1154
|
+
const controller = new AbortController();
|
|
1155
|
+
const timer = setTimeout(() => controller.abort(), PREVIEW_FETCH_BUDGET_MS);
|
|
1156
|
+
try {
|
|
1157
|
+
const url = new URL(ssrUrl(args.platformUrl, args.shopDomain));
|
|
1158
|
+
url.searchParams.set("pb", args.code);
|
|
1159
|
+
const res = await (args.fetchImpl ?? fetch)(url.toString(), {
|
|
1160
|
+
headers: { accept: "application/json" },
|
|
1161
|
+
signal: controller.signal
|
|
1162
|
+
});
|
|
1163
|
+
if (!res.ok) return null;
|
|
1164
|
+
return sanitizeSsrPayload(await res.json());
|
|
1165
|
+
} catch {
|
|
1166
|
+
return null;
|
|
1167
|
+
} finally {
|
|
1168
|
+
clearTimeout(timer);
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
function decidePreviewSsr(payload, pageKey, matchesPage) {
|
|
1172
|
+
const p = payload.preview;
|
|
1173
|
+
if (!p) return { skip: "preview-unresolved" };
|
|
1174
|
+
if (p.why) return { skip: `preview-${p.why}` };
|
|
1175
|
+
if (!pageKey || !matchesPage(p.pages)) return { skip: "preview-not-this-page" };
|
|
1176
|
+
if (p.experienceId && (!p.snapshot || p.snapshot.v !== SSR_SNAPSHOT_VERSION || !p.snapshot.nodes?.length)) {
|
|
1177
|
+
return { skip: "preview-no-snapshot" };
|
|
1178
|
+
}
|
|
1179
|
+
const [t, h = ""] = pageKey.split(":");
|
|
1180
|
+
return {
|
|
1181
|
+
decision: {
|
|
1182
|
+
experimentId: p.experimentId,
|
|
1183
|
+
armId: p.armId,
|
|
1184
|
+
experienceId: p.experienceId,
|
|
1185
|
+
idSource: "preview",
|
|
1186
|
+
arm: p.arm,
|
|
1187
|
+
snapshot: p.snapshot,
|
|
1188
|
+
page: { t, h },
|
|
1189
|
+
preview: { code: p.code, testName: p.testName, armLabel: p.armLabel }
|
|
1190
|
+
}
|
|
1191
|
+
};
|
|
1192
|
+
}
|
|
1105
1193
|
function fnv32a(s2) {
|
|
1106
1194
|
let h = 2166136261;
|
|
1107
1195
|
for (let i = 0; i < s2.length; i++) {
|
|
@@ -1211,15 +1299,35 @@ function ssrStateScript(d) {
|
|
|
1211
1299
|
reason: d.experienceId ? null : "control",
|
|
1212
1300
|
idSource: d.idSource,
|
|
1213
1301
|
placedAt: null,
|
|
1214
|
-
server: true
|
|
1302
|
+
server: true,
|
|
1303
|
+
// A preview render: the SDK keeps it without resolving or recording.
|
|
1304
|
+
...d.preview ? { preview: d.preview } : {}
|
|
1215
1305
|
};
|
|
1216
1306
|
return `window.__pbSsr=${JSON.stringify(state).replace(/</g, "\\u003c")};`;
|
|
1217
1307
|
}
|
|
1308
|
+
function armSlot(a) {
|
|
1309
|
+
if (!a) return null;
|
|
1310
|
+
return isSlotKey(a.slot) ? a.slot : slotFromSelector(a.s);
|
|
1311
|
+
}
|
|
1218
1312
|
function slotMatches(d, slot) {
|
|
1219
1313
|
const a = d.arm.a;
|
|
1220
1314
|
if (!a) return false;
|
|
1315
|
+
if (typeof slot.name === "string" && slot.name) {
|
|
1316
|
+
const key = armSlot(a);
|
|
1317
|
+
if (!key) return false;
|
|
1318
|
+
return slot.name === SECTION_SLOTS ? isSectionSlotKey(key) : key === slot.name;
|
|
1319
|
+
}
|
|
1320
|
+
if (typeof slot.anchor !== "string") return false;
|
|
1221
1321
|
return a.s.trim() === slot.anchor.trim() && a.p === (slot.position ?? "after");
|
|
1222
1322
|
}
|
|
1323
|
+
function toSlotRef(decl) {
|
|
1324
|
+
return typeof decl === "string" ? { name: decl } : decl;
|
|
1325
|
+
}
|
|
1326
|
+
function declaredSlotFor(d, refs) {
|
|
1327
|
+
if (refs.some((r) => typeof r.name === "string" && r.name && slotMatches(d, r))) return "slot";
|
|
1328
|
+
if (refs.some((r) => !r.name && slotMatches(d, r))) return "anchor";
|
|
1329
|
+
return null;
|
|
1330
|
+
}
|
|
1223
1331
|
|
|
1224
1332
|
// src/request-context.ts
|
|
1225
1333
|
var FINALIZE_WAIT_MS = 2e3;
|
|
@@ -1344,11 +1452,32 @@ function createPlaybookContext({
|
|
|
1344
1452
|
return cloakPromise;
|
|
1345
1453
|
};
|
|
1346
1454
|
let ssrRendered = false;
|
|
1455
|
+
const slotRefs = ssrSlots.map(toSlotRef);
|
|
1347
1456
|
let ssrPromise = null;
|
|
1348
1457
|
const ssrFor = (cloak) => {
|
|
1349
1458
|
if (!ssrPromise) {
|
|
1350
1459
|
ssrPromise = (async () => {
|
|
1351
|
-
if (
|
|
1460
|
+
if (ssrSlots.length === 0) return { ssr: null, skip: null };
|
|
1461
|
+
const url = new URL(request.url);
|
|
1462
|
+
const code = request.method === "GET" && !url.pathname.endsWith(".data") ? previewCodeFromRequestUrl(url) : null;
|
|
1463
|
+
if (code && shopId) {
|
|
1464
|
+
const shopDomain = normalizeDomain(shopDomainOption ?? str(e.PUBLIC_PLAYBOOK_SHOP_DOMAIN) ?? url.hostname);
|
|
1465
|
+
if (!shopDomain) return { ssr: null, skip: "preview-no-shop" };
|
|
1466
|
+
const payload2 = await getSsrPreviewPayload({ platformUrl, shopDomain, code, fetchImpl });
|
|
1467
|
+
if (!payload2) return { ssr: null, skip: "preview-unavailable" };
|
|
1468
|
+
const pageKey = targetPageKey(url.pathname);
|
|
1469
|
+
const out2 = decidePreviewSsr(payload2, pageKey, (pages) => matchTargetedPage(pages, url.pathname) !== null);
|
|
1470
|
+
if ("skip" in out2) return { ssr: null, skip: out2.skip };
|
|
1471
|
+
let d2 = out2.decision;
|
|
1472
|
+
if (d2.experienceId) {
|
|
1473
|
+
const via = declaredSlotFor(d2, slotRefs);
|
|
1474
|
+
if (!via) return { ssr: null, skip: "preview-no-slot" };
|
|
1475
|
+
d2 = { ...d2, matchedBy: via };
|
|
1476
|
+
}
|
|
1477
|
+
ssrRendered = true;
|
|
1478
|
+
return { ssr: d2, skip: null };
|
|
1479
|
+
}
|
|
1480
|
+
if (!cloak) return { ssr: null, skip: null };
|
|
1352
1481
|
const payload = await getSsrPayload({ platformUrl, shopDomain: cloak.shopDomain, waitUntil, fetchImpl });
|
|
1353
1482
|
if (!payload) return { ssr: null, skip: "doc-unavailable" };
|
|
1354
1483
|
const cookies = parseCookies(request.headers.get("cookie"));
|
|
@@ -1361,8 +1490,12 @@ function createPlaybookContext({
|
|
|
1361
1490
|
fallbackVisitorId: own
|
|
1362
1491
|
});
|
|
1363
1492
|
if ("skip" in out) return { ssr: null, skip: out.skip };
|
|
1364
|
-
|
|
1365
|
-
if (d.experienceId
|
|
1493
|
+
let d = out.decision;
|
|
1494
|
+
if (d.experienceId) {
|
|
1495
|
+
const via = declaredSlotFor(d, slotRefs);
|
|
1496
|
+
if (!via) return { ssr: null, skip: "no-slot" };
|
|
1497
|
+
d = { ...d, matchedBy: via };
|
|
1498
|
+
}
|
|
1366
1499
|
ssrRendered = true;
|
|
1367
1500
|
return { ssr: d, skip: null };
|
|
1368
1501
|
})().catch(() => ({ ssr: null, skip: "error" }));
|
|
@@ -1647,12 +1780,7 @@ function PlaybookSsrHead({ ssr, nonce }) {
|
|
|
1647
1780
|
hide ? /* @__PURE__ */ jsx("style", { id: "pb-ssr-hide", nonce, dangerouslySetInnerHTML: { __html: escStyle(hide) } }) : null
|
|
1648
1781
|
] });
|
|
1649
1782
|
}
|
|
1650
|
-
function
|
|
1651
|
-
ssr,
|
|
1652
|
-
anchor,
|
|
1653
|
-
position = "after"
|
|
1654
|
-
}) {
|
|
1655
|
-
if (!ssr || !ssr.experienceId || !ssr.snapshot || !slotMatches(ssr, { anchor, position })) return null;
|
|
1783
|
+
function SsrHost({ ssr }) {
|
|
1656
1784
|
return /* @__PURE__ */ jsx(
|
|
1657
1785
|
"div",
|
|
1658
1786
|
{
|
|
@@ -1665,6 +1793,144 @@ function PlaybookSlot({
|
|
|
1665
1793
|
}
|
|
1666
1794
|
);
|
|
1667
1795
|
}
|
|
1796
|
+
function PlaybookSlot(props) {
|
|
1797
|
+
const { ssr } = props;
|
|
1798
|
+
if (typeof props.name === "string") {
|
|
1799
|
+
const key = props.name;
|
|
1800
|
+
if (!isSlotKey(key)) return null;
|
|
1801
|
+
const marker = /* @__PURE__ */ jsx("template", { "data-pb-slot": key });
|
|
1802
|
+
const hit = !!ssr && !!ssr.experienceId && !!ssr.snapshot && ssr.matchedBy !== "anchor" && slotMatches(ssr, { name: key });
|
|
1803
|
+
if (!hit) return marker;
|
|
1804
|
+
return ssr.arm.a?.p === "before" ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1805
|
+
/* @__PURE__ */ jsx(SsrHost, { ssr }),
|
|
1806
|
+
marker
|
|
1807
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1808
|
+
marker,
|
|
1809
|
+
/* @__PURE__ */ jsx(SsrHost, { ssr })
|
|
1810
|
+
] });
|
|
1811
|
+
}
|
|
1812
|
+
if (!ssr || !ssr.experienceId || !ssr.snapshot || typeof props.anchor !== "string") return null;
|
|
1813
|
+
if (ssr.matchedBy === "slot") return null;
|
|
1814
|
+
if (!slotMatches(ssr, { anchor: props.anchor, position: props.position ?? "after" })) return null;
|
|
1815
|
+
return /* @__PURE__ */ jsx(SsrHost, { ssr });
|
|
1816
|
+
}
|
|
1817
|
+
function keyOf(el) {
|
|
1818
|
+
if (!isValidElement(el) || el.key == null) return null;
|
|
1819
|
+
return String(el.key);
|
|
1820
|
+
}
|
|
1821
|
+
function PlaybookSections({
|
|
1822
|
+
ssr,
|
|
1823
|
+
sections,
|
|
1824
|
+
sectionId
|
|
1825
|
+
}) {
|
|
1826
|
+
if (!sections?.length) return null;
|
|
1827
|
+
return /* @__PURE__ */ jsx(Fragment, { children: sections.map((el, index) => {
|
|
1828
|
+
if (el == null || el === false) return null;
|
|
1829
|
+
const id = (isValidElement(el) && sectionId ? sectionId(el, index) : null) ?? keyOf(el);
|
|
1830
|
+
const before = id ? sectionSlotKey(id, "before") : null;
|
|
1831
|
+
const after = id ? sectionSlotKey(id, "after") : null;
|
|
1832
|
+
const key = id ?? `pb-section-${index}`;
|
|
1833
|
+
if (!before || !after) return /* @__PURE__ */ jsx(Fragment$1, { children: el }, key);
|
|
1834
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
1835
|
+
/* @__PURE__ */ jsx(PlaybookSlot, { ssr, name: before }),
|
|
1836
|
+
el,
|
|
1837
|
+
/* @__PURE__ */ jsx(PlaybookSlot, { ssr, name: after })
|
|
1838
|
+
] }, key);
|
|
1839
|
+
}) });
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
// src/slot-report.ts
|
|
1843
|
+
var SLOT_REPORT_URL = "/apps/playbook/api/storefront/ssr/slots";
|
|
1844
|
+
var SLOT_REPORT_SAMPLE_RATE = 0.1;
|
|
1845
|
+
var SLOT_REPORT_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
1846
|
+
var SLOT_REPORT_MAX_SLOTS = 100;
|
|
1847
|
+
var STORAGE_PREFIX = "_pb_slots:";
|
|
1848
|
+
function renderedSlotKeys(root) {
|
|
1849
|
+
const out = [];
|
|
1850
|
+
try {
|
|
1851
|
+
for (const el of Array.from(root.querySelectorAll(`[${SSR_SLOT_ATTR}]`))) {
|
|
1852
|
+
const key = el.getAttribute(SSR_SLOT_ATTR);
|
|
1853
|
+
if (isSlotKey(key) && !out.includes(key)) out.push(key);
|
|
1854
|
+
if (out.length >= SLOT_REPORT_MAX_SLOTS) break;
|
|
1855
|
+
}
|
|
1856
|
+
} catch {
|
|
1857
|
+
}
|
|
1858
|
+
return out;
|
|
1859
|
+
}
|
|
1860
|
+
function slotReportScope(pageKey) {
|
|
1861
|
+
const type = pageKey.split(":")[0];
|
|
1862
|
+
return type === "product" || type === "collection" ? `${type}:*` : pageKey;
|
|
1863
|
+
}
|
|
1864
|
+
function slotListSignature(slots) {
|
|
1865
|
+
const s2 = [...slots].sort().join("|");
|
|
1866
|
+
let h = 2166136261;
|
|
1867
|
+
for (let i = 0; i < s2.length; i++) {
|
|
1868
|
+
h ^= s2.charCodeAt(i);
|
|
1869
|
+
h = Math.imul(h, 16777619);
|
|
1870
|
+
}
|
|
1871
|
+
return (h >>> 0).toString(36);
|
|
1872
|
+
}
|
|
1873
|
+
function shouldReportSlots(input) {
|
|
1874
|
+
if (!input.slots.length) return false;
|
|
1875
|
+
const sig = slotListSignature(input.slots);
|
|
1876
|
+
try {
|
|
1877
|
+
const raw = input.storage?.getItem(STORAGE_PREFIX + input.scope);
|
|
1878
|
+
if (raw) {
|
|
1879
|
+
const [prevSig, at] = raw.split("|");
|
|
1880
|
+
if (prevSig === sig && input.now - Number(at) < SLOT_REPORT_TTL_MS) return false;
|
|
1881
|
+
}
|
|
1882
|
+
} catch {
|
|
1883
|
+
}
|
|
1884
|
+
return input.random < input.sampleRate;
|
|
1885
|
+
}
|
|
1886
|
+
function rememberSlotReport(storage, scope, slots, now) {
|
|
1887
|
+
try {
|
|
1888
|
+
storage?.setItem(STORAGE_PREFIX + scope, `${slotListSignature(slots)}|${now}`);
|
|
1889
|
+
} catch {
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
function slotReportBody(shopId, pageKey, slots) {
|
|
1893
|
+
return JSON.stringify({ shopId, page: pageKey, slots: slots.slice(0, SLOT_REPORT_MAX_SLOTS) });
|
|
1894
|
+
}
|
|
1895
|
+
function localStore() {
|
|
1896
|
+
try {
|
|
1897
|
+
return window.localStorage;
|
|
1898
|
+
} catch {
|
|
1899
|
+
return null;
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
function reportPageSlots(input) {
|
|
1903
|
+
try {
|
|
1904
|
+
if (!input.shopId) return false;
|
|
1905
|
+
const pageKey = targetPageKey(input.pathname);
|
|
1906
|
+
if (!pageKey) return false;
|
|
1907
|
+
const slots = renderedSlotKeys(input.root ?? document);
|
|
1908
|
+
const storage = input.storage === void 0 ? localStore() : input.storage;
|
|
1909
|
+
const now = (input.now ?? Date.now)();
|
|
1910
|
+
const scope = slotReportScope(pageKey);
|
|
1911
|
+
const go = shouldReportSlots({
|
|
1912
|
+
scope,
|
|
1913
|
+
slots,
|
|
1914
|
+
now,
|
|
1915
|
+
random: (input.random ?? Math.random)(),
|
|
1916
|
+
sampleRate: input.sampleRate ?? SLOT_REPORT_SAMPLE_RATE,
|
|
1917
|
+
storage
|
|
1918
|
+
});
|
|
1919
|
+
if (!go) return false;
|
|
1920
|
+
rememberSlotReport(storage, scope, slots, now);
|
|
1921
|
+
const f = input.fetchImpl ?? fetch;
|
|
1922
|
+
void f(input.url ?? SLOT_REPORT_URL, {
|
|
1923
|
+
method: "POST",
|
|
1924
|
+
headers: { "content-type": "application/json" },
|
|
1925
|
+
body: slotReportBody(input.shopId, pageKey, slots),
|
|
1926
|
+
keepalive: true,
|
|
1927
|
+
credentials: "omit"
|
|
1928
|
+
}).catch(() => void 0);
|
|
1929
|
+
return true;
|
|
1930
|
+
} catch {
|
|
1931
|
+
return false;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1668
1934
|
var PlaybookBoundary = class extends Component {
|
|
1669
1935
|
state = { failed: false };
|
|
1670
1936
|
static getDerivedStateFromError() {
|
|
@@ -2885,6 +3151,19 @@ function PlaybookAAExposure({ testKey = AA_SITEWIDE_TEST }) {
|
|
|
2885
3151
|
const ref = usePlaybookExposure(testKey, variant);
|
|
2886
3152
|
return /* @__PURE__ */ jsx("span", { ref, "aria-hidden": "true", style: SENTINEL_STYLE });
|
|
2887
3153
|
}
|
|
3154
|
+
var SLOT_REPORT_DELAY_MS = 1500;
|
|
3155
|
+
function SlotReport({
|
|
3156
|
+
shopId,
|
|
3157
|
+
pathname,
|
|
3158
|
+
options
|
|
3159
|
+
}) {
|
|
3160
|
+
const { sampleRate, url } = options;
|
|
3161
|
+
useEffect(() => {
|
|
3162
|
+
const timer = setTimeout(() => reportPageSlots({ shopId, pathname, sampleRate, url }), SLOT_REPORT_DELAY_MS);
|
|
3163
|
+
return () => clearTimeout(timer);
|
|
3164
|
+
}, [shopId, pathname, sampleRate, url]);
|
|
3165
|
+
return null;
|
|
3166
|
+
}
|
|
2888
3167
|
function Engagement({ pathname, enabled }) {
|
|
2889
3168
|
usePageEngagement(pathname, enabled);
|
|
2890
3169
|
return null;
|
|
@@ -2905,10 +3184,12 @@ function PlaybookRoot({
|
|
|
2905
3184
|
exposureUrl,
|
|
2906
3185
|
eventUrl,
|
|
2907
3186
|
supportedEvents,
|
|
3187
|
+
slotReport = true,
|
|
2908
3188
|
children
|
|
2909
3189
|
}) {
|
|
2910
3190
|
const hydrated = useHydrated();
|
|
2911
3191
|
const enabled = !!state?.enabled;
|
|
3192
|
+
const slotReportOptions = slotReport === false ? null : slotReport === true ? {} : slotReport;
|
|
2912
3193
|
return /* @__PURE__ */ jsxs(
|
|
2913
3194
|
PlaybookProvider,
|
|
2914
3195
|
{
|
|
@@ -2934,6 +3215,7 @@ function PlaybookRoot({
|
|
|
2934
3215
|
}
|
|
2935
3216
|
) }),
|
|
2936
3217
|
state?.previewToken && /* @__PURE__ */ jsx(PlaybookBoundary, { name: "preview param", children: /* @__PURE__ */ jsx(PreviewParam, { location, navigate }) }),
|
|
3218
|
+
enabled && state?.shopId && slotReportOptions && !editorPreview && /* @__PURE__ */ jsx(PlaybookBoundary, { name: "slot report", children: /* @__PURE__ */ jsx(SlotReport, { shopId: state.shopId, pathname: location.pathname, options: slotReportOptions }) }),
|
|
2937
3219
|
enabled && aaSentinel && !editorPreview && /* @__PURE__ */ jsx(PlaybookBoundary, { name: "aa sentinel", children: /* @__PURE__ */ jsx(PlaybookAAExposure, {}) }),
|
|
2938
3220
|
/* @__PURE__ */ jsx(PlaybookBoundary, { name: "debug panel", children: /* @__PURE__ */ jsx(
|
|
2939
3221
|
PlaybookDebugPanel,
|
|
@@ -3135,4 +3417,4 @@ function trackAddToCartLines(lines, cartLines, source) {
|
|
|
3135
3417
|
}
|
|
3136
3418
|
}
|
|
3137
3419
|
|
|
3138
|
-
export { AA_SITEWIDE_TEST, CART_SYNC_IDLE_MS, CART_VISITOR_ATTRIBUTE, CLOAK_MAX_MS, CLOAK_STYLE_ID, CONSENT_LOADED_EVENTS, CONTROL_ARM, DEFAULT_DATALAYER_EVENT, DEFAULT_EVENT_URL, DEFAULT_EXPERIENCE_SDK_URL, DEFAULT_EXPOSURE_URL, DEFAULT_PLATFORM_URL, DEFAULT_PROXY_ENDPOINT, DEFAULT_SDK_URL, DEFAULT_SUPPORTED_EVENTS, EXPERIENCE_SDK_SCRIPT_ID, EXPERIENCE_VISITOR_CART_ATTRIBUTE, EXPOSURE_CART_ATTRIBUTE, EXPOSURE_COOKIE, LEGACY_VISITOR_COOKIE, MAX_WRITES_PER_CART, PlaybookAAExposure, PlaybookBoundary, PlaybookCloakHead, PlaybookDebugPanel, PlaybookExposure, PlaybookHead, PlaybookProvider, PlaybookRoot, PlaybookSlot, PlaybookSsrHead, SESSION_VISITOR_COOKIE, STANDARD_EVENTS, TRUEVAULT_CONSENT_COOKIE, VISITOR_COOKIE, addedLinesFrom, analyticsConsentFromCookie, assignTest, bucketIndex, buildHeadSnippet, cartHasPlaybookAttribute, chooseVariation, consentFromAnalytics, createCartAttributeReader, createCartAttributeSync, createCartAttributeWriter, createPlaybook, createPlaybookContext, createPlaybookProxyHandler, createTrueVaultConsent, debugPanelAllowed, decideSsr, declareConsentRequired, detectDevice, detectGeo, experienceVisitorIdFromCookies, getBucketRanges, getConsentState, getEqualWeights, getServerTrackedLog, getSessionContext, getSsrPayload, getTargetedPages, getTrackedLog, hasPlaybookParams, hasSentExposure, hash, inNamespace, isAnalyticsConsentGranted, isKillSwitchOn, isPreviewHost, manifestUrl, matchTargetedPage, matchesPathPrefix, mergeAttributes, normalizeExposureList, onConsentChange, onConsentLoaded, playbookBackendUrl, playbookCartAttribute, playbookHeadSnippet, playbookHeadSnippetHtml, playbookTrack, prefetchRuleset, reconcileExperienceCartAttributes, reconcilePlaybookCartAttribute, revealScript, sanitizeManifestPages, sanitizeRuleset, setNextAddSource, shopifyCustomerPrivacyConsent, slotMatches, ssrHostInnerHtml, ssrUrl, subscribeTrackedLog, syncVisitorId, takeAddSource, targetPageKey, trackAddToCart, trackAddToCartLines, trueVaultDecision, truevaultConsent, useAnalyticsAllowed, usePageEngagement, usePlaybookConsent, usePlaybookExposure, usePlaybookInteraction, usePlaybookState, usePlaybookVariant, usePreviewParamRedirect, visitorInRunningTest, visitorSeedScript, waitForConsentAPI, withPlaybook, withPlaybookCartAttribute };
|
|
3420
|
+
export { AA_SITEWIDE_TEST, CART_SYNC_IDLE_MS, CART_VISITOR_ATTRIBUTE, CLOAK_MAX_MS, CLOAK_STYLE_ID, CONSENT_LOADED_EVENTS, CONTROL_ARM, DEFAULT_DATALAYER_EVENT, DEFAULT_EVENT_URL, DEFAULT_EXPERIENCE_SDK_URL, DEFAULT_EXPOSURE_URL, DEFAULT_PLATFORM_URL, DEFAULT_PROXY_ENDPOINT, DEFAULT_SDK_URL, DEFAULT_SUPPORTED_EVENTS, EXPERIENCE_SDK_SCRIPT_ID, EXPERIENCE_VISITOR_CART_ATTRIBUTE, EXPOSURE_CART_ATTRIBUTE, EXPOSURE_COOKIE, LEGACY_VISITOR_COOKIE, MAX_WRITES_PER_CART, PlaybookAAExposure, PlaybookBoundary, PlaybookCloakHead, PlaybookDebugPanel, PlaybookExposure, PlaybookHead, PlaybookProvider, PlaybookRoot, PlaybookSections, PlaybookSlot, PlaybookSsrHead, SECTION_SLOTS, SESSION_VISITOR_COOKIE, SLOT_REPORT_SAMPLE_RATE, SLOT_REPORT_URL, SSR_SLOT_ATTR, SSR_SLOT_NAMES, STANDARD_EVENTS, TRUEVAULT_CONSENT_COOKIE, VISITOR_COOKIE, addedLinesFrom, analyticsConsentFromCookie, armSlot, assignTest, bucketIndex, buildHeadSnippet, cartHasPlaybookAttribute, chooseVariation, consentFromAnalytics, createCartAttributeReader, createCartAttributeSync, createCartAttributeWriter, createPlaybook, createPlaybookContext, createPlaybookProxyHandler, createTrueVaultConsent, debugPanelAllowed, decideSsr, declareConsentRequired, detectDevice, detectGeo, experienceVisitorIdFromCookies, getBucketRanges, getConsentState, getEqualWeights, getServerTrackedLog, getSessionContext, getSsrPayload, getTargetedPages, getTrackedLog, hasPlaybookParams, hasSentExposure, hash, inNamespace, isAnalyticsConsentGranted, isKillSwitchOn, isPreviewHost, isSlotKey, manifestUrl, matchTargetedPage, matchesPathPrefix, mergeAttributes, normalizeExposureList, onConsentChange, onConsentLoaded, playbookBackendUrl, playbookCartAttribute, playbookHeadSnippet, playbookHeadSnippetHtml, playbookTrack, prefetchRuleset, reconcileExperienceCartAttributes, reconcilePlaybookCartAttribute, renderedSlotKeys, reportPageSlots, revealScript, sanitizeManifestPages, sanitizeRuleset, sectionSlotKey, setNextAddSource, shopifyCustomerPrivacyConsent, slotFromSelector, slotMatches, slotSelector, ssrHostInnerHtml, ssrUrl, subscribeTrackedLog, syncVisitorId, takeAddSource, targetPageKey, trackAddToCart, trackAddToCartLines, trueVaultDecision, truevaultConsent, useAnalyticsAllowed, usePageEngagement, usePlaybookConsent, usePlaybookExposure, usePlaybookInteraction, usePlaybookState, usePlaybookVariant, usePreviewParamRedirect, visitorInRunningTest, visitorSeedScript, waitForConsentAPI, withPlaybook, withPlaybookCartAttribute };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heyplaybook/hydrogen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
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.",
|