@amplifyup/sdk 0.1.32 → 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
@@ -325,12 +350,17 @@ function resolveAmplifyUpUrls(target) {
325
350
 
326
351
  // src/preview.ts
327
352
  function isPreviewRequest(searchParams) {
353
+ return paramIsTrue(searchParams, "preview") || paramIsTrue(searchParams, "visualEditor");
354
+ }
355
+ function isWorkingContentRequest(searchParams) {
356
+ return paramIsTrue(searchParams, "working");
357
+ }
358
+ function paramIsTrue(searchParams, key) {
328
359
  if (searchParams instanceof URLSearchParams) {
329
- return searchParams.get("preview") === "true" || searchParams.get("visualEditor") === "true";
360
+ return searchParams.get(key) === "true";
330
361
  }
331
- const preview = searchParams.preview;
332
- const visualEditor = searchParams.visualEditor;
333
- return preview === "true" || visualEditor === "true";
362
+ const value = searchParams[key];
363
+ return value === "true" || Array.isArray(value) && value[0] === "true";
334
364
  }
335
365
  function isComposerPreview() {
336
366
  if (typeof window === "undefined") return false;
@@ -370,6 +400,19 @@ function normalizeRoute(route) {
370
400
  return path || "/";
371
401
  }
372
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
+
373
416
  // src/edge/resolve.ts
374
417
  var EdgeResolveError = class extends Error {
375
418
  constructor(message, status, body) {
@@ -387,12 +430,23 @@ async function fetchEdgeResolve(options) {
387
430
  trackingId: options.trackingId
388
431
  });
389
432
  if (options.siteId) params.set("site", options.siteId);
390
- 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
+ }
391
439
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
392
- const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, {
440
+ const init = {
393
441
  method: "GET",
394
442
  headers: { Accept: "application/json" }
395
- });
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);
396
450
  const body = await res.json().catch(() => ({}));
397
451
  if (!res.ok) {
398
452
  throw new EdgeResolveError(
@@ -481,9 +535,11 @@ async function fetchContent(options) {
481
535
  edgeUrl: options.edgeUrl,
482
536
  trackingId: options.trackingId,
483
537
  route: options.route,
484
- anonId: options.anonId,
538
+ anonId: options.variantKeys ? void 0 : options.anonId,
485
539
  siteId: options.siteId,
486
- fetch: options.fetchFn
540
+ fetch: options.fetchFn,
541
+ revalidate: options.revalidate,
542
+ variantKeys: options.variantKeys
487
543
  });
488
544
  const pageConfig = mapResolvedToPageConfig(payload);
489
545
  const degraded = collectDegraded(payload);
@@ -1466,6 +1522,9 @@ Available Console Commands:
1466
1522
  });
1467
1523
  if (preview) {
1468
1524
  params.set("preview", "true");
1525
+ if (typeof window !== "undefined" && isWorkingContentRequest(new URLSearchParams(window.location.search))) {
1526
+ params.set("working", "true");
1527
+ }
1469
1528
  }
