-
Notifications
You must be signed in to change notification settings - Fork 212
@W-20784635@ Productize base paths #3614
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
base: develop
Are you sure you want to change the base?
Changes from 10 commits
2c250bd
c493496
23bfeba
68f49a7
35a39f5
8f3c2ba
975fbd9
bb8586b
21cbe2b
3e522fb
821cf33
a121a7b
73b4bac
7e7a5ea
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,57 @@ type GetToken = () => string | undefined | Promise<string | undefined> | |
| type ContextChangeHandler = () => void | Promise<void> | ||
| type OptionalWhenDisabled<T> = ({enabled?: true} & T) | ({enabled: false} & Partial<T>) | ||
|
|
||
| /** | ||
| * Remove the base path from a path string. | ||
| * Only strips when path equals basePath or path starts with basePath + '/'. | ||
| */ | ||
| function removeBasePathFromPath(path: string, basePath: string): string { | ||
| const matches = | ||
| path.startsWith(basePath + '/') || path === basePath | ||
| return matches ? path.slice(basePath.length) || '/' : path | ||
| } | ||
|
|
||
| /** | ||
| * 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 removeBasePathFromPath(pathOrLocation, basePath) as LocationDescriptor<T> | ||
| } | ||
| const pathname = pathOrLocation.pathname ?? '/' | ||
| return { | ||
| ...pathOrLocation, | ||
| pathname: removeBasePathFromPath(pathname, basePath) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @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() | ||
|
|
@@ -39,6 +76,13 @@ export const StorefrontPreview = ({ | |
|
|
||
| useEffect(() => { | ||
| if (enabled && isHostTrusted) { | ||
| if (process.env.NODE_ENV !== 'production' && !getBasePath) { | ||
| console.warn( | ||
| '[StorefrontPreview] No getBasePath prop provided. ' + | ||
| 'If your app uses a base path for router routes (showBasePath is true in url config), ' + | ||
| 'pass getBasePath to avoid base path duplication during navigation.' | ||
| ) | ||
| } | ||
| window.STOREFRONT_PREVIEW = { | ||
| ...window.STOREFRONT_PREVIEW, | ||
| getToken, | ||
|
|
@@ -49,11 +93,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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to exclude basePath in this navigate? (I probably missed the context of this)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change was a part of #3666 . The history API is a part of react router so when it is invoked with a path, react router will automatically append the base path (set as the If we don't remove the base path, then react router will add a duplicate base path. It's the same issue that we fixed in sf-next in this PR: https://github.com/commerce-emu/storefront-next/pull/1050 |
||
| } | ||
| } | ||
| } | ||
| }, [enabled, getToken, onContextChange, siteId]) | ||
| }, [enabled, getToken, onContextChange, siteId, getBasePath]) | ||
|
|
||
| useEffect(() => { | ||
| if (enabled && isHostTrusted) { | ||
|
|
@@ -99,7 +145,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.
nit: Add a guard to check if basePath is enabled
showBasePathso we avoid printing the warning on all sites that don't use basePath