-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@W-16442332@ Add support for environment level base path (the base path for /mobify routes) #1970
Conversation
packages/template-retail-react-app/app/components/_app-config/index.jsx
Outdated
Show resolved
Hide resolved
} | ||
|
||
export const ssrNamespace = _getNamespace() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we intend to also keep ssrNamespace
as part of the deprecated variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Urk, good catch. It was exported so we should keep it as a deprecated variable like the others
// TODO - Allow projects to define this function? | ||
export const getEnvBasePath = () => { | ||
const config = getConfig() | ||
return config?.envBasePath ? config.envBasePath : '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we expect to find a default fallback envBasePath
in the config?
If we do, shouldn't we print an error in console so users know that the expected configuration was not found?
Something like:
export const getEnvBasePath = () => {
const config = getConfig()
if (!config || typeof config.envBasePath !== 'string') {
log.error('Invalid environment base path configuration')
return ''
}
return config.envBasePath || ''
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried adding a logger.error but then the client side attempt to invoke logger as well and the logger does not exist client side.
For now, I have set a console.log to print an error of envBasePath isn't a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logger does exist on the client-side as well. It's just a wrapper around Node's console
object.
export const getBundlePath = () => `${getEnvBasePath()}${BUNDLE_PATH_BASE}` | ||
export const getCachingPath = () => `${getEnvBasePath()}${CACHING_PATH_BASE}` | ||
export const getHealthCheckPath = () => `${getEnvBasePath()}${HEALTHCHECK_PATH}` | ||
export const getSlasPrivateProxyPath = () => `${getEnvBasePath()}${SLAS_PRIVATE_CLIENT_PROXY_PATH}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add this work to a feature branch until all the parts are feature complete and tested, to avoid introducing new breaking changes?
*/ | ||
return '' | ||
// TODO - Allow projects to define this function? | ||
export const getEnvBasePath = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function getEnvBasePath()
is called multiple times in different files. This could potentially create a performance bottleneck if loops or complex logic is implemented.
Keeping a simple logic and cache the result of getEnvBasePath
could help to reduce the performance impact.
Note that if we give customers access, it might lead to more complex implementations and thus performance issues.
We can also introduce an ssr metric to monitor the execution time of this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getConfig is memoized on the server side so there should not be much of a performance impact there.
I think we are also ok client side since there we are just reading from window.__config__
once it's hydrated.
I removed the note about allowing projects to define this. I think for now we will keep this function definition internal.
packages/pwa-kit-dev/jest.config.js
Outdated
@@ -11,7 +11,7 @@ module.exports = { | |||
...base, | |||
coverageThreshold: { | |||
global: { | |||
branches: 67, | |||
branches: 65, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to lower this threshold since the new conditional in build-dev-server.js
for setting the HMR publicPath dropped coverage below 67%. There is no easy way to test the new conditional since the changes are inside _addSDKInternalHandlers
and our tests override _addSDKInternalHandlers
with a no-op.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, bumped up to 68
Signed-off-by: Kevin He <kevin.he@salesforce.com>
Co-authored-by: Ben Chypak <bchypak@mobify.com> Signed-off-by: Kevin He <kevin.he@salesforce.com>
@@ -46,6 +46,9 @@ const AppConfig = ({children, locals = {}}) => { | |||
|
|||
const appOrigin = getAppOrigin() | |||
|
|||
const redirectURI = `${appOrigin}/callback` | |||
const proxy = `${appOrigin}${getEnvBasePath()}${commerceApiConfig.proxyPath}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I was a partner dev and I saw this line of code I would probably be quite confused. Since we are giving people this file, this code is theirs and they should probably be able to understand it.
Have you thought about either creating a new util called getAppOriginWithBasePatth
or maybe modifying so we can optionally include the base path? like getAppOrigin({includeBasePath: true})
Just a thought.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good thought, but I think we may need to put more effort into this. I'd really love to get this merged soon and move on to the second part of this work...
if (!window.__CONFIG__ && typeof document !== 'undefined') { | ||
try { | ||
const data = JSON.parse(document.getElementById('mobify-data').innerHTML) | ||
|
||
// Set all globals sent from the server on the window object. | ||
Object.entries(data).forEach(([key, value]) => { | ||
window[key] = value | ||
}) | ||
} catch (error) { | ||
console.error('Unable to parse server-side rendered config.') | ||
console.error(error) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you mentioned this is leaking internal details into the runtime specifically those from the react-sdk. This isn't ideal, but not horrible. But if, like you said, it might not be required in this implementation, then I would take it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted
OCAPISessionsURL={`${appOrigin}${proxyBasePath}/ocapi/s/${locals.site?.id}/dw/shop/v22_8/sessions`} | ||
OCAPISessionsURL={`${appOrigin}${getProxyPath()}/ocapi/s/${ | ||
locals.site?.id | ||
}/dw/shop/v22_8/sessions`} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'm curious, why we are not following the same pattern of creating a const OCAPISessionsURL
like we do for redirectURI
and proxy
?
In #1824, we started to lay the groundwork for adding a namespace to our /mobify routes (which will be called the environment base path, or
envBasePath
from here on out).This PR follows up that work by allowing projects to define this
envBasePath
via the environment specific configuration files. A separate PR will be created following this PR to set the base path of app pages such as our PLP or PDP.Configuring the envBasePath
For example, suppose I have the an MRT environment named amer
In the config file named amer.js, I can set the envBasePath like so:
Change summary:
Testing these changes:
Locally:
Create a local.js config file and set envBasePath: '/local' (see above)
Start the app
See that all /mobify requests now have the
/local
basePathNavigate between different pages and make changes. See that HMR is working
Remote:
Create a ca.js and us.js config file and set envBasePath to
/ca
and/us
respectivelyDeploy the changes to our ca and us environments on MRT
See that all /mobify requests on ca now have the
/ca
basePathSee that all /mobify requests on us now have the
/us
basePathTODO
Apply PR feedback