@amplifyup/sdk 0.1.73 → 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.
@@ -806,6 +806,7 @@ function applyPageContext(input, opts) {
806
806
  }
807
807
  const pageFields = {};
808
808
  for (const pf of opts.pageData?.pageFields || []) {
809
+ if (pf.dataType === "taxonomy") continue;
809
810
  if (pf.source === "static") {
810
811
  if (pf.value !== void 0) pageFields[pf.key] = pf.value;
811
812
  continue;
@@ -828,6 +829,7 @@ function applyPageContext(input, opts) {
828
829
  function staticPageFieldsFromSnapshot(pageData) {
829
830
  const pageFields = {};
830
831
  for (const pf of pageData?.pageFields || []) {
832
+ if (pf.dataType === "taxonomy") continue;
831
833
  if (pf.source === "static" && pf.value !== void 0) {
832
834
  pageFields[pf.key] = pf.value;
833
835
  }
@@ -845,13 +847,19 @@ function attachStaticPageContext(pageData, pageRoute) {
845
847
  pageRoute: pageRoute || ""
846
848
  };
847
849
  }
850
+ function withPageTaxonomies(properties, taxonomies) {
851
+ if (!taxonomies || Object.keys(taxonomies).length === 0) return properties;
852
+ const page = properties.page && typeof properties.page === "object" && !Array.isArray(properties.page) ? properties.page : { fields: {} };
853
+ return { ...properties, page: { ...page, taxonomies } };
854
+ }
848
855
  function mergeEventContext(properties, attached) {
849
856
  if (!attached) return properties;
850
857
  return {
851
858
  ...properties,
852
859
  ...attached.resource ? { resource: attached.resource } : {},
853
860
  page: attached.page,
854
- ...attached.pageRoute ? { pageRoute: attached.pageRoute } : {}
861
+ ...attached.pageRoute ? { pageRoute: attached.pageRoute } : {},
862
+ ...attached.pageResource ? { pageResource: attached.pageResource } : {}
855
863
  };
856
864
  }
857
865
  var PAGE_RESOURCE_MARKER = "__amplifyupPageResource";
@@ -913,6 +921,27 @@ function hydratePageResourceProps(props, fields) {
913
921
  return out;
914
922
  }
915
923
 
924
+ // src/edge/normalizeRoute.ts
925
+ function normalizeRoute(route) {
926
+ let path = String(route || "").trim();
927
+ if (!path) return "/";
928
+ try {
929
+ if (/^https?:\/\//i.test(path)) {
930
+ path = new URL(path).pathname;
931
+ }
932
+ } catch {
933
+ }
934
+ const q = path.indexOf("?");
935
+ if (q >= 0) path = path.slice(0, q);
936
+ const h = path.indexOf("#");
937
+ if (h >= 0) path = path.slice(0, h);
938
+ if (!path.startsWith("/")) path = `/${path}`;
939
+ if (path.length > 1 && path.endsWith("/")) {
940
+ path = path.replace(/\/+$/, "");
941
+ }
942
+ return path || "/";
943
+ }
944
+
916
945
  // src/page-resource-runtime.ts
917
946
  var pageResourceFields = null;
918
947
  function setPageResourceFields(fields) {
@@ -1006,6 +1035,7 @@ function dedupeLayoutProviderFetch(key, fetchFn) {
1006
1035
  }
1007
1036
 
1008
1037
  // src/index.ts
1038
+ var PAGE_VIEW_HOLD_MS = 1500;
1009
1039
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
1010
1040
  var DWELL_POLL_MS = 5e3;
1011
1041
  function readPreviewQueryOverrides() {
@@ -1076,8 +1106,15 @@ var AmplifyUpSDK = class {
1076
1106
  this.pendingPageContext = null;
1077
1107
  this.attachedPageContext = null;
1078
1108
  this.resolvedPageData = null;
1109
+ this.resolvedPageResource = null;
1079
1110
  this.resolvedPageRoute = "";
1080
1111
  this.resolvedRouteParams = {};
1112
+ /** Path whose delivery response is in flight; page_view for it is held. */
1113
+ this.pendingResolvePath = null;
1114
+ this.heldPageView = null;
1115
+ this.resolvedPageTaxonomies = null;
1116
+ /** Path the taxonomies above were resolved for. */
1117
+ this.resolvedTaxonomiesPath = null;
1081
1118
  this.historyHooksInstalled = false;
1082
1119
  this.pageEngagementState = {
1083
1120
  reached: /* @__PURE__ */ new Set(),
@@ -1608,31 +1645,65 @@ var AmplifyUpSDK = class {
1608
1645
  * Apply published pageData (from /v1/resolve) so field validation and
1609
1646
  * mapped page fields can run.
1610
1647
  */
1611
- applyResolvedPageData(pageData, pageRoute, routeParams) {
1648
+ applyResolvedPageData(pageData, pageRoute, routeParams, pageResources, pageTaxonomies) {
1612
1649
  this.resolvedPageData = pageData || null;
1650
+ this.resolvedPageTaxonomies = pageTaxonomies || null;
1651
+ this.resolvedTaxonomiesPath = this.pendingResolvePath;
1613
1652
  if (pageRoute) this.resolvedPageRoute = pageRoute;
1614
1653
  if (routeParams) this.resolvedRouteParams = routeParams;
1654
+ const firstResourceName = pageData?.resources?.[0]?.name;
1655
+ const resolvedForPage = (firstResourceName ? pageResources?.[firstResourceName] : void 0) || (pageResources ? Object.values(pageResources)[0] : void 0);
1656
+ this.resolvedPageResource = resolvedForPage || null;
1615
1657
  this.recomputeAttachedContext();
1616
1658
  }
1617
1659
  recomputeAttachedContext() {
1618
1660
  if (!this.pendingPageContext) {
1619
- this.attachedPageContext = attachStaticPageContext(
1620
- this.resolvedPageData,
1621
- this.resolvedPageRoute
1661
+ this.attachedPageContext = this.withPageResource(
1662
+ attachStaticPageContext(this.resolvedPageData, this.resolvedPageRoute)
1622
1663
  );
1623
1664
  setPageResourceFields(null);
1624
1665
  return;
1625
1666
  }
1626
- this.attachedPageContext = applyPageContext(this.pendingPageContext, {
1627
- pageData: this.resolvedPageData,
1628
- routeParams: this.resolvedRouteParams,
1629
- pageRoute: this.resolvedPageRoute
1630
- });
1667
+ this.attachedPageContext = this.withPageResource(
1668
+ applyPageContext(this.pendingPageContext, {
1669
+ pageData: this.resolvedPageData,
1670
+ routeParams: this.resolvedRouteParams,
1671
+ pageRoute: this.resolvedPageRoute
1672
+ })
1673
+ );
1631
1674
  setPageResourceFields(this.attachedPageContext?.resource?.fields || null);
1632
1675
  }
1676
+ /** Stamp the resolved entry onto the context so events carry it. */
1677
+ withPageResource(context) {
1678
+ if (!context || !this.resolvedPageResource) return context;
1679
+ const { entity, providerId, docId } = this.resolvedPageResource;
1680
+ return { ...context, pageResource: { entity, providerId, docId } };
1681
+ }
1633
1682
  getAttachedPageContext() {
1634
1683
  return this.attachedPageContext;
1635
1684
  }
1685
+ /**
1686
+ * The page's delivery response for `path` is on its way. A page_view for the
1687
+ * same path waits for it (up to PAGE_VIEW_HOLD_MS) so it carries the page's
1688
+ * taxonomies.
1689
+ */
1690
+ beginPageResolve(path) {
1691
+ this.pendingResolvePath = normalizeRoute(path);
1692
+ this.resolvedPageTaxonomies = null;
1693
+ this.resolvedTaxonomiesPath = null;
1694
+ }
1695
+ endPageResolve(path) {
1696
+ if (this.pendingResolvePath !== normalizeRoute(path)) return;
1697
+ this.pendingResolvePath = null;
1698
+ this.flushHeldPageView();
1699
+ }
1700
+ flushHeldPageView() {
1701
+ const held = this.heldPageView;
1702
+ if (!held) return;
1703
+ clearTimeout(held.timer);
1704
+ this.heldPageView = null;
1705
+ this.sendPageView(held.url, held.properties);
1706
+ }
1636
1707
  /**
1637
1708
  * Track a page view
1638
1709
  */
@@ -1643,6 +1714,21 @@ var AmplifyUpSDK = class {
1643
1714
  if (!this.isInitialized()) {
1644
1715
  return;
1645
1716
  }
1717
+ this.lastTrackedPath = url;
1718
+ if (this.pendingResolvePath && this.pendingResolvePath === normalizeRoute(url)) {
1719
+ this.flushHeldPageView();
1720
+ this.heldPageView = {
1721
+ url,
1722
+ properties,
1723
+ timer: setTimeout(() => this.flushHeldPageView(), PAGE_VIEW_HOLD_MS)
1724
+ };
1725
+ return;
1726
+ }
1727
+ this.flushHeldPageView();
1728
+ this.sendPageView(url, properties);
1729
+ }
1730
+ sendPageView(url, properties) {
1731
+ const taxonomies = this.resolvedTaxonomiesPath && this.resolvedTaxonomiesPath === normalizeRoute(url) ? this.resolvedPageTaxonomies : null;
1646
1732
  const instanceId = getInstanceId();
1647
1733
  const sessionId = getSessionId();
1648
1734
  const pageProperties = {
@@ -1654,10 +1740,9 @@ var AmplifyUpSDK = class {
1654
1740
  sessionId,
1655
1741
  trackingId: this.config?.trackingId,
1656
1742
  // Timestamp is included in event payload
1657
- ...mergeEventContext(properties || {}, this.attachedPageContext)
1743
+ ...withPageTaxonomies(mergeEventContext(properties || {}, this.attachedPageContext), taxonomies)
1658
1744
  };
1659
1745
  this.adapter.page(pageProperties);
1660
- this.lastTrackedPath = url;
1661
1746
  }
1662
1747
  /**
1663
1748
  * Track an event