@amplifyup/sdk 0.1.33 → 0.1.34

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/react.mjs CHANGED
@@ -206,6 +206,7 @@ var AWSAdapter = class {
206
206
  // src/storage.ts
207
207
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
208
208
  var USER_ID_KEY = "amplifyup_user_id";
209
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
209
210
  function generateGuid() {
210
211
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
211
212
  return crypto.randomUUID();
@@ -216,16 +217,39 @@ function generateGuid() {
216
217
  return v.toString(16);
217
218
  });
218
219
  }
220
+ function readCookie(name) {
221
+ if (typeof document === "undefined") return null;
222
+ const parts = document.cookie.split(";");
223
+ for (const part of parts) {
224
+ const idx = part.indexOf("=");
225
+ if (idx === -1) continue;
226
+ if (part.slice(0, idx).trim() !== name) continue;
227
+ const value = part.slice(idx + 1).trim();
228
+ try {
229
+ return value ? decodeURIComponent(value) : null;
230
+ } catch {
231
+ return value || null;
232
+ }
233
+ }
234
+ return null;
235
+ }
236
+ function writeCookie(name, value) {
237
+ if (typeof document === "undefined") return;
238
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax`;
239
+ }
219
240
  function getInstanceId() {
220
241
  if (typeof window === "undefined") {
221
242
  return "";
222
243
  }
223
- const stored = localStorage.getItem(INSTANCE_ID_KEY);
224
- if (stored) {
225
- return stored;
244
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
245
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
246
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
247
+ if (fromLs !== instanceId) {
248
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
249
+ }
250
+ if (fromCookie !== instanceId) {
251
+ writeCookie(ANON_COOKIE_NAME, instanceId);
226
252
  }
227
- const instanceId = `browser_${generateGuid()}`;
228
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
229
253
  return instanceId;
230
254
  }
231
255
  function getUserId() {
@@ -246,6 +270,7 @@ function clearIds() {
246
270
  }
247
271
  localStorage.removeItem(INSTANCE_ID_KEY);
248
272
  localStorage.removeItem(USER_ID_KEY);
273
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
249
274
  }
250
275
 
251
276
  // src/targets.ts
@@ -373,6 +398,19 @@ function normalizeRoute(route) {
373
398
  return path || "/";
374
399
  }
375
400
 
401
+ // src/edge/variantKeys.ts
402
+ function serializeVariantKeys(keys) {
403
+ if (!keys) return "";
404
+ const parts = [];
405
+ const zoneIds = Object.keys(keys).sort();
406
+ for (const zoneId of zoneIds) {
407
+ const variant = keys[zoneId];
408
+ if (!variant || variant === "default") continue;
409
+ parts.push(`${zoneId}:${variant}`);
410
+ }
411
+ return parts.join(",");
412
+ }
413
+
376
414
  // src/edge/resolve.ts
377
415
  var EdgeResolveError = class extends Error {
378
416
  constructor(message, status, body) {
@@ -390,12 +428,23 @@ async function fetchEdgeResolve(options) {
390
428
  trackingId: options.trackingId
391
429
  });
392
430
  if (options.siteId) params.set("site", options.siteId);
393
- if (options.anonId) params.set("anonId", options.anonId);
431
+ const variantParam = serializeVariantKeys(options.variantKeys);
432
+ if (variantParam) {
433
+ params.set("variants", variantParam);
434
+ } else if (options.anonId) {
435
+ params.set("anonId", options.anonId);
436
+ }
394
437
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
395
- const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, {
438
+ const init = {
396
439
  method: "GET",
397
440
  headers: { Accept: "application/json" }
398
- });
441
+ };
442
+ if (options.revalidate !== void 0) {
443
+ init.next = { revalidate: options.revalidate };
444
+ } else {
445
+ init.cache = "no-store";
446
+ }
447
+ const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, init);
399
448
  const body = await res.json().catch(() => ({}));
400
449
  if (!res.ok) {
401
450
  throw new EdgeResolveError(
@@ -484,9 +533,11 @@ async function fetchContent(options) {
484
533
  edgeUrl: options.edgeUrl,
485
534
  trackingId: options.trackingId,
486
535
  route: options.route,
487
- anonId: options.anonId,
536
+ anonId: options.variantKeys ? void 0 : options.anonId,
488
537
  siteId: options.siteId,
489
- fetch: options.fetchFn
538
+ fetch: options.fetchFn,
539
+ revalidate: options.revalidate,
540
+ variantKeys: options.variantKeys
490
541
  });
491
542
  const pageConfig = mapResolvedToPageConfig(payload);
492
543
  const degraded = collectDegraded(payload);
@@ -3774,6 +3825,21 @@ function defaultPreviewCanvas() {
3774
3825
  function hasAmplifyLayout(pageConfig) {
3775
3826
  return !!(pageConfig && pageConfig.id !== "default" && Array.isArray(pageConfig.layoutTree) && pageConfig.layoutTree.length > 0);
3776
3827
  }
3828
+ function normalizeRoutePath(route) {
3829
+ if (!route) return "";
3830
+ const path = route.split("?")[0] || "/";
3831
+ if (path.length > 1 && path.endsWith("/")) return path.replace(/\/+$/, "");
3832
+ return path || "/";
3833
+ }
3834
+ function samePageRoute(a, b) {
3835
+ const ar = normalizeRoutePath(a?.route);
3836
+ const br = normalizeRoutePath(b?.route);
3837
+ return !!ar && !!br && ar === br;
3838
+ }
3839
+ function layoutTreesEqual(a, b) {
3840
+ if (!a || !b) return a === b;
3841
+ return JSON.stringify(a.layoutTree) === JSON.stringify(b.layoutTree);
3842
+ }
3777
3843
  function AmplifyPageContent({
3778
3844
  fallback,
3779
3845
  renderComponent,
@@ -3787,9 +3853,18 @@ function AmplifyPageContent({
3787
3853
  }) {
3788
3854
  const client = useAmplifyUp();
3789
3855
  const preview = forceComposerPreview || isComposerPreview();
3790
- const pageConfig = pageConfigProp ?? client.pageConfig;
3791
- const loading = pageConfigProp ? false : client.loading;
3792
- const error = pageConfigProp ? null : client.error;
3856
+ const clientLayout = hasAmplifyLayout(client.pageConfig) ? client.pageConfig : null;
3857
+ const serverLayout = hasAmplifyLayout(pageConfigProp) ? pageConfigProp : null;
3858
+ let pageConfig = serverLayout ?? clientLayout;
3859
+ if (preview && clientLayout) {
3860
+ pageConfig = clientLayout;
3861
+ } else if (clientLayout && serverLayout && samePageRoute(clientLayout, serverLayout) && !layoutTreesEqual(clientLayout, serverLayout)) {
3862
+ pageConfig = clientLayout;
3863
+ } else if (clientLayout && !serverLayout) {
3864
+ pageConfig = clientLayout;
3865
+ }
3866
+ const loading = !hasAmplifyLayout(pageConfig) && client.loading;
3867
+ const error = hasAmplifyLayout(pageConfig) ? null : pageConfigProp ? null : client.error;
3793
3868
  const previewCanvas = previewEmptyComponent ?? defaultPreviewCanvas();
3794
3869
  const previewLoading = previewLoadingComponent ?? previewCanvas;
3795
3870
  if (preview) {
@@ -3828,6 +3903,7 @@ function AmplifyPageContent({
3828
3903
  }
3829
3904
 
3830
3905
  // src/server.tsx
3906
+ var AMPLIFY_DEFAULT_REVALIDATE = 60;
3831
3907
  async function fetchPageConfigServer(route, trackingId, preview = false, options) {
3832
3908
  const target = resolveAmplifyUpTarget({
3833
3909
  configTarget: options?.target
@@ -3863,7 +3939,9 @@ async function fetchPageConfigServer(route, trackingId, preview = false, options
3863
3939
  preview: previewFlag,
3864
3940
  orchestratorUrl,
3865
3941
  edgeUrl,
3866
- fetchOrchestratorPage
3942
+ fetchOrchestratorPage,
3943
+ revalidate: previewFlag ? void 0 : options?.revalidate ?? AMPLIFY_DEFAULT_REVALIDATE,
3944
+ variantKeys: previewFlag ? void 0 : options?.variantKeys
3867
3945
  });
3868
3946
  return result.pageConfig;
3869
3947
  }
@@ -3953,10 +4031,9 @@ function AmplifyUpProvider({ children, config }) {
3953
4031
  }, [config.trackingId, config.target, config.debug, config.dev, config.siteId]);
3954
4032
  useEffect(() => {
3955
4033
  if (!initialized) return;
4034
+ let cancelled = false;
3956
4035
  const load = async () => {
3957
4036
  try {
3958
- setLoading(true);
3959
- setError(null);
3960
4037
  const actualRoute = getActualRoute();
3961
4038
  if (!actualRoute) {
3962
4039
  setLoading(false);
@@ -3965,6 +4042,15 @@ function AmplifyUpProvider({ children, config }) {
3965
4042
  const sdk = getSDK();
3966
4043
  sdk.routeChange(actualRoute);
3967
4044
  const preview = isComposerPreview();
4045
+ const overlay = preview || config.clientPersonalization !== false;
4046
+ if (!overlay) {
4047
+ setPageConfig(null);
4048
+ setLoading(false);
4049
+ return;
4050
+ }
4051
+ setPageConfig(null);
4052
+ setError(null);
4053
+ setLoading(false);
3968
4054
  const edgeUrl = sdk.getEdgeUrl();
3969
4055
  const orchestratorUrl = sdk.getOrchestratorUrl();
3970
4056
  const trackingId = sdk.getTrackingId();
@@ -3983,6 +4069,7 @@ function AmplifyUpProvider({ children, config }) {
3983
4069
  edgeUrl,
3984
4070
  fetchOrchestratorPage: (route, isPreview) => sdk.fetchPageConfig(route, isPreview)
3985
4071
  });
4072
+ if (cancelled) return;
3986
4073
  setSource(result.source);
3987
4074
  setMeta(result.meta);
3988
4075
  setError(result.error);
@@ -3998,16 +4085,20 @@ function AmplifyUpProvider({ children, config }) {
3998
4085
  setPageConfig(result.pageConfig);
3999
4086
  }
4000
4087
  } catch (err) {
4088
+ if (cancelled) return;
4001
4089
  console.error("Error fetching page config:", err);
4002
4090
  setError(err instanceof Error ? err.message : "Unknown error");
4003
4091
  setSource(null);
4004
4092
  setMeta(null);
4005
4093
  } finally {
4006
- setLoading(false);
4094
+ if (!cancelled) setLoading(false);
4007
4095
  }
4008
4096
  };
4009
4097
  void load();
4010
- }, [pathname, initialized]);
4098
+ return () => {
4099
+ cancelled = true;
4100
+ };
4101
+ }, [pathname, initialized, config.clientPersonalization]);
4011
4102
  return /* @__PURE__ */ jsx(
4012
4103
  AmplifyUpContext.Provider,
4013
4104
  {