@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/index.mjs CHANGED
@@ -195,6 +195,7 @@ var AWSAdapter = class {
195
195
  // src/storage.ts
196
196
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
197
197
  var USER_ID_KEY = "amplifyup_user_id";
198
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
198
199
  function generateGuid() {
199
200
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
200
201
  return crypto.randomUUID();
@@ -205,16 +206,39 @@ function generateGuid() {
205
206
  return v.toString(16);
206
207
  });
207
208
  }
209
+ function readCookie(name) {
210
+ if (typeof document === "undefined") return null;
211
+ const parts = document.cookie.split(";");
212
+ for (const part of parts) {
213
+ const idx = part.indexOf("=");
214
+ if (idx === -1) continue;
215
+ if (part.slice(0, idx).trim() !== name) continue;
216
+ const value = part.slice(idx + 1).trim();
217
+ try {
218
+ return value ? decodeURIComponent(value) : null;
219
+ } catch {
220
+ return value || null;
221
+ }
222
+ }
223
+ return null;
224
+ }
225
+ function writeCookie(name, value) {
226
+ if (typeof document === "undefined") return;
227
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax`;
228
+ }
208
229
  function getInstanceId() {
209
230
  if (typeof window === "undefined") {
210
231
  return "";
211
232
  }
212
- const stored = localStorage.getItem(INSTANCE_ID_KEY);
213
- if (stored) {
214
- return stored;
233
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
234
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
235
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
236
+ if (fromLs !== instanceId) {
237
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
238
+ }
239
+ if (fromCookie !== instanceId) {
240
+ writeCookie(ANON_COOKIE_NAME, instanceId);
215
241
  }
216
- const instanceId = `browser_${generateGuid()}`;
217
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
218
242
  return instanceId;
219
243
  }
220
244
  function getUserId() {
@@ -235,6 +259,7 @@ function clearIds() {
235
259
  }
236
260
  localStorage.removeItem(INSTANCE_ID_KEY);
237
261
  localStorage.removeItem(USER_ID_KEY);
262
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
238
263
  }
239
264
 
240
265
  // src/targets.ts
@@ -312,12 +337,17 @@ function resolveAmplifyUpUrls(target) {
312
337
 
313
338
  // src/preview.ts
314
339
  function isPreviewRequest(searchParams) {
340
+ return paramIsTrue(searchParams, "preview") || paramIsTrue(searchParams, "visualEditor");
341
+ }
342
+ function isWorkingContentRequest(searchParams) {
343
+ return paramIsTrue(searchParams, "working");
344
+ }
345
+ function paramIsTrue(searchParams, key) {
315
346
  if (searchParams instanceof URLSearchParams) {
316
- return searchParams.get("preview") === "true" || searchParams.get("visualEditor") === "true";
347
+ return searchParams.get(key) === "true";
317
348
  }
318
- const preview = searchParams.preview;
319
- const visualEditor = searchParams.visualEditor;
320
- return preview === "true" || visualEditor === "true";
349
+ const value = searchParams[key];
350
+ return value === "true" || Array.isArray(value) && value[0] === "true";
321
351
  }
322
352
  function isComposerPreview() {
323
353
  if (typeof window === "undefined") return false;
@@ -357,6 +387,19 @@ function normalizeRoute(route) {
357
387
  return path || "/";
358
388
  }
359
389
 
390
+ // src/edge/variantKeys.ts
391
+ function serializeVariantKeys(keys) {
392
+ if (!keys) return "";
393
+ const parts = [];
394
+ const zoneIds = Object.keys(keys).sort();
395
+ for (const zoneId of zoneIds) {
396
+ const variant = keys[zoneId];
397
+ if (!variant || variant === "default") continue;
398
+ parts.push(`${zoneId}:${variant}`);
399
+ }
400
+ return parts.join(",");
401
+ }
402
+
360
403
  // src/edge/resolve.ts
361
404
  var EdgeResolveError = class extends Error {
362
405
  constructor(message, status, body) {
@@ -374,12 +417,23 @@ async function fetchEdgeResolve(options) {
374
417
  trackingId: options.trackingId
375
418
  });
376
419
  if (options.siteId) params.set("site", options.siteId);
377
- if (options.anonId) params.set("anonId", options.anonId);
420
+ const variantParam = serializeVariantKeys(options.variantKeys);
421
+ if (variantParam) {
422
+ params.set("variants", variantParam);
423
+ } else if (options.anonId) {
424
+ params.set("anonId", options.anonId);
425
+ }
378
426
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
379
- const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, {
427
+ const init2 = {
380
428
  method: "GET",
381
429
  headers: { Accept: "application/json" }
382
- });
430
+ };
431
+ if (options.revalidate !== void 0) {
432
+ init2.next = { revalidate: options.revalidate };
433
+ } else {
434
+ init2.cache = "no-store";
435
+ }
436
+ const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, init2);
383
437
  const body = await res.json().catch(() => ({}));
384
438
  if (!res.ok) {
385
439
  throw new EdgeResolveError(
@@ -468,9 +522,11 @@ async function fetchContent(options) {
468
522
  edgeUrl: options.edgeUrl,
469
523
  trackingId: options.trackingId,
470
524
  route: options.route,
471
- anonId: options.anonId,
525
+ anonId: options.variantKeys ? void 0 : options.anonId,
472
526
  siteId: options.siteId,
473
- fetch: options.fetchFn
527
+ fetch: options.fetchFn,
528
+ revalidate: options.revalidate,
529
+ variantKeys: options.variantKeys
474
530
  });
475
531
  const pageConfig = mapResolvedToPageConfig(payload);
476
532
  const degraded = collectDegraded(payload);
@@ -1453,6 +1509,9 @@ Available Console Commands:
1453
1509
  });
1454
1510
  if (preview) {
1455
1511
  params.set("preview", "true");
1512
+ if (typeof window !== "undefined" && isWorkingContentRequest(new URLSearchParams(window.location.search))) {
1513
+ params.set("working", "true");
1514
+ }
1456
1515
  }
1457
1516
  if (isOnOrchestratorOrigin) {
1458
1517
  return `/api/layout/provider?${params.toString()}`;
@@ -2832,6 +2891,6 @@ var verifyAWSConnection = () => {
2832
2891
  };
2833
2892
  var index_default = getSDK();
2834
2893
 
2835
- export { AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, SESSION_IDLE_GAP_MS, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeResolve, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, mapResolvedToPageConfig, normalizeAmplifyUpTarget, normalizeRoute, page, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
2894
+ export { AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, SESSION_IDLE_GAP_MS, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeResolve, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, isWorkingContentRequest, mapResolvedToPageConfig, normalizeAmplifyUpTarget, normalizeRoute, page, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
2836
2895
  //# sourceMappingURL=index.mjs.map
2837
2896
  //# sourceMappingURL=index.mjs.map