Last active
May 22, 2025 23:24
-
-
Save sandragolden/10e1d14e86ce23c41bbad5be689df9ee to your computer and use it in GitHub Desktop.
Hybrid Override routes.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {routes as defaultRoutes} from '@salesforce/retail-react-app/app/routes' | |
import React, {useEffect} from 'react' | |
import {withRouter} from 'react-router-dom' | |
import loadable from '@loadable/component' | |
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config' | |
import useMultiSite from '@salesforce/retail-react-app/app/hooks/use-multi-site' | |
// Components | |
import {Skeleton} from '@salesforce/retail-react-app/app/components/shared/ui' | |
import {configureRoutes} from '@salesforce/retail-react-app/app/utils/routes-utils' | |
const fallback = <Skeleton height="75vh" width="100%" /> | |
// Pages | |
const PageNotFound = loadable( | |
() => import('@salesforce/retail-react-app/app/pages/page-not-found'), | |
{fallback} | |
) | |
// Custom Pages | |
// const MyCustomPage = loadable(() => import('./pages/my-custom-page'), {fallback}) | |
export const routes = defaultRoutes | |
// below is a commented-out example of how you could add your custom routes | |
// routes.splice(-1, 0, { | |
// path: '/my-custom-page/:pageId', | |
// component: MyCustomPage | |
// }) | |
// Remove SFRA/SiteGenesis routes from PWA Kit | |
const ecomRoutes = ['/cart', '/checkout', '*'] | |
const hybridRoutes = [ | |
...routes.filter((route) => !ecomRoutes.includes(route.path)), | |
{ | |
path: '*', | |
component: withRouter((props) => { | |
const config = getConfig() | |
const {location} = props | |
const urlParams = new URLSearchParams(location.search) | |
const {site} = useMultiSite() | |
const siteId = site && site.id ? site.id : config?.app?.defaultSite | |
if (typeof window !== 'undefined') { | |
useEffect(() => { | |
const newURL = new URL(window.location) | |
if (!urlParams.has('redirected')) { | |
newURL.searchParams.append('redirected', '1') | |
newURL.pathname = `/s/${siteId}/${window.location.pathname | |
.split('/') | |
.slice(2) | |
.join('/')}` | |
window.location.replace(newURL) | |
} | |
}, [window.location.href]) | |
} | |
if (urlParams.has('redirected')) { | |
return <PageNotFound {...props} /> | |
} | |
return null | |
}) | |
} | |
] | |
export default () => { | |
const config = getConfig() | |
// Only use these routes if we are in hybrid mode otherwise use defaults | |
// This is driven via the config and env variables | |
const routesToConfigure = config.app.enableHybrid ? hybridRoutes : routes | |
return configureRoutes(routesToConfigure, config, { | |
ignoredRoutes: ['/callback', '*'] | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment