Skip to content

Commit acb49ce

Browse files
committed
Add a feature toggle for app base names
1 parent 5ac1003 commit acb49ce

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

packages/pwa-kit-react-sdk/src/ssr/browser/main.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {uuidv4} from '../../utils/uuidv4.client'
1818
import PropTypes from 'prop-types'
1919
import logger from '../../utils/logger-instance'
2020
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
21+
import {getBasename} from '../universal/utils'
2122

2223
/* istanbul ignore next */
2324
export const registerServiceWorker = (url) => {
@@ -45,11 +46,11 @@ export const registerServiceWorker = (url) => {
4546
export const OuterApp = ({routes, error, WrappedApp, locals, onHydrate}) => {
4647
const AppConfig = getAppConfig()
4748
const isInitialPageRef = useRef(true)
48-
const basepath = getEnvBasePath()
49+
const routerBasename = getBasename() || undefined
4950

5051
return (
5152
<ServerContext.Provider value={{}}>
52-
<Router ref={onHydrate} basename={basepath}>
53+
<Router ref={onHydrate} basename={routerBasename}>
5354
<CorrelationIdProvider
5455
correlationId={() => {
5556
// If we are hydrating an error page use the server correlation id.

packages/pwa-kit-react-sdk/src/ssr/server/react-rendering.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import PerformanceTimer from '../../utils/performance'
4141
import {PERFORMANCE_MARKS} from '../../utils/performance-marks'
4242

4343
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
44+
import {getBasename} from '../universal/utils'
4445

4546
const CWD = process.cwd()
4647
const BUNDLES_PATH = path.resolve(CWD, 'build/loadable-stats.json')
@@ -272,10 +273,11 @@ export const render = (req, res, next) => {
272273

273274
const OuterApp = ({req, res, error, App, appState, routes, routerContext, location}) => {
274275
const AppConfig = getAppConfig()
275-
const basepath = getEnvBasePath()
276+
const routerBasename = getBasename() || undefined
277+
276278
return (
277279
<ServerContext.Provider value={{req, res}}>
278-
<Router location={location} context={routerContext} basename={basepath}>
280+
<Router location={location} context={routerContext} basename={routerBasename}>
279281
<CorrelationIdProvider
280282
correlationId={res.locals.requestId}
281283
resetOnPageChange={false}

packages/pwa-kit-react-sdk/src/ssr/universal/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
import {proxyConfigs} from '@salesforce/pwa-kit-runtime/utils/ssr-shared'
1111
import {getEnvBasePath, bundleBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
12+
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
1213

1314
const onClient = typeof window !== 'undefined'
1415

@@ -53,3 +54,16 @@ export const getProxyConfigs = () => {
5354
// Clone to avoid accidental mutation of important configuration variables.
5455
return configs.map((config) => ({...config}))
5556
}
57+
58+
/**
59+
* Returns the basename (envBasePath) if showBasename is enabled in the app config.
60+
* This is used for URLs that bypass React Router (e.g., window.location, href attributes).
61+
* React Router handles basename via its basename prop when showBasename is enabled.
62+
*
63+
* @returns {string} - The basename if showBasename is true, otherwise an empty string
64+
*/
65+
export const getBasename = () => {
66+
const config = getConfig()
67+
const showBasename = config?.app?.url?.showBasename !== false
68+
return showBasename ? getEnvBasePath() : ''
69+
}

packages/template-retail-react-app/app/components/_error/index.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '@salesforce/retail-react-app/app/components/shared/ui'
1919

2020
import {BrandLogo, FileIcon} from '@salesforce/retail-react-app/app/components/icons'
21-
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
21+
import {getBasename} from '@salesforce/pwa-kit-react-sdk/ssr/universal/utils'
2222

2323
// <Error> is rendered when:
2424
//
@@ -55,9 +55,10 @@ const Error = (props) => {
5555
// as the application is in an error state. We need to force a
5656
// hard navigation to get back to the normal state.
5757
// Include basename since this bypasses React Router
58+
// Respect showBasename config setting
5859
onClick={() => {
59-
const envBasePath = getEnvBasePath()
60-
window.location.href = envBasePath ? `${envBasePath}/` : '/'
60+
const basename = getBasename()
61+
window.location.href = basename ? `${basename}/` : '/'
6162
}}
6263
/>
6364
</Box>

packages/template-retail-react-app/app/utils/site-utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
9-
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
9+
import {getBasename} from '@salesforce/pwa-kit-react-sdk/ssr/universal/utils'
1010

1111
/**
1212
* This functions takes an url and returns a site object,
@@ -104,11 +104,12 @@ export const getSiteByReference = (siteRef) => {
104104
export const getParamsFromPath = (path) => {
105105
let {pathname, search} = getPathnameAndSearch(path)
106106

107-
// Remove the envBasePath from the pathname if present since
107+
// Remove the basename from the pathname if present since
108108
// it shifts the location of the site and locale in the pathname
109-
const envBasePath = getEnvBasePath()
110-
if (pathname.startsWith(envBasePath)) {
111-
pathname = pathname.substring(envBasePath.length)
109+
// Respect showBasename config setting
110+
const basename = getBasename()
111+
if (basename && pathname.startsWith(basename)) {
112+
pathname = pathname.substring(basename.length)
112113
}
113114

114115
const config = getConfig()

packages/template-retail-react-app/app/utils/url.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@salesforce/retail-react-app/app/utils/site-utils'
1515
import {HOME_HREF, urlPartPositions} from '@salesforce/retail-react-app/app/constants'
1616
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
17+
import {getBasename} from '@salesforce/pwa-kit-react-sdk/ssr/universal/utils'
1718

1819
/**
1920
* Constructs an absolute URL from a given path and an optional application origin.
@@ -128,7 +129,7 @@ export const searchUrlBuilder = (searchTerm) => '/search?q=' + encodeURIComponen
128129
* window.location to force a hard refresh of the page.
129130
* Because this bypasses React Router, we need to manually add the basename to the url, if one is
130131
* defined in the app config.
131-
*
132+
*
132133
* @param {String} shortCode - The locale short code.
133134
* @param {function(*, *, *, *=): string} - Generates a site URL from the provided path, site and locale.
134135
* @param {string[]} opts.disallowParams - URL parameters to remove
@@ -184,10 +185,12 @@ export const getPathWithLocale = (shortCode, buildUrl, opts = {}) => {
184185
site.alias || site.id,
185186
locale?.alias || locale?.id
186187
)
187-
188+
188189
// Add basename for locale selection URLs since they bypass React Router
189190
// (React Router handles basename for navigation via basename prop)
190-
return envBasePath ? `${envBasePath}${newUrl}` : newUrl
191+
// Respect showBasename config setting
192+
const basename = getBasename()
193+
return basename ? `${basename}${newUrl}` : newUrl
191194
}
192195

193196
/**

0 commit comments

Comments
 (0)