@salesforce/commerce-sdk-react 5.1.0-preview.2 → 5.1.1-preview.0

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/CHANGELOG.md CHANGED
@@ -1,4 +1,7 @@
1
- ## v5.1.0-preview.2 (Mar 12, 2026)
1
+ ## v5.1.1-preview.0 (Mar 13, 2026)
2
+ - Update storefront preview to support base paths [#3614](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3614)
3
+
4
+ ## v5.1.0 (Mar 12, 2026)
2
5
  - Add Page Designer Support [#3727](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3727)
3
6
  - Bump commerce-sdk-isomorphic to 5.1.0 [#3725](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3725)
4
7
  - Update ShopperBasketsV2 hooks documentation and query keys [#3728](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3728)
@@ -12,17 +12,21 @@ type OptionalWhenDisabled<T> = ({
12
12
  * @param enabled - flag to turn on/off Storefront Preview feature. By default, it is set to true.
13
13
  * This flag only applies if storefront is running in a Runtime Admin iframe.
14
14
  * @param getToken - A method that returns the access token for the current user
15
+ * @param getBasePath - A method that returns the router base path of the app. Requird if using
16
+ * base path for router routes (showBasePath is true in url config).
15
17
  */
16
18
  export declare const StorefrontPreview: {
17
- ({ children, enabled, getToken, onContextChange }: React.PropsWithChildren<OptionalWhenDisabled<{
19
+ ({ children, enabled, getToken, onContextChange, getBasePath }: React.PropsWithChildren<OptionalWhenDisabled<{
18
20
  getToken: GetToken;
19
21
  onContextChange?: ContextChangeHandler | undefined;
22
+ getBasePath?: (() => string) | undefined;
20
23
  }>>): import("react/jsx-runtime").JSX.Element;
21
24
  propTypes: {
22
25
  children: PropTypes.Requireable<PropTypes.ReactNodeLike>;
23
26
  enabled: PropTypes.Requireable<boolean>;
24
27
  getToken: (props: any, propName: any, componentName: any) => Error | undefined;
25
28
  onContextChange: PropTypes.Requireable<(...args: any[]) => any>;
29
+ getBasePath: PropTypes.Requireable<(...args: any[]) => any>;
26
30
  };
27
31
  };
28
32
  export default StorefrontPreview;
@@ -22,17 +22,46 @@ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e =
22
22
  * SPDX-License-Identifier: BSD-3-Clause
23
23
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
24
24
  */
25
+ /**
26
+ * Remove the base path from a path string.
27
+ * Only strips when path equals basePath or path starts with basePath + '/'.
28
+ */
29
+ function removeBasePathFromPath(path, basePath) {
30
+ const matches = path.startsWith(basePath + '/') || path === basePath;
31
+ return matches ? path.slice(basePath.length) || '/' : path;
32
+ }
33
+
34
+ /**
35
+ * Strip the base path from a path
36
+ *
37
+ * React Router history re-adds the base path to the path, so we
38
+ * remove it here to avoid base path duplication.
39
+ */
40
+ function removeBasePathFromLocation(pathOrLocation, basePath) {
41
+ if (!basePath) return pathOrLocation;
42
+ if (typeof pathOrLocation === 'string') {
43
+ return removeBasePathFromPath(pathOrLocation, basePath);
44
+ }
45
+ const pathname = pathOrLocation.pathname ?? '/';
46
+ return _objectSpread(_objectSpread({}, pathOrLocation), {}, {
47
+ pathname: removeBasePathFromPath(pathname, basePath)
48
+ });
49
+ }
50
+
25
51
  /**
26
52
  *
27
53
  * @param enabled - flag to turn on/off Storefront Preview feature. By default, it is set to true.
28
54
  * This flag only applies if storefront is running in a Runtime Admin iframe.
29
55
  * @param getToken - A method that returns the access token for the current user
56
+ * @param getBasePath - A method that returns the router base path of the app. Requird if using
57
+ * base path for router routes (showBasePath is true in url config).
30
58
  */
31
59
  const StorefrontPreview = ({
32
60
  children,
33
61
  enabled = true,
34
62
  getToken,
35
- onContextChange
63
+ onContextChange,
64
+ getBasePath
36
65
  }) => {
37
66
  const history = (0, _reactRouterDom.useHistory)();
38
67
  const isHostTrusted = (0, _utils.detectStorefrontPreview)();
@@ -47,11 +76,13 @@ const StorefrontPreview = ({
47
76
  onContextChange,
48
77
  siteId,
49
78
  experimentalUnsafeNavigate: (path, action = 'push', ...args) => {
50
- history[action](path, ...args);
79
+ const basePath = (getBasePath === null || getBasePath === void 0 ? void 0 : getBasePath()) ?? '';
80
+ const pathWithoutBase = removeBasePathFromLocation(path, basePath);
81
+ history[action](pathWithoutBase, ...args);
51
82
  }
52
83
  });
53
84
  }
54
- }, [enabled, getToken, onContextChange, siteId]);
85
+ }, [enabled, getToken, onContextChange, siteId, getBasePath]);
55
86
  (0, _react.useEffect)(() => {
56
87
  if (enabled && isHostTrusted) {
57
88
  // In Storefront Preview mode, add cache breaker for all SCAPI's requests.
@@ -85,6 +116,7 @@ StorefrontPreview.propTypes = {
85
116
  // to get to a place where both these props are simply optional and we will provide default implementations.
86
117
  // This would make the API simpler to use.
87
118
  getToken: _utils.CustomPropTypes.requiredFunctionWhenEnabled,
88
- onContextChange: _propTypes.default.func
119
+ onContextChange: _propTypes.default.func,
120
+ getBasePath: _propTypes.default.func
89
121
  };
90
122
  var _default = exports.default = StorefrontPreview;
@@ -30,9 +30,7 @@ const detectStorefrontPreview = () => {
30
30
  exports.detectStorefrontPreview = detectStorefrontPreview;
31
31
  const getClientScript = () => {
32
32
  const parentOrigin = (0, _utils.getParentOrigin)() ?? 'https://runtime.commercecloud.com';
33
- return parentOrigin === _utils.DEVELOPMENT_ORIGIN
34
- // TODO: This will need to be updated to support base paths with storefront preview
35
- ? `${parentOrigin}${LOCAL_BUNDLE_PATH}/static/storefront-preview.js` : `${parentOrigin}/cc/b2c/preview/preview.client.js`;
33
+ return parentOrigin === _utils.DEVELOPMENT_ORIGIN ? `${parentOrigin}${LOCAL_BUNDLE_PATH}/static/storefront-preview.js` : `${parentOrigin}/cc/b2c/preview/preview.client.js`;
36
34
  };
37
35
 
38
36
  // Custom Prop Types.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/commerce-sdk-react",
3
- "version": "5.1.0-preview.2",
3
+ "version": "5.1.1-preview.0",
4
4
  "description": "A library that provides react hooks for fetching data from Commerce Cloud",
5
5
  "homepage": "https://github.com/SalesforceCommerceCloud/pwa-kit/tree/develop/packages/ecom-react-hooks#readme",
6
6
  "bugs": {
@@ -46,7 +46,7 @@
46
46
  "jwt-decode": "^4.0.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@salesforce/pwa-kit-dev": "3.17.0-preview.2",
49
+ "@salesforce/pwa-kit-dev": "3.17.1-preview.0",
50
50
  "@tanstack/react-query": "^4.28.0",
51
51
  "@testing-library/jest-dom": "^5.16.5",
52
52
  "@testing-library/react": "^14.0.0",
@@ -61,7 +61,7 @@
61
61
  "@types/react-helmet": "~6.1.6",
62
62
  "@types/react-router-dom": "~5.3.3",
63
63
  "cross-env": "^5.2.1",
64
- "internal-lib-build": "3.17.0-preview.2",
64
+ "internal-lib-build": "3.17.1-preview.0",
65
65
  "jsonwebtoken": "^9.0.0",
66
66
  "nock": "^13.3.0",
67
67
  "nodemon": "^2.0.22",
@@ -97,5 +97,5 @@
97
97
  "publishConfig": {
98
98
  "directory": "dist"
99
99
  },
100
- "gitHead": "1da061ff7778db2ff9737838b71c2d09975382b1"
100
+ "gitHead": "beabe8a971b6170bddfe08ae3f5ca2f1bfe66aa7"
101
101
  }