@pack/react 4.1.0 → 4.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.
@@ -1,5 +1,6 @@
1
1
  import React, { type ReactNode } from "react";
2
2
  import { type ErrorTrackingOptions } from "@pack/errors-react";
3
+ import "./pack-debug";
3
4
  type PackContextValue = {
4
5
  isPreview: boolean;
5
6
  customizerMeta?: {
@@ -1 +1 @@
1
- {"version":3,"file":"pack-context.d.ts","sourceRoot":"","sources":["../../src/pack/pack-context.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAKZ,KAAK,SAAS,EAEf,MAAM,OAAO,CAAC;AAEf,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,oBAAoB,CAAC;AAK5B,KAAK,gBAAgB,GAAG;IACtB,SAAS,EAAE,OAAO,CAAC;IACnB,cAAc,CAAC,EAAE;QACf,OAAO,CAAC,EAAE;YACR,GAAG,CAAC,EAAE,MAAM,CAAC;YACb,OAAO,CAAC,EAAE,MAAM,CAAC;SAClB,CAAC;QACF,cAAc,CAAC,EAAE;YACf,MAAM,CAAC,EAAE,MAAM,CAAC;YAChB,SAAS,CAAC,EAAE,MAAM,CAAC;YACnB,iBAAiB,CAAC,EAAE,MAAM,CAAC;YAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;YACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;SAC5B,CAAC;QACF,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACpB,CAAC;IACF,YAAY,EAAE,GAAG,CAAC;IAClB,eAAe,EAAE,CAAC,YAAY,EAAE,GAAG,KAAK,IAAI,CAAC;IAC7C,WAAW,EAAE,GAAG,CAAC;IACjB,cAAc,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,CAAC;IACvC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,MAAM,EAAE;QACN,QAAQ,EAAE,MAAM,CAAC;QACjB,OAAO,EAAE,MAAM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MAAM,CAAC;QACjB,OAAO,EAAE,MAAM,CAAC;KACjB,GAAG,IAAI,CAAC;IACT,WAAW,CAAC,EAAE,GAAG,CAAC;IAClB,iBAAiB,CAAC,EAAE,oBAAoB,CAAC;CAC1C;AAUD,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,aAAa,EACb,oBAA4B,EAC5B,YAAY,EAAE,yBAAyB,EACvC,cAAc,EACd,kBAAuB,EACvB,MAAM,EAAE,aAAoB,EAC5B,iBAAiB,GAClB,EAAE,gBAAgB,qBA6DlB;AAED,eAAO,MAAM,eAAe,qBAAe,CAAC"}
@@ -4,6 +4,7 @@ import { usePackErrorTrack, PackErrorBoundary, } from "@pack/errors-react";
4
4
  import { PreviewToast } from "./preview-toast";
5
5
  import { useOverlayScript } from "../use-overlay-script";
6
6
  import { useCustomizerShell } from "../use-customizer-shell";
7
+ import "./pack-debug";
7
8
  export const PackContext = createContext({
8
9
  isPreview: false,
9
10
  setSiteSettings: () => { },
@@ -0,0 +1,22 @@
1
+ interface StorefrontTestInfo {
2
+ test: string;
3
+ variant: string;
4
+ expiresAt: string | null;
5
+ }
6
+ interface StorefrontTestRulesCacheInfo {
7
+ rulesCount: number;
8
+ cachedAt: string;
9
+ ageSeconds: number;
10
+ isFresh: boolean;
11
+ }
12
+ interface PackDebugHelpers {
13
+ getTestInfo(): StorefrontTestInfo | null;
14
+ getTestRulesCacheInfo(): StorefrontTestRulesCacheInfo | null;
15
+ }
16
+ declare global {
17
+ interface Window {
18
+ packDebug?: PackDebugHelpers;
19
+ }
20
+ }
21
+ export {};
22
+ //# sourceMappingURL=pack-debug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pack-debug.d.ts","sourceRoot":"","sources":["../../src/pack/pack-debug.ts"],"names":[],"mappings":"AAmBA,UAAU,kBAAkB;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,UAAU,4BAA4B;IACpC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,UAAU,gBAAgB;IACxB,WAAW,IAAI,kBAAkB,GAAG,IAAI,CAAC;IACzC,qBAAqB,IAAI,4BAA4B,GAAG,IAAI,CAAC;CAC9D;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,SAAS,CAAC,EAAE,gBAAgB,CAAC;KAC9B;CACF;AA0HD,OAAO,EAAE,CAAC"}
@@ -0,0 +1,105 @@
1
+ // Must mirror @pack/hydrogen/src/constants.ts (PACK_TEST_COOKIE_ID).
2
+ const PACK_TEST_COOKIE_ID = "__pack_test";
3
+ // Must mirror @pack/hydrogen/src/tests/test.ts cacheStrategy.maxAge (1 hour).
4
+ // Keep this in sync with @pack/hydrogen/src/session/test-session.ts default cacheDurationMs.
5
+ const TEST_RULES_CACHE_TTL_SECONDS = 3600;
6
+ function getCookieValue(cookieHeader, name) {
7
+ const prefix = `${name}=`;
8
+ const values = cookieHeader.split(";");
9
+ for (const value of values) {
10
+ const cookie = value.trim();
11
+ if (cookie.startsWith(prefix)) {
12
+ return cookie.slice(prefix.length);
13
+ }
14
+ }
15
+ return null;
16
+ }
17
+ function decodeCookieValue(value) {
18
+ try {
19
+ return decodeURIComponent(value);
20
+ }
21
+ catch {
22
+ return value;
23
+ }
24
+ }
25
+ function decodeBase64Value(value) {
26
+ try {
27
+ const binary = atob(value);
28
+ const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
29
+ return new TextDecoder().decode(bytes);
30
+ }
31
+ catch {
32
+ return null;
33
+ }
34
+ }
35
+ function parseTestSessionCookie(cookieHeader) {
36
+ const rawValue = getCookieValue(cookieHeader, PACK_TEST_COOKIE_ID);
37
+ if (!rawValue) {
38
+ return null;
39
+ }
40
+ const decodedValue = decodeCookieValue(rawValue);
41
+ const signatureDelimiterIndex = decodedValue.lastIndexOf(".");
42
+ const unsignedValue = signatureDelimiterIndex === -1
43
+ ? decodedValue
44
+ : decodedValue.slice(0, signatureDelimiterIndex);
45
+ const payload = decodeBase64Value(unsignedValue);
46
+ if (!payload) {
47
+ return null;
48
+ }
49
+ try {
50
+ const parsed = JSON.parse(payload);
51
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
52
+ return parsed;
53
+ }
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ return null;
59
+ }
60
+ function getStorefrontTestInfo() {
61
+ const payload = parseTestSessionCookie(document.cookie || "");
62
+ const test = payload?.test_data?.handle;
63
+ const variant = payload?.test_data?.testVariant?.handle;
64
+ if (!test || !variant) {
65
+ return null;
66
+ }
67
+ return {
68
+ test,
69
+ variant,
70
+ expiresAt: payload?.test_expire_at || null,
71
+ };
72
+ }
73
+ function getStorefrontTestRulesCacheInfo() {
74
+ const payload = parseTestSessionCookie(document.cookie || "");
75
+ const timestamp = payload?.test_rules_cache_timestamp;
76
+ if (typeof timestamp !== "number" || Number.isNaN(timestamp)) {
77
+ return null;
78
+ }
79
+ const cachedAtDate = new Date(timestamp);
80
+ if (Number.isNaN(cachedAtDate.getTime())) {
81
+ return null;
82
+ }
83
+ const ageSeconds = Math.max(0, Math.floor((Date.now() - timestamp) / 1000));
84
+ const rulesCount = Array.isArray(payload?.test_rules_cache)
85
+ ? payload.test_rules_cache.length
86
+ : 0;
87
+ return {
88
+ rulesCount,
89
+ cachedAt: cachedAtDate.toISOString(),
90
+ ageSeconds,
91
+ isFresh: ageSeconds < TEST_RULES_CACHE_TTL_SECONDS,
92
+ };
93
+ }
94
+ function initializePackDebugHelpers() {
95
+ if (typeof window === "undefined" || typeof document === "undefined") {
96
+ return;
97
+ }
98
+ window.packDebug = {
99
+ ...window.packDebug,
100
+ getTestInfo: getStorefrontTestInfo,
101
+ getTestRulesCacheInfo: getStorefrontTestRulesCacheInfo,
102
+ };
103
+ }
104
+ initializePackDebugHelpers();
105
+ export {};
@@ -1 +1 @@
1
- {"version":3,"file":"use-customizer-shell.d.ts","sourceRoot":"","sources":["../src/use-customizer-shell.tsx"],"names":[],"mappings":"AAeA,eAAO,MAAM,kBAAkB,YA2R9B,CAAC"}
1
+ {"version":3,"file":"use-customizer-shell.d.ts","sourceRoot":"","sources":["../src/use-customizer-shell.tsx"],"names":[],"mappings":"AAeA,eAAO,MAAM,kBAAkB,YAiT9B,CAAC"}
@@ -13,8 +13,8 @@ export const useCustomizerShell = () => {
13
13
  description: liveContent?.description,
14
14
  };
15
15
  const [hasInit, setHasInit] = useState(false);
16
+ const [currentPath, setCurrentPath] = useState(undefined);
16
17
  const pendingMessagesRef = useRef(new Map());
17
- const currentPathRef = useRef(undefined);
18
18
  const parentLocaleIdRef = useRef(null);
19
19
  const sendToParent = useCallback(async (type, data) => {
20
20
  const messageId = crypto.randomUUID();
@@ -179,28 +179,46 @@ export const useCustomizerShell = () => {
179
179
  return;
180
180
  // eslint-disable-next-line react-hooks/exhaustive-deps
181
181
  }, [data.handle, sendToParent, storefrontSettingsSchema]);
182
- // Update the ref whenever pathname changes
182
+ // Track all pathname changes (client-side routing, popstate, hard navs)
183
+ // so SET_CURRENT_ROUTE fires even between pages that both lack CMS content.
183
184
  useEffect(() => {
184
- currentPathRef.current = window.location.pathname;
185
- }, [data.handle]);
185
+ setCurrentPath(window.location.pathname);
186
+ const updatePath = () => setCurrentPath(window.location.pathname);
187
+ // Intercept pushState/replaceState for client-side routing (e.g. React Router)
188
+ const originalPushState = history.pushState.bind(history);
189
+ const originalReplaceState = history.replaceState.bind(history);
190
+ history.pushState = (...args) => {
191
+ originalPushState(...args);
192
+ updatePath();
193
+ };
194
+ history.replaceState = (...args) => {
195
+ originalReplaceState(...args);
196
+ updatePath();
197
+ };
198
+ // Handle back/forward navigation
199
+ window.addEventListener("popstate", updatePath);
200
+ return () => {
201
+ window.removeEventListener("popstate", updatePath);
202
+ history.pushState = originalPushState;
203
+ history.replaceState = originalReplaceState;
204
+ };
205
+ }, []);
186
206
  // Tell the parent window about the current route
187
207
  useEffect(() => {
188
208
  const { template, templateType, handle, title, description } = data;
189
- if (!hasInit ||
190
- !currentPathRef.current ||
191
- (!template && !templateType && !handle && !title && !description)) {
209
+ if (!hasInit || !currentPath) {
192
210
  return;
193
211
  }
194
212
  sendToParent("SET_CURRENT_ROUTE", {
195
213
  environment: environment || "",
196
- currentPath: currentPathRef.current,
197
- template,
198
- templateType,
199
- handle,
200
- title,
201
- description,
214
+ currentPath,
215
+ template: template || null,
216
+ templateType: templateType || null,
217
+ handle: handle || null,
218
+ title: title || null,
219
+ description: description || null,
202
220
  });
203
- }, [hasInit, environment, data.handle, sendToParent]);
221
+ }, [hasInit, environment, currentPath, data.handle, sendToParent]);
204
222
  // Notify parent when locale changes (but not when it came from parent)
205
223
  useEffect(() => {
206
224
  if (!hasInit)
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "4.1.0";
1
+ export const VERSION = "4.2.1";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pack/react",
3
3
  "description": "React",
4
- "version": "4.1.0",
4
+ "version": "4.2.1",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "engines": {