@salesforce/commerce-sdk-react 5.1.0 → 5.1.1

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,3 +1,7 @@
1
+ ## v5.1.1 (Mar 20, 2026)
2
+ - Update storefront preview to support base paths [#3614](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3614)
3
+ - Remove base path from /__pwa-kit route requests when showBasePath is false [#3758](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3758)
4
+
1
5
  ## v5.1.0 (Mar 12, 2026)
2
6
  - Add Page Designer Support [#3727](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3727)
3
7
  - Bump commerce-sdk-isomorphic to 5.1.0 [#3725](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3725)
@@ -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.
16
+ * Required if using a 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,69 @@ 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
+ const PWA_KIT_PATH_PREFIX = '/__pwa-kit/';
34
+
35
+ /**
36
+ * Runtime Admin always prepends envBasePath to /__pwa-kit/ paths (e.g. /test/__pwa-kit/refresh),
37
+ * but when showBasePath is false, React Router has no basename and expects /__pwa-kit/refresh.
38
+ *
39
+ * This ensures that regardless of the showBasePath setting, these paths are normalized to
40
+ * remove the base path.
41
+ */
42
+ function normalizePwaKitPath(pathOrLocation) {
43
+ if (typeof pathOrLocation === 'string') {
44
+ const idx = pathOrLocation.indexOf(PWA_KIT_PATH_PREFIX);
45
+ return idx > 0 ? pathOrLocation.slice(idx) : pathOrLocation;
46
+ }
47
+ const pathname = pathOrLocation.pathname ?? '/';
48
+ const idx = pathname.indexOf(PWA_KIT_PATH_PREFIX);
49
+ if (idx > 0) {
50
+ return _objectSpread(_objectSpread({}, pathOrLocation), {}, {
51
+ pathname: pathname.slice(idx)
52
+ });
53
+ }
54
+ return pathOrLocation;
55
+ }
56
+
57
+ /**
58
+ * Strip the base path from a path
59
+ *
60
+ * React Router history re-adds the base path to the path, so we
61
+ * remove it here to avoid base path duplication.
62
+ */
63
+ function removeBasePathFromLocation(pathOrLocation, basePath) {
64
+ if (!basePath) return pathOrLocation;
65
+ if (typeof pathOrLocation === 'string') {
66
+ return removeBasePathFromPath(pathOrLocation, basePath);
67
+ }
68
+ const pathname = pathOrLocation.pathname ?? '/';
69
+ return _objectSpread(_objectSpread({}, pathOrLocation), {}, {
70
+ pathname: removeBasePathFromPath(pathname, basePath)
71
+ });
72
+ }
73
+
25
74
  /**
26
75
  *
27
76
  * @param enabled - flag to turn on/off Storefront Preview feature. By default, it is set to true.
28
77
  * This flag only applies if storefront is running in a Runtime Admin iframe.
29
78
  * @param getToken - A method that returns the access token for the current user
79
+ * @param getBasePath - A method that returns the router base path of the app.
80
+ * Required if using a base path for router routes (showBasePath is true in url config).
30
81
  */
31
82
  const StorefrontPreview = ({
32
83
  children,
33
84
  enabled = true,
34
85
  getToken,
35
- onContextChange
86
+ onContextChange,
87
+ getBasePath
36
88
  }) => {
37
89
  const history = (0, _reactRouterDom.useHistory)();
38
90
  const isHostTrusted = (0, _utils.detectStorefrontPreview)();
@@ -47,11 +99,14 @@ const StorefrontPreview = ({
47
99
  onContextChange,
48
100
  siteId,
49
101
  experimentalUnsafeNavigate: (path, action = 'push', ...args) => {
50
- history[action](path, ...args);
102
+ const basePath = (getBasePath === null || getBasePath === void 0 ? void 0 : getBasePath()) ?? '';
103
+ const normalizedPath = normalizePwaKitPath(path);
104
+ const pathWithoutBase = removeBasePathFromLocation(normalizedPath, basePath);
105
+ history[action](pathWithoutBase, ...args);
51
106
  }
52
107
  });
53
108
  }
54
- }, [enabled, getToken, onContextChange, siteId]);
109
+ }, [enabled, getToken, onContextChange, siteId, getBasePath]);
55
110
  (0, _react.useEffect)(() => {
56
111
  if (enabled && isHostTrusted) {
57
112
  // In Storefront Preview mode, add cache breaker for all SCAPI's requests.
@@ -85,6 +140,7 @@ StorefrontPreview.propTypes = {
85
140
  // to get to a place where both these props are simply optional and we will provide default implementations.
86
141
  // This would make the API simpler to use.
87
142
  getToken: _utils.CustomPropTypes.requiredFunctionWhenEnabled,
88
- onContextChange: _propTypes.default.func
143
+ onContextChange: _propTypes.default.func,
144
+ getBasePath: _propTypes.default.func
89
145
  };
90
146
  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",
3
+ "version": "5.1.1",
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",
49
+ "@salesforce/pwa-kit-dev": "3.17.1",
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",
64
+ "internal-lib-build": "3.17.1",
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": "6d0cfd2308b9f25d8badd722469074889bd3b891"
100
+ "gitHead": "d09ed57f84432913ab3e55e3073802d319c5dc3c"
101
101
  }