Skip to content

Commit 5ac1003

Browse files
committed
Handle basename for urls that bypass react router
1 parent 523c429 commit 5ac1003

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +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'
2122

2223
// <Error> is rendered when:
2324
//
@@ -53,7 +54,11 @@ const Error = (props) => {
5354
// We need to use window.location.href here rather than history
5455
// as the application is in an error state. We need to force a
5556
// hard navigation to get back to the normal state.
56-
onClick={() => (window.location.href = '/')}
57+
// Include basename since this bypasses React Router
58+
onClick={() => {
59+
const envBasePath = getEnvBasePath()
60+
window.location.href = envBasePath ? `${envBasePath}/` : '/'
61+
}}
5762
/>
5863
</Box>
5964
</Box>

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +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'
910

1011
/**
1112
* This functions takes an url and returns a site object,
@@ -101,7 +102,14 @@ export const getSiteByReference = (siteRef) => {
101102
* @returns {{siteRef: string, localeRef: string}} - site and locale reference (it could either be id or alias)
102103
*/
103104
export const getParamsFromPath = (path) => {
104-
const {pathname, search} = getPathnameAndSearch(path)
105+
let {pathname, search} = getPathnameAndSearch(path)
106+
107+
// Remove the envBasePath from the pathname if present since
108+
// 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)
112+
}
105113

106114
const config = getConfig()
107115
const {pathMatcher, searchMatcherForSite, searchMatcherForLocale} = getConfigMatcher(config)

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getSiteByReference
1414
} from '@salesforce/retail-react-app/app/utils/site-utils'
1515
import {HOME_HREF, urlPartPositions} from '@salesforce/retail-react-app/app/constants'
16+
import {getEnvBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
1617

1718
/**
1819
* Constructs an absolute URL from a given path and an optional application origin.
@@ -54,6 +55,8 @@ export const rebuildPathWithParams = (url, extraParams) => {
5455
const paramStr = params.toString().replace(/=&/g, '&').replace(/=$/, '')
5556

5657
// Generate the newly updated url.
58+
// Note: If a basename is set in React Router, it will be added to the url automatically
59+
// so we do not need to use getEnvBasePath here.
5760
return `${pathname}${Array.from(paramStr).length > 0 ? `?${paramStr}` : ''}`
5861
}
5962

@@ -121,6 +124,11 @@ export const searchUrlBuilder = (searchTerm) => '/search?q=' + encodeURIComponen
121124
* Returns a relative URL for a locale short code.
122125
* Based on your app configuration, this function will replace your current locale shortCode with a new one
123126
*
127+
* Note: The URLs generated by this function are exptected to be used directly by
128+
* window.location to force a hard refresh of the page.
129+
* Because this bypasses React Router, we need to manually add the basename to the url, if one is
130+
* defined in the app config.
131+
*
124132
* @param {String} shortCode - The locale short code.
125133
* @param {function(*, *, *, *=): string} - Generates a site URL from the provided path, site and locale.
126134
* @param {string[]} opts.disallowParams - URL parameters to remove
@@ -132,6 +140,12 @@ export const getPathWithLocale = (shortCode, buildUrl, opts = {}) => {
132140
let {siteRef, localeRef} = getParamsFromPath(`${location.pathname}${location.search}`)
133141
let {pathname, search} = location
134142

143+
// Remove basename from pathname if present before processing
144+
const envBasePath = getEnvBasePath()
145+
if (envBasePath && pathname.startsWith(envBasePath)) {
146+
pathname = pathname.substring(envBasePath.length)
147+
}
148+
135149
// sanitize the site from current url if existing
136150
if (siteRef) {
137151
pathname = pathname.replace(`/${siteRef}`, '')
@@ -159,15 +173,21 @@ export const getPathWithLocale = (shortCode, buildUrl, opts = {}) => {
159173
const site = getSiteByReference(siteRef)
160174
const locale = getLocaleByReference(site, shortCode)
161175

162-
// rebuild the url with new locale,
176+
// Build the path without basename
177+
const url = `${pathname}${Array.from(queryString).length !== 0 ? `?${queryString}` : ''}`
178+
179+
// rebuild the url with new locale
163180
const newUrl = buildUrl(
164-
`${pathname}${Array.from(queryString).length !== 0 ? `?${queryString}` : ''}`,
181+
url,
165182
// By default, as for home page, when the values of site and locale belongs to the default site,
166183
// they will be not shown in the url just
167184
site.alias || site.id,
168185
locale?.alias || locale?.id
169186
)
170-
return newUrl
187+
188+
// Add basename for locale selection URLs since they bypass React Router
189+
// (React Router handles basename for navigation via basename prop)
190+
return envBasePath ? `${envBasePath}${newUrl}` : newUrl
171191
}
172192

173193
/**
@@ -224,6 +244,9 @@ export const createUrlTemplate = (appConfig, siteRef, localeRef) => {
224244
queryLocale && locale && searchParams.append('locale', locale)
225245
queryString = `?${searchParams.toString()}`
226246
}
247+
248+
// Note: If a basename is set in React Router, it will be added to the url automatically
249+
// so we do not need to add it here.
227250
return `${sitePath}${localePath}${path}${queryString}`
228251
}
229252
}

0 commit comments

Comments
 (0)