-
Notifications
You must be signed in to change notification settings - Fork 212
@W-20784635@ Support adding base paths to shopper facing URLs #3615
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 10 commits
523c429
5ac1003
acb49ce
c0b691d
e760832
f8c2a30
9a63eb8
244010e
941873e
8503a08
470aabb
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| */ | ||
| import {proxyConfigs} from '@salesforce/pwa-kit-runtime/utils/ssr-shared' | ||
| import {getEnvBasePath, bundleBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths' | ||
| import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config' | ||
|
|
||
| const onClient = typeof window !== 'undefined' | ||
|
|
||
|
|
@@ -53,3 +54,21 @@ export const getProxyConfigs = () => { | |
| // Clone to avoid accidental mutation of important configuration variables. | ||
| return configs.map((config) => ({...config})) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the base path (defined in config.ssrParameters.envBasePath) | ||
| * for React Router routes when showBasePath is enabled in the app config. | ||
| * | ||
| * This function should be used when working with a React Router route | ||
| * (The route is defined in routes.jsx). | ||
| * | ||
| * Use getEnvBasePath (pwa-kit-runtime) if you are working with an express route\ | ||
| * (The route is defined in ssr.js). | ||
| * | ||
| * @returns {string} - The base path if showBasePath is true, otherwise an empty string | ||
| */ | ||
| export const getRouterBasePath = () => { | ||
| const config = getConfig() | ||
| const showBasePath = config?.app?.url?.showBasePath === true | ||
| return showBasePath ? getEnvBasePath() : '' | ||
|
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 utility function exists to support the feature toggle since calling I opted to separate this from getEnvBasePath to support cases where a project wants the base path only on express routes. For example, suppose a project wants to have www.example.com/emea/mobify/proxy/... (envBasePath: /emea) along with shopper facing urls like www.example.com/gb/category/.. or www.example.com/fr/product/.. where Note that to support the above scenario, projects would need a CDN to route requests with |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ import React, {useState, useEffect, useMemo} from 'react' | |
| import PropTypes from 'prop-types' | ||
| import {useHistory, useLocation} from 'react-router-dom' | ||
| import {StorefrontPreview} from '@salesforce/commerce-sdk-react/components' | ||
| import {getAssetUrl} from '@salesforce/pwa-kit-react-sdk/ssr/universal/utils' | ||
| import {getAssetUrl, getRouterBasePath} from '@salesforce/pwa-kit-react-sdk/ssr/universal/utils' | ||
| import useActiveData from '@salesforce/retail-react-app/app/hooks/use-active-data' | ||
| import {useQuery} from '@tanstack/react-query' | ||
| import { | ||
|
|
@@ -340,25 +340,33 @@ const App = (props) => { | |
| <link | ||
| rel="alternate" | ||
| hrefLang={locale.id.toLowerCase()} | ||
| href={`${appOrigin}${getPathWithLocale(locale.id, buildUrl, { | ||
| location: { | ||
| ...location, | ||
| search: '' | ||
| href={`${appOrigin}${getRouterBasePath()}${getPathWithLocale( | ||
|
||
| locale.id, | ||
| buildUrl, | ||
| { | ||
| location: { | ||
| ...location, | ||
| search: '' | ||
| } | ||
| } | ||
| })}`} | ||
| )}`} | ||
| key={locale.id} | ||
| /> | ||
| ))} | ||
| {/* A general locale as fallback. For example: "en" if default locale is "en-GB" */} | ||
| <link | ||
| rel="alternate" | ||
| hrefLang={site.l10n.defaultLocale.slice(0, 2)} | ||
| href={`${appOrigin}${getPathWithLocale(locale.id, buildUrl, { | ||
| location: { | ||
| ...location, | ||
| search: '' | ||
| href={`${appOrigin}${getRouterBasePath()}${getPathWithLocale( | ||
| locale.id, | ||
| buildUrl, | ||
| { | ||
| location: { | ||
| ...location, | ||
| search: '' | ||
| } | ||
| } | ||
| })}`} | ||
| )}`} | ||
| /> | ||
| {/* A wider fallback for user locales that the app does not support */} | ||
| <link rel="alternate" hrefLang="x-default" href={`${appOrigin}/`} /> | ||
|
|
||
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.
Adding the
basenameprop here and in main.jsx automatically adds the base path to almost all URLs.This includes links that use the
Linkcomponent and places where we usehistoryto navigate.This
basenameprop does not apply to places where we updatewindow.locationdirectly, hence other changes in this PR.