1470
1529
  if (isOnOrchestratorOrigin) {
1471
1530
  return `/api/layout/provider?${params.toString()}`;
@@ -3768,6 +3827,21 @@ function defaultPreviewCanvas() {
3768
3827
  function hasAmplifyLayout(pageConfig) {
3769
3828
  return !!(pageConfig && pageConfig.id !== "default" && Array.isArray(pageConfig.layoutTree) && pageConfig.layoutTree.length > 0);
3770
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
+ }
3771
3845
  function AmplifyPageContent({
3772
3846
  fallback,
3773
3847
  renderComponent,
@@ -3781,9 +3855,18 @@ function AmplifyPageContent({
3781
3855
  }) {
3782
3856
  const client = useAmplifyUp();
3783
3857
  const preview = forceComposerPreview || isComposerPreview();
3784
- const pageConfig = pageConfigProp ?? client.pageConfig;
3785
- const loading = pageConfigProp ? false : client.loading;
3786
- 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;
3787
3870
  const previewCanvas = previewEmptyComponent ?? defaultPreviewCanvas();
3788
3871
  const previewLoading = previewLoadingComponent ?? previewCanvas;
3789
3872
  if (preview) {
@@ -3822,18 +3905,21 @@ function AmplifyPageContent({
3822
3905
  }
3823
3906
 
3824
3907
  // src/server.tsx
3908
+ var AMPLIFY_DEFAULT_REVALIDATE = 60;
3825
3909
  async function fetchPageConfigServer(route, trackingId, preview = false, options) {
3826
3910
  const target = resolveAmplifyUpTarget({
3827
3911
  configTarget: options?.target
3828
3912
  });
3829
3913
  const { orchestratorUrl, edgeUrl } = resolveAmplifyUpUrls(target);
3830
3914
  const previewFlag = preview || (options?.searchParams ? isPreviewRequest(options.searchParams) : false);
3915
+ const workingFlag = options?.searchParams ? isWorkingContentRequest(options.searchParams) : false;
3831
3916
  const fetchOrchestratorPage = async (pageRoute, isPreview) => {
3832
3917
  const params = new URLSearchParams({
3833
3918
  route: pageRoute,
3834
3919
  trackingId
3835
3920
  });
3836
3921
  if (isPreview) params.set("preview", "true");
3922
+ if (workingFlag) params.set("working", "true");
3837
3923
  const url = `${orchestratorUrl}/api/layout/provider?${params.toString()}`;
3838
3924
  try {
3839
3925
  const res = await fetch(url, {
@@ -3855,7 +3941,9 @@ async function fetchPageConfigServer(route, trackingId, preview = false, options
3855
3941
  preview: previewFlag,
3856
3942
  orchestratorUrl,
3857
3943
  edgeUrl,
3858
- fetchOrchestratorPage
3944
+ fetchOrchestratorPage,
3945
+ revalidate: previewFlag ? void 0 : options?.revalidate ?? AMPLIFY_DEFAULT_REVALIDATE,
3946
+ variantKeys: previewFlag ? void 0 : options?.variantKeys
3859
3947
  });
3860
3948
  return result.pageConfig;
3861
3949
  }
@@ -3945,10 +4033,9 @@ function AmplifyUpProvider({ children, config }) {
3945
4033
  }, [config.trackingId, config.target, config.debug, config.dev, config.siteId]);
3946
4034
  react.useEffect(() => {
3947
4035
  if (!initialized) return;
4036
+ let cancelled = false;
3948
4037
  const load = async () => {
3949
4038
  try {
3950
- setLoading(true);
3951
- setError(null);
3952
4039
  const actualRoute = getActualRoute();
3953
4040
  if (!actualRoute) {
3954
4041
  setLoading(false);
@@ -3957,6 +4044,15 @@ function AmplifyUpProvider({ children, config }) {
3957
4044
  const sdk = getSDK();
3958
4045
  sdk.routeChange(actualRoute);
3959
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);
3960
4056
  const edgeUrl = sdk.getEdgeUrl();
3961
4057
  const orchestratorUrl = sdk.getOrchestratorUrl();
3962
4058
  const trackingId = sdk.getTrackingId();
@@ -3975,6 +4071,7 @@ function AmplifyUpProvider({ children, config }) {
3975
4071
  edgeUrl,
3976
4072
  fetchOrchestratorPage: (route, isPreview) => sdk.fetchPageConfig(route, isPreview)
3977
4073
  });
4074
+ if (cancelled) return;
3978
4075
  setSource(result.source);
3979
4076
  setMeta(result.meta);
3980
4077
  setError(result.error);
@@ -3990,16 +4087,20 @@ function AmplifyUpProvider({ children, config }) {
3990
4087
  setPageConfig(result.pageConfig);
3991
4088
  }
3992
4089
  } catch (err) {
4090
+ if (cancelled) return;
3993
4091
  console.error("Error fetching page config:", err);
3994
4092
  setError(err instanceof Error ? err.message : "Unknown error");
3995
4093
  setSource(null);
3996
4094
  setMeta(null);
3997
4095
  } finally {
3998
- setLoading(false);
4096
+ if (!cancelled) setLoading(false);
3999
4097
  }
4000
4098
  };
4001
4099
  void load();
4002
- }, [pathname, initialized]);
4100
+ return () => {
4101
+ cancelled = true;
4102
+ };
4103
+ }, [pathname, initialized, config.clientPersonalization]);
4003
4104
  return /* @__PURE__ */ jsxRuntime.jsx(
4004
4105
  AmplifyUpContext.Provider,
4005
4106
  {
@@ -4027,6 +4128,7 @@ exports.Slot = Slot;
4027
4128
  exports.fetchPageConfigServer = fetchPageConfigServer;
4028
4129
  exports.isComposerPreview = isComposerPreview;
4029
4130
  exports.isPreviewRequest = isPreviewRequest;
4131
+ exports.isWorkingContentRequest = isWorkingContentRequest;
4030
4132
  exports.useAmplifyUp = useAmplifyUp;
4031
4133
  exports.useComponentProps = useComponentProps;
4032
4134
  exports.useLiveComposerProps = useLiveComposerProps;