1212// we don't want it to count toward coverage until we figure out how to cover the `functions`
1313// metric for this file in its test.
1414
15- import React from 'react'
15+ import React , { useEffect } from 'react'
16+ import { withRouter } from 'react-router-dom'
1617import loadable from '@loadable/component'
1718import { getConfig } from '@salesforce/pwa-kit-runtime/utils/ssr-config'
18- import { withRouter } from 'react-router-dom '
19- import { useEffect } from 'react'
19+ import useMultiSite from '@salesforce/retail- react-app/app/hooks/use-multi-site '
20+
2021// Components
2122import { Skeleton } from '@salesforce/retail-react-app/app/components/shared/ui'
2223import { configureRoutes } from '@salesforce/retail-react-app/app/utils/routes-utils'
@@ -122,6 +123,9 @@ export const routes = [
122123 }
123124]
124125
126+ // Remove SFRA/SiteGenesis routes from PWA Kit
127+ const ecomRoutes = [ '/cart' , '/checkout' , '*' ]
128+
125129export default ( ) => {
126130 const config = getConfig ( )
127131 const enableHybrid = config ?. app ?. enableHybrid
@@ -132,9 +136,6 @@ export default () => {
132136 const passwordlessLoginEnabled = loginConfig ?. passwordless ?. enabled
133137 const passwordlessLoginLandingPath = loginConfig ?. passwordless ?. landingPath
134138
135- // Paths handled by SFRA in hybrid mode
136- const hybridPaths = [ '/cart' , '/checkout' , '/checkout/confirmation/:orderNo' ]
137-
138139 // Add dynamic routes conditionally (only if features are enabled and paths are defined)
139140 const dynamicRoutes = [
140141 resetPasswordLandingPath && {
@@ -156,53 +157,46 @@ export default () => {
156157 }
157158 ] . filter ( Boolean )
158159
159- const allRoutes = configureRoutes (
160- [ ...routes , ...dynamicRoutes ] . filter (
161- ( r ) => ! enableHybrid || ! hybridPaths . includes ( r . path )
162- ) ,
163- config ,
164- { ignoredRoutes : [ '/callback' ] , fuzzyPathMatching : true }
165- )
166-
167- // Add catch-all route at the end so it doesn't match before dynamic routes
168- return [
169- ...allRoutes ,
160+ const allBaseRoutes = [ ...routes , ...dynamicRoutes ]
161+
162+ const hybridRoutes = [
163+ ...allBaseRoutes . filter ( ( route ) => ! ecomRoutes . includes ( route . path ) ) ,
170164 {
171165 path : '*' ,
172166 component : withRouter ( ( props ) => {
173167 const { location} = props
174168 const urlParams = new URLSearchParams ( location . search )
169+ const { site} = useMultiSite ( )
170+ const siteId = site && site . id ? site . id : config ?. app ?. defaultSite
175171
176- useEffect ( ( ) => {
177- if ( enableHybrid && ! urlParams . has ( 'redirected' ) ) {
178- // Redirect client-side navigations directly to SFRA
179- const sfccOrigin = getConfig ( ) ?. app ?. sfccOrigin
180- const siteId = getConfig ( ) ?. app ?. defaultSite || 'RefArchGlobal'
181- // Strip the PWA Kit site/locale prefix (e.g. /global/en-GB) from the path
182- const pwaPath = location . pathname . replace (
183- / ^ \/ [ ^ / ] + \/ [ ^ / ] + \/ / ,
184- '/'
185- )
186- const isLocalhost = window . location . hostname === 'localhost'
187- const target =
188- sfccOrigin && ! isLocalhost
189- ? `${ sfccOrigin } /s/${ siteId } ${ pwaPath } `
190- : `/s/${ siteId } ${ pwaPath } `
191- window . location . replace ( target )
192- return
193- }
194- const newURL = new URL ( window . location )
195- if ( ! urlParams . has ( 'redirected' ) ) {
196- newURL . searchParams . append ( 'redirected' , '1' )
197- window . location . href = newURL
198- }
199- } , [ window . location . href ] )
172+ if ( typeof window !== 'undefined' ) {
173+ useEffect ( ( ) => {
174+ const newURL = new URL ( window . location )
175+ if ( ! urlParams . has ( 'redirected' ) ) {
176+ newURL . searchParams . append ( 'redirected' , '1' )
177+ newURL . pathname = `/s/${ siteId } /${ window . location . pathname
178+ . split ( '/' )
179+ . slice ( 2 )
180+ . join ( '/' ) } `
181+ window . location . replace ( newURL )
182+ }
183+ } , [ window . location . href ] )
184+ }
200185
201186 if ( urlParams . has ( 'redirected' ) ) {
202187 return < PageNotFound { ...props } />
203188 }
189+
204190 return null
205191 } )
206192 }
207193 ]
194+
195+ // Only use these routes if we are in hybrid mode otherwise use defaults
196+ // This is driven via the config and env variables
197+ const routesToConfigure = enableHybrid ? hybridRoutes : allBaseRoutes
198+
199+ return configureRoutes ( routesToConfigure , config , {
200+ ignoredRoutes : [ '/callback' , '*' ]
201+ } )
208202}
0 commit comments