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