Skip to content

Commit 1b4f83e

Browse files
committed
Apply suggestions
1 parent 59c2c2c commit 1b4f83e

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

packages/commerce-sdk-react/src/components/StorefrontPreview/storefront-preview.test.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,54 @@ describe('Storefront Preview Component', function () {
165165
expect(mockPush).toHaveBeenCalledWith('/other/product/123')
166166
})
167167

168+
test('experimentalUnsafeNavigate does not strip when path has basePath only as substring (e.g. /shop vs /shopping/cart)', () => {
169+
;(detectStorefrontPreview as jest.Mock).mockReturnValue(true)
170+
171+
render(
172+
<StorefrontPreview
173+
enabled={true}
174+
getToken={() => 'my-token'}
175+
getBasePath={() => '/shop'}
176+
/>
177+
)
178+
179+
window.STOREFRONT_PREVIEW?.experimentalUnsafeNavigate?.('/shopping/cart', 'push')
180+
expect(mockPush).toHaveBeenCalledWith('/shopping/cart')
181+
})
182+
183+
test('experimentalUnsafeNavigate strips to / when path exactly equals basePath', () => {
184+
;(detectStorefrontPreview as jest.Mock).mockReturnValue(true)
185+
186+
render(
187+
<StorefrontPreview
188+
enabled={true}
189+
getToken={() => 'my-token'}
190+
getBasePath={() => '/mybase'}
191+
/>
192+
)
193+
194+
window.STOREFRONT_PREVIEW?.experimentalUnsafeNavigate?.('/mybase', 'push')
195+
expect(mockPush).toHaveBeenCalledWith('/')
196+
})
197+
198+
test('experimentalUnsafeNavigate removes base path from location object when getBasePath is provided', () => {
199+
;(detectStorefrontPreview as jest.Mock).mockReturnValue(true)
200+
201+
render(
202+
<StorefrontPreview
203+
enabled={true}
204+
getToken={() => 'my-token'}
205+
getBasePath={() => '/mybase'}
206+
/>
207+
)
208+
209+
window.STOREFRONT_PREVIEW?.experimentalUnsafeNavigate?.(
210+
{pathname: '/mybase/product/123', search: '?q=1'},
211+
'push'
212+
)
213+
expect(mockPush).toHaveBeenCalledWith({pathname: '/product/123', search: '?q=1'})
214+
})
215+
168216
test('cache breaker is added to the parameters of SCAPI requests, only if in storefront preview', () => {
169217
;(detectStorefrontPreview as jest.Mock).mockReturnValue(true)
170218
mockQueryEndpoint('baskets/123', {})

packages/commerce-sdk-react/src/components/StorefrontPreview/storefront-preview.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ type GetToken = () => string | undefined | Promise<string | undefined>
1717
type ContextChangeHandler = () => void | Promise<void>
1818
type OptionalWhenDisabled<T> = ({enabled?: true} & T) | ({enabled: false} & Partial<T>)
1919

20+
/**
21+
* Remove the base path from a path string.
22+
* Only strips when path equals basePath or path starts with basePath + '/'.
23+
*/
24+
function removeBasePathFromPath(path: string, basePath: string): string {
25+
const matches =
26+
path.startsWith(basePath + '/') || path === basePath
27+
return matches ? path.slice(basePath.length) || '/' : path
28+
}
29+
2030
/**
2131
* Strip the base path from a path
22-
*
32+
*
2333
* React Router history re-adds the base path to the path, so we
2434
* remove it here to avoid base path duplication.
2535
*/
@@ -29,15 +39,13 @@ function removeBasePathFromLocation<T>(
2939
): LocationDescriptor<T> {
3040
if (!basePath) return pathOrLocation
3141
if (typeof pathOrLocation === 'string') {
32-
return pathOrLocation.startsWith(basePath)
33-
? pathOrLocation.slice(basePath.length) || '/'
34-
: pathOrLocation
42+
return removeBasePathFromPath(pathOrLocation, basePath) as LocationDescriptor<T>
3543
}
3644
const pathname = pathOrLocation.pathname ?? '/'
37-
const strippedPathname = pathname.startsWith(basePath)
38-
? pathname.slice(basePath.length) || '/'
39-
: pathname
40-
return {...pathOrLocation, pathname: strippedPathname}
45+
return {
46+
...pathOrLocation,
47+
pathname: removeBasePathFromPath(pathname, basePath)
48+
}
4149
}
4250

4351
/**

packages/commerce-sdk-react/src/components/StorefrontPreview/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const detectStorefrontPreview = () => {
2626
export const getClientScript = () => {
2727
const parentOrigin = getParentOrigin() ?? 'https://runtime.commercecloud.com'
2828
return parentOrigin === DEVELOPMENT_ORIGIN
29-
// TODO: This will need to be updated to support base paths with storefront preview
3029
? `${parentOrigin}${LOCAL_BUNDLE_PATH}/static/storefront-preview.js`
3130
: `${parentOrigin}/cc/b2c/preview/preview.client.js`
3231
}

0 commit comments

Comments
 (0)