@amplifyup/sdk 0.1.74 → 0.1.75

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
@@ -837,6 +837,7 @@ function applyPageContext(input, opts) {
837
837
  }
838
838
  const pageFields = {};
839
839
  for (const pf of opts.pageData?.pageFields || []) {
840
+ if (pf.dataType === "taxonomy") continue;
840
841
  if (pf.source === "static") {
841
842
  if (pf.value !== void 0) pageFields[pf.key] = pf.value;
842
843
  continue;
@@ -859,6 +860,7 @@ function applyPageContext(input, opts) {
859
860
  function staticPageFieldsFromSnapshot(pageData) {
860
861
  const pageFields = {};
861
862
  for (const pf of pageData?.pageFields || []) {
863
+ if (pf.dataType === "taxonomy") continue;
862
864
  if (pf.source === "static" && pf.value !== void 0) {
863
865
  pageFields[pf.key] = pf.value;
864
866
  }
@@ -876,6 +878,11 @@ function attachStaticPageContext(pageData, pageRoute) {
876
878
  pageRoute: pageRoute || ""
877
879
  };
878
880
  }
881
+ function withPageTaxonomies(properties, taxonomies) {
882
+ if (!taxonomies || Object.keys(taxonomies).length === 0) return properties;
883
+ const page = properties.page && typeof properties.page === "object" && !Array.isArray(properties.page) ? properties.page : { fields: {} };
884
+ return { ...properties, page: { ...page, taxonomies } };
885
+ }
879
886
  function mergeEventContext(properties, attached) {
880
887
  if (!attached) return properties;
881
888
  return {
@@ -945,6 +952,27 @@ function hydratePageResourceProps(props, fields) {
945
952
  return out;
946
953
  }
947
954
 
955
+ // src/edge/normalizeRoute.ts
956
+ function normalizeRoute(route) {
957
+ let path = String(route || "").trim();
958
+ if (!path) return "/";
959
+ try {
960
+ if (/^https?:\/\//i.test(path)) {
961
+ path = new URL(path).pathname;
962
+ }
963
+ } catch {
964
+ }
965
+ const q = path.indexOf("?");
966
+ if (q >= 0) path = path.slice(0, q);
967
+ const h = path.indexOf("#");
968
+ if (h >= 0) path = path.slice(0, h);
969
+ if (!path.startsWith("/")) path = `/${path}`;
970
+ if (path.length > 1 && path.endsWith("/")) {
971
+ path = path.replace(/\/+$/, "");
972
+ }
973
+ return path || "/";
974
+ }
975
+
948
976
  // src/page-resource-runtime.ts
949
977
  var pageResourceFields = null;
950
978
  function setPageResourceFields(fields) {
@@ -1042,27 +1070,6 @@ function previewLayoutReady(opts) {
1042
1070
  return true;
1043
1071
  }
1044
1072
 
1045
- // src/edge/normalizeRoute.ts
1046
- function normalizeRoute(route) {
1047
- let path = String(route || "").trim();
1048
- if (!path) return "/";
1049
- try {
1050
- if (/^https?:\/\//i.test(path)) {
1051
- path = new URL(path).pathname;
1052
- }
1053
- } catch {
1054
- }
1055
- const q = path.indexOf("?");
1056
- if (q >= 0) path = path.slice(0, q);
1057
- const h = path.indexOf("#");
1058
- if (h >= 0) path = path.slice(0, h);
1059
- if (!path.startsWith("/")) path = `/${path}`;
1060
- if (path.length > 1 && path.endsWith("/")) {
1061
- path = path.replace(/\/+$/, "");
1062
- }
1063
- return path || "/";
1064
- }
1065
-
1066
1073
  // src/edge/variantKeys.ts
1067
1074
  function serializeVariantKeys(keys) {
1068
1075
  if (!keys) return "";
@@ -1184,7 +1191,8 @@ function mapResolvedToPageConfig(payload) {
1184
1191
  },
1185
1192
  ...payload.eventTracking && payload.eventTracking.length > 0 ? { eventTracking: payload.eventTracking } : {},
1186
1193
  ...payload.pageData ? { pageData: payload.pageData } : {},
1187
- ...payload.pageResources ? { pageResources: payload.pageResources } : {}
1194
+ ...payload.pageResources ? { pageResources: payload.pageResources } : {},
1195
+ ...payload.pageTaxonomies ? { pageTaxonomies: payload.pageTaxonomies } : {}
1188
1196
  };
1189
1197
  }
1190
1198
  function collectDegraded(payload) {
@@ -1416,6 +1424,7 @@ function searchSpec(pagination, field, term) {
1416
1424
  }
1417
1425
 
1418
1426
  // src/index.ts
1427
+ var PAGE_VIEW_HOLD_MS = 1500;
1419
1428
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
1420
1429
  var DWELL_POLL_MS = 5e3;
1421
1430
  function readPreviewQueryOverrides() {
@@ -1489,6 +1498,12 @@ var AmplifyUpSDK = class {
1489
1498
  this.resolvedPageResource = null;
1490
1499
  this.resolvedPageRoute = "";
1491
1500
  this.resolvedRouteParams = {};
1501
+ /** Path whose delivery response is in flight; page_view for it is held. */
1502
+ this.pendingResolvePath = null;
1503
+ this.heldPageView = null;
1504
+ this.resolvedPageTaxonomies = null;
1505
+ /** Path the taxonomies above were resolved for. */
1506
+ this.resolvedTaxonomiesPath = null;
1492
1507
  this.historyHooksInstalled = false;
1493
1508
  this.pageEngagementState = {
1494
1509
  reached: /* @__PURE__ */ new Set(),
@@ -2019,8 +2034,10 @@ var AmplifyUpSDK = class {
2019
2034
  * Apply published pageData (from /v1/resolve) so field validation and
2020
2035
  * mapped page fields can run.
2021
2036
  */
2022
- applyResolvedPageData(pageData, pageRoute, routeParams, pageResources) {
2037
+ applyResolvedPageData(pageData, pageRoute, routeParams, pageResources, pageTaxonomies) {
2023
2038
  this.resolvedPageData = pageData || null;
2039
+ this.resolvedPageTaxonomies = pageTaxonomies || null;
2040
+ this.resolvedTaxonomiesPath = this.pendingResolvePath;
2024
2041
  if (pageRoute) this.resolvedPageRoute = pageRoute;
2025
2042
  if (routeParams) this.resolvedRouteParams = routeParams;
2026
2043
  const firstResourceName = pageData?.resources?.[0]?.name;
@@ -2054,6 +2071,28 @@ var AmplifyUpSDK = class {
2054
2071
  getAttachedPageContext() {
2055
2072
  return this.attachedPageContext;
2056
2073
  }
2074
+ /**
2075
+ * The page's delivery response for `path` is on its way. A page_view for the
2076
+ * same path waits for it (up to PAGE_VIEW_HOLD_MS) so it carries the page's
2077
+ * taxonomies.
2078
+ */
2079
+ beginPageResolve(path) {
2080
+ this.pendingResolvePath = normalizeRoute(path);
2081
+ this.resolvedPageTaxonomies = null;
2082
+ this.resolvedTaxonomiesPath = null;
2083
+ }
2084
+ endPageResolve(path) {
2085
+ if (this.pendingResolvePath !== normalizeRoute(path)) return;
2086
+ this.pendingResolvePath = null;
2087
+ this.flushHeldPageView();
2088
+ }
2089
+ flushHeldPageView() {
2090
+ const held = this.heldPageView;
2091
+ if (!held) return;
2092
+ clearTimeout(held.timer);
2093
+ this.heldPageView = null;
2094
+ this.sendPageView(held.url, held.properties);
2095
+ }
2057
2096
  /**
2058
2097
  * Track a page view
2059
2098
  */
@@ -2064,6 +2103,21 @@ var AmplifyUpSDK = class {
2064
2103
  if (!this.isInitialized()) {
2065
2104
  return;
2066
2105
  }
2106
+ this.lastTrackedPath = url;
2107
+ if (this.pendingResolvePath && this.pendingResolvePath === normalizeRoute(url)) {
2108
+ this.flushHeldPageView();
2109
+ this.heldPageView = {
2110
+ url,
2111
+ properties,
2112
+ timer: setTimeout(() => this.flushHeldPageView(), PAGE_VIEW_HOLD_MS)
2113
+ };
2114
+ return;
2115
+ }
2116
+ this.flushHeldPageView();
2117
+ this.sendPageView(url, properties);
2118
+ }
2119
+ sendPageView(url, properties) {
2120
+ const taxonomies = this.resolvedTaxonomiesPath && this.resolvedTaxonomiesPath === normalizeRoute(url) ? this.resolvedPageTaxonomies : null;
2067
2121
  const instanceId = getInstanceId();
2068
2122
  const sessionId = getSessionId();
2069
2123
  const pageProperties = {
@@ -2075,10 +2129,9 @@ var AmplifyUpSDK = class {
2075
2129
  sessionId,
2076
2130
  trackingId: this.config?.trackingId,
2077
2131
  // Timestamp is included in event payload
2078
- ...mergeEventContext(properties || {}, this.attachedPageContext)
2132
+ ...withPageTaxonomies(mergeEventContext(properties || {}, this.attachedPageContext), taxonomies)
2079
2133
  };
2080
2134
  this.adapter.page(pageProperties);
2081
- this.lastTrackedPath = url;
2082
2135
  }
2083
2136
  /**
2084
2137
  * Track an event
@@ -5525,6 +5578,8 @@ function AmplifyUpProvider({
5525
5578
  useEffect(() => {
5526
5579
  let cancelled = false;
5527
5580
  const sdk = getSDK();
5581
+ const heldRoute = isComposerPreview() || config.clientPersonalization !== false ? getActualRoute() : null;
5582
+ if (heldRoute) sdk.beginPageResolve(heldRoute);
5528
5583
  const run = async () => {
5529
5584
  try {
5530
5585
  await sdk.init({
@@ -5604,7 +5659,8 @@ function AmplifyUpProvider({
5604
5659
  result.pageConfig.pageData || null,
5605
5660
  result.pageConfig.route,
5606
5661
  extractRouteParams(result.pageConfig.route, actualRoute),
5607
- result.pageConfig.pageResources || null
5662
+ result.pageConfig.pageResources || null,
5663
+ result.pageConfig.pageTaxonomies || null
5608
5664
  );
5609
5665
  }
5610
5666
  setInitialized(true);
@@ -5628,6 +5684,7 @@ function AmplifyUpProvider({
5628
5684
  setSource(null);
5629
5685
  setMeta(null);
5630
5686
  } finally {
5687
+ if (heldRoute) sdk.endPageResolve(heldRoute);
5631
5688
  if (!cancelled) setLoading(false);
5632
5689
  }
5633
5690
  };