Skip to content

Commit

Permalink
Rename ssr-namespace-paths exports methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vcua-mobify committed Jun 20, 2024
1 parent 039a660 commit 69b9031
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
resolveLocaleFromUrl
} from '@salesforce/retail-react-app/app/utils/site-utils'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {getProxyPathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {proxyBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {createUrlTemplate} from '@salesforce/retail-react-app/app/utils/url'

import {CommerceApiProvider} from '@salesforce/commerce-sdk-react'
Expand Down Expand Up @@ -56,9 +56,7 @@ const AppConfig = ({children, locals = {}}) => {
redirectURI={`${appOrigin}/callback`}
proxy={`${appOrigin}${commerceApiConfig.proxyPath}`}
headers={headers}
OCAPISessionsURL={`${appOrigin}${getProxyPathBase()}/ocapi/s/${
locals.site?.id
}/dw/shop/v22_8/sessions`}
OCAPISessionsURL={`${appOrigin}${proxyBasePath}/ocapi/s/${locals.site?.id}/dw/shop/v22_8/sessions`}
{{#if answers.project.commerce.isSlasPrivate}}
// Set 'enablePWAKitPrivateClient' to true use SLAS private client login flows.
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
resolveLocaleFromUrl
} from '@salesforce/retail-react-app/app/utils/site-utils'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {getProxyPathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {proxyBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {createUrlTemplate} from '@salesforce/retail-react-app/app/utils/url'

import {CommerceApiProvider} from '@salesforce/commerce-sdk-react'
Expand Down Expand Up @@ -56,9 +56,7 @@ const AppConfig = ({children, locals = {}}) => {
redirectURI={`${appOrigin}/callback`}
proxy={`${appOrigin}${commerceApiConfig.proxyPath}`}
headers={headers}
OCAPISessionsURL={`${appOrigin}${getProxyPathBase()}/ocapi/s/${
locals.site?.id
}/dw/shop/v22_8/sessions`}
OCAPISessionsURL={`${appOrigin}${proxyBasePath}/ocapi/s/${locals.site?.id}/dw/shop/v22_8/sessions`}
{{#if answers.project.commerce.isSlasPrivate}}
// Set 'enablePWAKitPrivateClient' to true use SLAS private client login flows.
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
Expand Down
6 changes: 3 additions & 3 deletions packages/pwa-kit-dev/src/ssr/server/build-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import open from 'open'
import requireFromString from 'require-from-string'
import {RemoteServerFactory} from '@salesforce/pwa-kit-runtime/ssr/server/build-remote-server'
import {proxyConfigs} from '@salesforce/pwa-kit-runtime/utils/ssr-shared'
import {getBundlePathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {bundleBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {
SERVER,
CLIENT,
Expand Down Expand Up @@ -167,7 +167,7 @@ export const DevServerMixin = {
app.__hotServerMiddleware = webpackHotServerMiddleware(app.__compiler)
}

app.use(`${getBundlePathBase()}/development`, app.__devMiddleware)
app.use(`${bundleBasePath}/development`, app.__devMiddleware)

app.__hmrMiddleware = (_, res) => res.status(501).send('Hot Module Reloading is disabled.')
const clientCompiler = app.__compiler.compilers.find((compiler) => compiler.name === CLIENT)
Expand Down Expand Up @@ -210,7 +210,7 @@ export const DevServerMixin = {
// Proxy bundle asset requests to the local
// build directory.
app.use(
`${getBundlePathBase()}/development`,
`${bundleBasePath}/development`,
express.static(path.resolve(process.cwd(), 'src'), {
dotFiles: 'deny',
setHeaders: setLocalAssetHeaders,
Expand Down
4 changes: 2 additions & 2 deletions packages/pwa-kit-dev/src/ssr/server/public-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* but we need something more dynamic to support namespaced /mobify paths.
*/

import {getBundlePathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {bundleBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'

/* global __webpack_public_path__: writable */
__webpack_public_path__ = `${getBundlePathBase()}/development/`
__webpack_public_path__ = `${bundleBasePath}/development/`
4 changes: 2 additions & 2 deletions packages/pwa-kit-react-sdk/src/ssr/universal/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @module progressive-web-sdk/ssr/universal/utils
*/
import {proxyConfigs} from '@salesforce/pwa-kit-runtime/utils/ssr-shared'
import {getBundlePathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {bundleBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'

const onClient = typeof window !== 'undefined'

Expand All @@ -23,7 +23,7 @@ export const getAssetUrl = (path) => {
/* istanbul ignore next */
const publicPath = onClient
? `${window.Progressive.buildOrigin}`
: `${getBundlePathBase()}/${process.env.BUNDLE_ID || 'development'}/`
: `${bundleBasePath}/${process.env.BUNDLE_ID || 'development'}/`
return path ? `${publicPath}${path}` : publicPath
}

Expand Down
25 changes: 16 additions & 9 deletions packages/pwa-kit-runtime/src/ssr/server/build-remote-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ import http from 'http'
import https from 'https'
import {proxyConfigs, updatePackageMobify} from '../../utils/ssr-shared'
import {
startsWithMobify,
getProxyPathBase,
getHealthCheckPath,
getSLASPrivateProxyPath
proxyBasePath,
bundleBasePath,
healthCheckPath,
slasPrivateProxyPath
} from '../../utils/ssr-namespace-paths'
import {applyProxyRequestHeaders} from '../../utils/ssr-server/configure-proxy'
import awsServerlessExpress from 'aws-serverless-express'
Expand Down Expand Up @@ -210,6 +210,13 @@ export const RemoteServerFactory = {
return true
},

/**
* @private
*/
_isBundleOrProxyPath(url) {
return url.startsWith(proxyBasePath) || url.startsWith(bundleBasePath)
},

/**
* @private
*/
Expand Down Expand Up @@ -435,7 +442,7 @@ export const RemoteServerFactory = {
const processIncomingRequest = (req, res) => {
const options = req.app.options
// If the request is for a proxy or bundle path, do nothing
if (startsWithMobify(req.originalUrl)) {
if (this._isBundleOrProxyPath(req.originalUrl)) {
return
}

Expand Down Expand Up @@ -558,7 +565,7 @@ export const RemoteServerFactory = {
// different types of the 'req' object, and will
// always contain the original full path.
/* istanbul ignore else */
if (!startsWithMobify(req.originalUrl)) {
if (!this._isBundleOrProxyPath(req.originalUrl)) {
req.app.sendMetric(
'RequestTime',
Date.now() - locals.requestStart,
Expand Down Expand Up @@ -604,7 +611,7 @@ export const RemoteServerFactory = {
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_setupProxying(app, options) {
app.all(`${getProxyPathBase()}/*`, (_, res) => {
app.all(`${proxyBasePath}/*`, (_, res) => {
return res.status(501).json({
message:
'Environment proxies are not set: https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/proxying-requests.html'
Expand Down Expand Up @@ -632,7 +639,7 @@ export const RemoteServerFactory = {
return
}

const slasPrivateClientProxyPath = getSLASPrivateProxyPath()
const slasPrivateClientProxyPath = slasPrivateProxyPath

localDevLog(`Proxying ${slasPrivateClientProxyPath} to ${options.slasTarget}`)

Expand Down Expand Up @@ -694,7 +701,7 @@ export const RemoteServerFactory = {
* @private
*/
_setupHealthcheck(app) {
app.get(`${getHealthCheckPath()}`, (_, res) =>
app.get(`${healthCheckPath}`, (_, res) =>
res.set('cache-control', NO_CACHE).sendStatus(200).end()
)
},
Expand Down
4 changes: 2 additions & 2 deletions packages/pwa-kit-runtime/src/ssr/server/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const APPLICATION_OCTET_STREAM = 'application/octet-stream'
export const BUILD = 'build'
export const STATIC_ASSETS = 'static_assets'

/** * @deprecated Use ssr-namespace-paths getProxyPathBase() instead */
/** * @deprecated Use ssr-namespace-paths proxyBasePath instead */
export const PROXY_PATH_PREFIX = '/mobify/proxy'

// All these values MUST be lower case
Expand All @@ -23,5 +23,5 @@ export const NO_CACHE = 'max-age=0, nocache, nostore, must-revalidate'
export const CONTENT_SECURITY_POLICY = 'content-security-policy'
export const STRICT_TRANSPORT_SECURITY = 'strict-transport-security'

/** * @deprecated Use ssr-namespace-paths getSLASPrivateProxyPath() instead */
/** * @deprecated Use ssr-namespace-paths.slasPrivateProxyPath instead */
export const SLAS_CUSTOM_PROXY_PATH = '/mobify/slas/private'
40 changes: 10 additions & 30 deletions packages/pwa-kit-runtime/src/utils/ssr-namespace-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const CACHING_PATH_BASE = `${MOBIFY_PATH}/caching`
const HEALTHCHECK_PATH = `${MOBIFY_PATH}/ping`
const SLAS_PRIVATE_CLIENT_PROXY_PATH = `${MOBIFY_PATH}/slas/private`

const getNamespace = () => {
/**
* @private
*/
const _getNamespace = () => {
// TODO - namespaces for /mobify path will be implemented at a later date.
// Returns an empty string for now.
// Below is an example of what this implementation might look like.
Expand All @@ -31,33 +34,10 @@ const getNamespace = () => {
return ''
}

export const startsWithMobify = (url) => {
const namespace = getNamespace()
const mobifyPath = `${namespace}${MOBIFY_PATH}`
return url.startsWith(mobifyPath)
}

export const getProxyPathBase = () => {
const namespace = getNamespace()
return `${namespace}${PROXY_PATH_BASE}`
}

export const getBundlePathBase = () => {
const namespace = getNamespace()
return `${namespace}${BUNDLE_PATH_BASE}`
}

export const getCachingPathBase = () => {
const namespace = getNamespace()
return `${namespace}${CACHING_PATH_BASE}`
}
export const ssrNamespace = _getNamespace()

export const getHealthCheckPath = () => {
const namespace = getNamespace()
return `${namespace}${HEALTHCHECK_PATH}`
}

export const getSLASPrivateProxyPath = () => {
const namespace = getNamespace()
return `${namespace}${SLAS_PRIVATE_CLIENT_PROXY_PATH}`
}
export const proxyBasePath = `${ssrNamespace}${PROXY_PATH_BASE}`
export const bundleBasePath = `${ssrNamespace}${BUNDLE_PATH_BASE}`
export const cachingBasePath = `${ssrNamespace}${CACHING_PATH_BASE}`
export const healthCheckPath = `${ssrNamespace}${HEALTHCHECK_PATH}`
export const slasPrivateProxyPath = `${ssrNamespace}${SLAS_PRIVATE_CLIENT_PROXY_PATH}`
6 changes: 3 additions & 3 deletions packages/pwa-kit-runtime/src/utils/ssr-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
APPLICATION_OCTET_STREAM
} from '../ssr/server/constants'

import {getProxyPathBase} from './ssr-namespace-paths'
import {proxyBasePath} from './ssr-namespace-paths'

const baseMobify = {
ssrEnabled: true,
Expand Down Expand Up @@ -118,11 +118,11 @@ describe('utils/ssr-server tests', () => {

updatePackageMobify(baseMobify)

expect(getFullRequestURL(`${getProxyPathBase()}/base/somepath`)).toBe(
expect(getFullRequestURL(`${proxyBasePath}/base/somepath`)).toBe(
'https://www.merlinspotions.com/somepath'
)

expect(getFullRequestURL(`${getProxyPathBase()}/base2/somepath`)).toBe(
expect(getFullRequestURL(`${proxyBasePath}/base2/somepath`)).toBe(
'https://api.merlinspotions.com/somepath'
)
})
Expand Down
13 changes: 5 additions & 8 deletions packages/pwa-kit-runtime/src/utils/ssr-server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import crypto from 'crypto'
import {proxyConfigs} from '../ssr-shared'
import {getProxyPathBase, getBundlePathBase} from '../ssr-namespace-paths'
import {proxyBasePath, bundleBasePath} from '../ssr-namespace-paths'

// TODO: Clean this up or provide a way to toggle
export const verboseProxyLogging = false
Expand All @@ -21,7 +21,7 @@ export const isRemote = () =>
Object.prototype.hasOwnProperty.call(process.env, 'AWS_LAMBDA_FUNCTION_NAME')

export const getBundleBaseUrl = () => {
return `${getBundlePathBase()}/${isRemote() ? process.env.BUNDLE_ID : 'development'}/`
return `${bundleBasePath}/${isRemote() ? process.env.BUNDLE_ID : 'development'}/`
}

let QUIET = false
Expand Down Expand Up @@ -82,16 +82,13 @@ export const getHashForString = (text) => {
export const getFullRequestURL = (url) => {
// If it starts with a protocol (e.g. http(s)://, file://), then it's already a full URL
if (/^[a-zA-Z]+:\/\//.test(url)) return url
const proxy = proxyConfigs.find(({path}) => url.startsWith(`${getProxyPathBase()}/${path}/`))
const proxy = proxyConfigs.find(({path}) => url.startsWith(`${proxyBasePath}/${path}/`))
if (proxy) {
return url.replace(
`${getProxyPathBase()}/${proxy.path}`,
`${proxy.protocol}://${proxy.host}`
)
return url.replace(`${proxyBasePath}/${proxy.path}`, `${proxy.protocol}://${proxy.host}`)
}

throw new Error(
`Unable to fetch ${url}, relative paths must begin with ${getProxyPathBase()} followed by a configured proxy path.`
`Unable to fetch ${url}, relative paths must begin with ${proxyBasePath} followed by a configured proxy path.`
)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/pwa-kit-runtime/src/utils/ssr-shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import {getProxyPathBase, getCachingPathBase} from './ssr-namespace-paths'
import {proxyBasePath, cachingBasePath} from './ssr-namespace-paths'

/**
* @module progressive-web-sdk/utils/ssr-shared
Expand Down Expand Up @@ -169,8 +169,8 @@ export const updatePackageMobify = (newValue) => {
}

// Generate paths
config.proxyPath = `${getProxyPathBase()}/${config.path}`
config.cachingPath = `${getCachingPathBase()}/${config.path}`
config.proxyPath = `${proxyBasePath}/${config.path}`
config.cachingPath = `${cachingBasePath}/${config.path}`

proxyConfigs.push(config)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
resolveLocaleFromUrl
} from '@salesforce/retail-react-app/app/utils/site-utils'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {getProxyPathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {proxyBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {createUrlTemplate} from '@salesforce/retail-react-app/app/utils/url'

import {CommerceApiProvider} from '@salesforce/commerce-sdk-react'
Expand Down Expand Up @@ -68,9 +68,7 @@ const AppConfig = ({children, locals = {}}) => {
// Uncomment 'enablePWAKitPrivateClient' to use SLAS private client login flows.
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
// enablePWAKitPrivateClient={true}
OCAPISessionsURL={`${appOrigin}${getProxyPathBase()}/ocapi/s/${
locals.site?.id
}/dw/shop/v22_8/sessions`}
OCAPISessionsURL={`${appOrigin}${proxyBasePath}/ocapi/s/${locals.site?.id}/dw/shop/v22_8/sessions`}
>
<MultiSiteProvider site={locals.site} locale={locals.locale} buildUrl={locals.buildUrl}>
<ChakraProvider theme={theme}>{children}</ChakraProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
/*global dw*/
import {ACTIVE_DATA_ENABLED} from '@salesforce/retail-react-app/app/constants'
import {getProxyPathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {proxyBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'

const useActiveData = () => {
// Returns true when the feature flag is enabled and the tracking scripts have been executed
Expand Down Expand Up @@ -69,7 +69,7 @@ const useActiveData = () => {
if (!canTrack()) return
try {
var activeDataUrl =
`${getProxyPathBase()}/ocapi/on/demandware.store/Sites-` +
`${proxyBasePath}/ocapi/on/demandware.store/Sites-` +
siteId +
'-Site/' +
localeId +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, {useState, ReactElement} from 'react'
import {CommerceApiProvider} from '@salesforce/commerce-sdk-react'
import {withReactQuery} from '@salesforce/pwa-kit-react-sdk/ssr/universal/components/with-react-query'
import {useCorrelationId} from '@salesforce/pwa-kit-react-sdk/ssr/universal/hooks'
import {getProxyPathBase} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'
import {proxyBasePath} from '@salesforce/pwa-kit-runtime/utils/ssr-namespace-paths'

interface AppConfigProps {
children: React.ReactNode
Expand All @@ -32,7 +32,7 @@ const AppConfig = (props: AppConfigProps): ReactElement => {
clientId="4afbc51f-6423-41c8-8b29-d7f2825b5bee"
organizationId="f_ecom_zzrf_006"
redirectURI="http://localhost:3000/callback"
proxy={`http://localhost:3000/${String(getProxyPathBase())}/api`}
proxy={`http://localhost:3000/${String(proxyBasePath)}/api`}
locale={locale}
currency="USD"
headers={headers}
Expand Down

0 comments on commit 69b9031

Please sign in to comment.