Skip to content
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-17039090@ : [Server Affinity] - Attach dwsid to SCAPI request headers #2090

Merged
merged 10 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/commerce-sdk-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v3.2.0-dev (Oct 14, 2024)

- [Server Affinity] Attach dwsid to SCAPI request headers [#2090](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2090)
- Add the `authorizeCustomer` and `getPasswordResetToken` to the `ShopperLoginMutations` [#2056](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2056)
- Add useDNT hook to commerce-sdk-react and put DNT in auth [#2067](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2067/files)
- Add Trusted Agent on Behalf of (TAOB) support for SLAS APIs [#2077](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2077)
Expand Down
11 changes: 6 additions & 5 deletions packages/commerce-sdk-react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import {
SLAS_SECRET_WARNING_MSG,
SLAS_SECRET_PLACEHOLDER,
SLAS_SECRET_OVERRIDE_MSG,
SLAS_REFRESH_TOKEN_COOKIE_TTL_OVERRIDE_MSG,
DNT_COOKIE_NAME
DNT_COOKIE_NAME,
DWSID_COOKIE_NAME,
SLAS_REFRESH_TOKEN_COOKIE_TTL_OVERRIDE_MSG
} from '../constant'

import {Logger} from '../types'
Expand Down Expand Up @@ -76,7 +77,7 @@ type AuthDataKeys =
| 'refresh_token_registered'
| 'access_token_sfra'
| typeof DNT_COOKIE_NAME
| 'dwsid'
| typeof DWSID_COOKIE_NAME

type AuthDataMap = Record<
AuthDataKeys,
Expand Down Expand Up @@ -171,7 +172,7 @@ const DATA_MAP: AuthDataMap = {
},
dwsid: {
storageType: 'cookie',
key: 'dwsid'
key: DWSID_COOKIE_NAME
}
}

Expand Down Expand Up @@ -478,7 +479,7 @@ class Auth {
* registered shopper refresh-token and restores session and basket on SFRA.
*/
private clearECOMSession() {
const {key, storageType} = DATA_MAP['dwsid']
const {key, storageType} = DATA_MAP[DWSID_COOKIE_NAME]
const store = this.stores[storageType]
store.delete(key)
}
Expand Down
16 changes: 15 additions & 1 deletion packages/commerce-sdk-react/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ export const SLAS_REFRESH_TOKEN_COOKIE_TTL_OVERRIDE_MSG =
'You are attempting to use an invalid refresh token TTL value.'

export const DNT_COOKIE_NAME = 'dw_dnt' as const

export const DWSID_COOKIE_NAME = 'dwsid'
// commerce-sdk-react namespaces cookies with siteID as suffixes to allow multisite setups.
// However some cookies are set and used outside of PWA Kit and must not be modified with suffixes.
export const EXCLUDE_COOKIE_SUFFIX = ['dwsid', DNT_COOKIE_NAME]
export const EXCLUDE_COOKIE_SUFFIX = [DWSID_COOKIE_NAME, DNT_COOKIE_NAME]

/**
* For Hybrid Setups only!
* Unlike SCAPI/OCAPI, ECOM creates baskets in app-server cache initially and move the basket object
* to the db later based on basket state. In a hybrid storefront, storefront requests might be
* routed to different appservers, if the basket object is still in appserver cache, you will start
* seeing inconsistencies in basket state. To avoid this, if you have a dwsid cookie, you must send
* the value of the dwsid cookie with each SCAPI/OCAPI request in a hybrid storefront to maintain appserver affinity.
*
* Use the header key below to send dwsid value with SCAPI/OCAPI requests.
*/
export const SERVER_AFFINITY_HEADER_KEY = 'sfdc_dwsid'
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {

jest.mock('../../auth/index.ts', () => {
return jest.fn().mockImplementation(() => ({
ready: jest.fn().mockResolvedValue({access_token: 'access_token'})
ready: jest.fn().mockResolvedValue({access_token: 'access_token'}),
get: jest.fn().mockResolvedValue({dwsid: 'dw-session-123'})
}))
})

Expand Down
88 changes: 51 additions & 37 deletions packages/commerce-sdk-react/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import {
import Auth from './auth'
import {ApiClientConfigParams, ApiClients} from './hooks/types'
import {Logger} from './types'
import {MOBIFY_PATH, SLAS_PRIVATE_PROXY_PATH} from './constant'
import {
DWSID_COOKIE_NAME,
MOBIFY_PATH,
SERVER_AFFINITY_HEADER_KEY,
SLAS_PRIVATE_PROXY_PATH
} from './constant'
export interface CommerceApiProviderProps extends ApiClientConfigParams {
children: React.ReactNode
proxy: string
Expand Down Expand Up @@ -125,9 +130,53 @@ const CommerceApiProvider = (props: CommerceApiProviderProps): ReactElement => {
// Set the logger based on provided configuration, or default to the console object if no logger is provided
const configLogger = logger || console

const auth = useMemo(() => {
return new Auth({
clientId,
organizationId,
shortCode,
siteId,
proxy,
redirectURI,
fetchOptions,
fetchedToken,
enablePWAKitPrivateClient,
clientSecret,
silenceWarnings,
logger: configLogger,
defaultDnt,
refreshTokenRegisteredCookieTTL,
refreshTokenGuestCookieTTL
})
}, [
clientId,
organizationId,
shortCode,
siteId,
proxy,
redirectURI,
fetchOptions,
fetchedToken,
enablePWAKitPrivateClient,
clientSecret,
silenceWarnings,
configLogger,
refreshTokenRegisteredCookieTTL,
refreshTokenGuestCookieTTL
])

const dwsid = auth.get(DWSID_COOKIE_NAME)
const serverAffinityHeader: Record<string, string> = {}
if (dwsid) {
serverAffinityHeader[SERVER_AFFINITY_HEADER_KEY] = dwsid
}

const config = {
proxy,
headers,
headers: {
...headers,
...serverAffinityHeader
},
parameters: {
clientId,
organizationId,
Expand Down Expand Up @@ -173,41 +222,6 @@ const CommerceApiProvider = (props: CommerceApiProviderProps): ReactElement => {
headers?.['correlation-id']
])

const auth = useMemo(() => {
return new Auth({
clientId,
organizationId,
shortCode,
siteId,
proxy,
redirectURI,
fetchOptions,
fetchedToken,
enablePWAKitPrivateClient,
clientSecret,
silenceWarnings,
logger: configLogger,
defaultDnt,
refreshTokenRegisteredCookieTTL,
refreshTokenGuestCookieTTL
})
}, [
clientId,
organizationId,
shortCode,
siteId,
proxy,
redirectURI,
fetchOptions,
fetchedToken,
enablePWAKitPrivateClient,
clientSecret,
silenceWarnings,
configLogger,
refreshTokenRegisteredCookieTTL,
refreshTokenGuestCookieTTL
])

// Initialize the session
useEffect(() => void auth.ready(), [auth])

Expand Down
2 changes: 2 additions & 0 deletions packages/pwa-kit-create-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## v3.8.0-dev (Aug 8, 2024)

- Removed OCAPISessionURL prop from provider template. [#2090](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2090)
- Update ssr.js templates to include new feature flag to encode non ASCII HTTP headers [#2048](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2048)
- Replace getAppOrigin with useOrigin to have a better support for an app origin building. [#2050](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2050)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const AppConfig = ({children, locals = {}}) => {
redirectURI={`${appOrigin}/callback`}
proxy={`${appOrigin}${commerceApiConfig.proxyPath}`}
headers={headers}
OCAPISessionsURL={`${appOrigin}${proxyBasePath}/ocapi/s/${locals.site?.id}/dw/shop/v22_8/sessions`}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that we're removing the OCAPI sessions URL from the generated apps since we've removed it from template-retail-react-app already.

logger={createLogger({packageName: 'commerce-sdk-react'})}
{{#if answers.project.commerce.isSlasPrivate}}
// Set 'enablePWAKitPrivateClient' to true use SLAS private client login flows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const AppConfig = ({children, locals = {}}) => {
redirectURI={`${appOrigin}/callback`}
proxy={`${appOrigin}${commerceApiConfig.proxyPath}`}
headers={headers}
OCAPISessionsURL={`${appOrigin}${proxyBasePath}/ocapi/s/${locals.site?.id}/dw/shop/v22_8/sessions`}
logger={createLogger({packageName: 'commerce-sdk-react'})}
{{#if answers.project.commerce.isSlasPrivate}}
// Set 'enablePWAKitPrivateClient' to true use SLAS private client login flows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ module.exports = {
{
host: '{{answers.project.commerce.shortCode}}.api.commercecloud.salesforce.com',
path: 'api'
}{{~#if answers.project.commerce.instanceUrl}},
{
host: '{{answers.project.commerce.instanceUrl}}',
path: 'ocapi'
}{{/if}}
}
]
}
}
1 change: 1 addition & 0 deletions packages/pwa-kit-react-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## v3.8.0-dev (Aug 08, 2024)
- [Server Affinity] - Attach dwsid to SCAPI request headers [#2090](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2090)
- Create useOrigin hook to return an app origin that takes x-forwarded-host header into consideration. [#2050](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2050)

## v3.7.0 (Aug 07, 2024)
Expand Down
4 changes: 0 additions & 4 deletions packages/pwa-kit-react-sdk/setup-jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ jest.mock('@salesforce/pwa-kit-runtime/utils/ssr-config', () => {
{
host: 'kv7kzm78.api.commercecloud.salesforce.com',
path: 'api'
},
{
host: 'zzrf-001.dx.commercecloud.salesforce.com',
path: 'ocapi'
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions packages/template-retail-react-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## v4.1.0-dev (Aug 8, 2024)

- [Server Affinity] - Attach dwsid to SCAPI request headers & remove OCAPI proxy [#2090](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2090)
- Announce wishlist change in total for screen readers (a11y) [#2033](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2033)
- Fixed a bug that incorrectly imports uninstalled package `@chakra-ui/layout` [#2047](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2047)
- Replace getAppOrigin with useOrigin to have a better support for an app origin building. [#2050](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2050)
Expand Down
4 changes: 0 additions & 4 deletions packages/template-retail-react-app/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ module.exports = {
{
host: 'kv7kzm78.api.commercecloud.salesforce.com',
path: 'api'
},
{
host: 'zzrf-001.dx.commercecloud.salesforce.com',
path: 'ocapi'
}
]
}
Expand Down
4 changes: 0 additions & 4 deletions packages/template-retail-react-app/config/mocks/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ module.exports = {
{
host: 'localhost:8888',
path: 'api'
},
{
host: 'localhost:9999',
path: 'ocapi'
}
]
}
Expand Down
Loading