Skip to content

Commit 1ef8189

Browse files
fix: pwa review enhancements
1 parent 5334d90 commit 1ef8189

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

packages/template-retail-react-app/app/components/shopper-agent/index.jsx

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ const initEmbeddedMessaging = (
4949
window.embeddedservice_bootstrap &&
5050
window.embeddedservice_bootstrap.settings
5151
) {
52+
console.log('Initializing Embedded Messaging with:', {
53+
salesforceOrgId,
54+
embeddedServiceDeploymentName,
55+
embeddedServiceDeploymentUrl,
56+
scrt2Url,
57+
locale
58+
})
59+
5260
window.embeddedservice_bootstrap.settings.language = locale
5361
window.embeddedservice_bootstrap.settings.disableStreamingResponses = true
5462
window.embeddedservice_bootstrap.init(
@@ -59,6 +67,12 @@ const initEmbeddedMessaging = (
5967
scrt2URL: scrt2Url
6068
}
6169
)
70+
} else {
71+
console.warn('Embedded Messaging not ready:', {
72+
onClient,
73+
hasBootstrap: !!window.embeddedservice_bootstrap,
74+
hasSettings: !!window.embeddedservice_bootstrap?.settings
75+
})
6276
}
6377
} catch (err) {
6478
console.error('Error initializing Embedded Messaging: ', err)
@@ -92,7 +106,7 @@ function useMiaw(
92106
locale
93107
) {
94108
useEffect(() => {
95-
// Check if the embedded messaging script has been loaded and is available
109+
// Only initialize if the script is already loaded
96110
if (onClient && window.embeddedservice_bootstrap) {
97111
initEmbeddedMessaging(
98112
salesforceOrgId,
@@ -188,7 +202,7 @@ const ShopperAgentWindow = ({commerceAgentConfiguration, locale, basketId}) => {
188202
handleEmbeddedMessagingWindowMaximized
189203
)
190204
}
191-
}, [commerceAgentConfiguration, usid, theme.zIndices.sticky])
205+
}, [siteId, locale, commerceOrgId, usid, theme.zIndices.sticky])
192206

193207
/**
194208
* Updates basket ID in pre-chat fields when basket changes.
@@ -228,6 +242,18 @@ const ShopperAgentWindow = ({commerceAgentConfiguration, locale, basketId}) => {
228242
src={scriptSourceUrl}
229243
async
230244
type="text/javascript"
245+
onLoad={() => {
246+
// Trigger initialization when script loads
247+
if (window.embeddedservice_bootstrap) {
248+
initEmbeddedMessaging(
249+
salesforceOrgId,
250+
embeddedServiceName,
251+
embeddedServiceEndpoint,
252+
scrt2Url,
253+
locale
254+
)
255+
}
256+
}}
231257
/>
232258
</Helmet>
233259
)
@@ -267,10 +293,21 @@ ShopperAgentWindow.propTypes = {
267293
function ShopperAgent({commerceAgentConfiguration, basketId, locale, basketDoneLoading}) {
268294
const {enabled} = commerceAgentConfiguration
269295
const isShopperAgentEnabled = isEnabled(enabled)
296+
const isConfigValid = validateCommerceAgentSettings(commerceAgentConfiguration)
297+
298+
// Debug logging
299+
if (onClient) {
300+
console.log('ShopperAgent render conditions:', {
301+
isShopperAgentEnabled,
302+
basketDoneLoading,
303+
isConfigValid,
304+
enabled,
305+
basketId,
306+
locale
307+
})
308+
}
270309

271-
return isShopperAgentEnabled &&
272-
basketDoneLoading &&
273-
validateCommerceAgentSettings(commerceAgentConfiguration) ? (
310+
return isShopperAgentEnabled && basketDoneLoading && isConfigValid ? (
274311
<ShopperAgentWindow
275312
commerceAgentConfiguration={commerceAgentConfiguration}
276313
locale={locale}

packages/template-retail-react-app/app/utils/shopper-agent-validation.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
* These domains are considered safe for loading Shopper Agent scripts.
1111
* @type {string[]}
1212
*/
13-
const TRUSTED_DOMAINS = ['*.salesforce.com', '*.force.com', '*.salesforce-scrt.com', '*.site.com']
13+
const TRUSTED_DOMAINS = [
14+
'*.salesforce.com',
15+
'*.force.com',
16+
'*.salesforce-scrt.com',
17+
'*.site.com',
18+
'*.pc-rnd.site.com',
19+
'*.pc-rnd.salesforce-scrt.com'
20+
]
1421

1522
/**
1623
* Validates if a URL is from a trusted domain.

packages/template-retail-react-app/app/utils/shopper-agent-validation.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ describe('Shopper Agent Validation Utils', () => {
3737
expect(isValidTrustedDomain('https://site.com/script.js')).toBe(true)
3838
})
3939

40+
test('should return true for valid pc-rnd.site.com domains', () => {
41+
expect(
42+
isValidTrustedDomain(
43+
'https://orgfarm-1645fa246c.test1.my.pc-rnd.site.com/script.js'
44+
)
45+
).toBe(true)
46+
expect(isValidTrustedDomain('https://test.pc-rnd.site.com/script.js')).toBe(true)
47+
expect(isValidTrustedDomain('https://pc-rnd.site.com/script.js')).toBe(true)
48+
})
49+
50+
test('should return true for valid pc-rnd.salesforce-scrt.com domains', () => {
51+
expect(
52+
isValidTrustedDomain(
53+
'https://orgfarm-1645fa246c.test1.my.pc-rnd.salesforce-scrt.com/script.js'
54+
)
55+
).toBe(true)
56+
expect(isValidTrustedDomain('https://test.pc-rnd.salesforce-scrt.com/script.js')).toBe(
57+
true
58+
)
59+
expect(isValidTrustedDomain('https://pc-rnd.salesforce-scrt.com/script.js')).toBe(true)
60+
})
61+
4062
test('should return false for malicious domains', () => {
4163
expect(isValidTrustedDomain('https://malicious-site.com/script.js')).toBe(false)
4264
expect(isValidTrustedDomain('https://evil.com/script.js')).toBe(false)
@@ -209,6 +231,17 @@ describe('Shopper Agent Validation Utils', () => {
209231

210232
expect(validateCommerceAgentSettings(validConfigWithSite)).toBe(true)
211233
})
234+
235+
test('should accept valid pc-rnd domains', () => {
236+
const validConfigWithPcRnd = {
237+
...validConfig,
238+
scriptSourceUrl:
239+
'https://orgfarm-1645fa246c.test1.my.pc-rnd.site.com/ESWAgentforceChannelWeb1745544099167/assets/js/bootstrap.min.js',
240+
scrt2Url: 'https://orgfarm-1645fa246c.test1.my.pc-rnd.salesforce-scrt.com'
241+
}
242+
243+
expect(validateCommerceAgentSettings(validConfigWithPcRnd)).toBe(true)
244+
})
212245
})
213246

214247
describe('isEnabled', () => {

0 commit comments

Comments
 (0)