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