@@ -13,6 +13,7 @@ import {
1313 getSiteByReference
1414} from '@salesforce/retail-react-app/app/utils/site-utils'
1515import { 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