@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/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
@@ -362,6 +387,19 @@ function normalizeRoute(route) {
362
387
  return path || "/";
363
388
  }
364
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
+
365
403
  // src/edge/resolve.ts
366
404
  var EdgeResolveError = class extends Error {
367
405
  constructor(message, status, body) {
@@ -379,12 +417,23 @@ async function fetchEdgeResolve(options) {
379
417
  trackingId: options.trackingId
380
418
  });
381
419
  if (options.siteId) params.set("site", options.siteId);
382
- 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
+ }
383
426
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
384
- const res = await fetchFn(`${base}/v1/resolve?${params.toString()}`, {
427
+ const init2 = {
385
428
  method: "GET",
386
429
  headers: { Accept: "application/json" }
387
- });
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);
388
437
  const body = await res.json().catch(() => ({}));
389
438
  if (!res.ok) {
390
439
  throw new EdgeResolveError(
@@ -473,9 +522,11 @@ async function fetchContent(options) {
473
522
  edgeUrl: options.edgeUrl,
474
523
  trackingId: options.trackingId,
475
524
  route: options.route,
476
- anonId: options.anonId,
525
+ anonId: options.variantKeys ? void 0 : options.anonId,
477
526
  siteId: options.siteId,
478
- fetch: options.fetchFn
527
+ fetch: options.fetchFn,
528
+ revalidate: options.revalidate,
529
+ variantKeys: options.variantKeys
479
530
  });
480
531
  const pageConfig = mapResolvedToPageConfig(payload);
481
532
  const degraded = collectDegraded(payload);