-
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 all 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) { | ||
|
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. nit: Add a guard to check if basePath is enabled |
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| ## v3.18.0-dev | ||
| - Add base path prefix to support multiple MRT environments under 1 domain [#3614](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3614) | ||
|
|
||
| ## v3.17.0-dev | ||
|
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 have two -dev changelog here?
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. I'm getting a bit ahead of the versioning since 3.17.0 is now in preview. This change isn't making the 3.17 release so it's under a new version. |
||
| - Add Salesforce Payments configuration to generated projects [#3725] (https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3725) | ||
| - Clear verdaccio npm cache during project generation [#3652](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3652) | ||
|
|
||
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.
Two -dev versions? Does not seem right
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.
This was because 5.1.0-dev corresponds with 3.17.0. Since this is not aimed at the 3.17 release, it's a separate version.