-
Notifications
You must be signed in to change notification settings - Fork 212
[Storefront Preview] Address base path duplication for manually entered urls #3666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
eb0445c
5c6cbf2
8bc2d6f
55779ce
506ddd8
59c2c2c
1b4f83e
791d407
f2eb4d0
a83abb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,20 +17,49 @@ type GetToken = () => string | undefined | Promise<string | undefined> | |
| type ContextChangeHandler = () => void | Promise<void> | ||
| type OptionalWhenDisabled<T> = ({enabled?: true} & T) | ({enabled: false} & Partial<T>) | ||
|
|
||
| /** | ||
| * Strip the base path from a path | ||
| * | ||
| * React Router history re-adds the base path to the path, so we | ||
| * remove it here to avoid base path duplication. | ||
| */ | ||
| function removeBasePathFromLocation<T>( | ||
| pathOrLocation: LocationDescriptor<T>, | ||
| basePath: string | ||
| ): LocationDescriptor<T> { | ||
| if (!basePath) return pathOrLocation | ||
| if (typeof pathOrLocation === 'string') { | ||
| return pathOrLocation.startsWith(basePath) | ||
| ? pathOrLocation.slice(basePath.length) || '/' | ||
|
||
| : pathOrLocation | ||
| } | ||
| const pathname = pathOrLocation.pathname ?? '/' | ||
| const strippedPathname = pathname.startsWith(basePath) | ||
| ? pathname.slice(basePath.length) || '/' | ||
|
||
| : pathname | ||
| return {...pathOrLocation, pathname: strippedPathname} | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @param enabled - flag to turn on/off Storefront Preview feature. By default, it is set to true. | ||
| * This flag only applies if storefront is running in a Runtime Admin iframe. | ||
| * @param getToken - A method that returns the access token for the current user | ||
| * @param getBasePath - A method that returns the router base path of the app. | ||
| */ | ||
| export const StorefrontPreview = ({ | ||
| children, | ||
| enabled = true, | ||
| getToken, | ||
| onContextChange | ||
| onContextChange, | ||
| getBasePath | ||
| }: React.PropsWithChildren< | ||
| // Props are only required when Storefront Preview is enabled | ||
| OptionalWhenDisabled<{getToken: GetToken; onContextChange?: ContextChangeHandler}> | ||
| OptionalWhenDisabled<{ | ||
| getToken: GetToken | ||
| onContextChange?: ContextChangeHandler | ||
| getBasePath?: () => string | ||
| }> | ||
| >) => { | ||
| const history = useHistory() | ||
| const isHostTrusted = detectStorefrontPreview() | ||
|
|
@@ -49,11 +78,13 @@ export const StorefrontPreview = ({ | |
| action: 'push' | 'replace' = 'push', | ||
| ...args: unknown[] | ||
| ) => { | ||
| history[action](path, ...args) | ||
| const basePath = getBasePath?.() ?? '' | ||
| const pathWithoutBase = removeBasePathFromLocation(path, basePath) | ||
| history[action](pathWithoutBase, ...args) | ||
| } | ||
| } | ||
| } | ||
| }, [enabled, getToken, onContextChange, siteId]) | ||
| }, [enabled, getToken, onContextChange, siteId, getBasePath]) | ||
|
|
||
| useEffect(() => { | ||
| if (enabled && isHostTrusted) { | ||
|
|
@@ -99,7 +130,8 @@ StorefrontPreview.propTypes = { | |
| // to get to a place where both these props are simply optional and we will provide default implementations. | ||
| // This would make the API simpler to use. | ||
| getToken: CustomPropTypes.requiredFunctionWhenEnabled, | ||
| onContextChange: PropTypes.func | ||
| onContextChange: PropTypes.func, | ||
| getBasePath: PropTypes.func | ||
| } | ||
|
|
||
| export default StorefrontPreview | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I noticed this TODO related to basePath:
pwa-kit/packages/commerce-sdk-react/src/components/StorefrontPreview/utils.ts
Line 29 in a56693f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching that!
I've removed that comment since we don't actually need to make any changes on that section of code. No updates are required since the client script is fetched from runtime admin and not the PWA bundle.