SalesforceCommerceCloud / pwa-kit

React-based JavaScript frontend framework to create a progressive web app (PWA) storefront for Salesforce B2C Commerce.
https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/pwa-kit-overview.html
BSD 3-Clause "New" or "Revised" License
285 stars 138 forks source link

[BUG] Missing onContextChange callback for version 2.8 #1766

Closed denchev closed 3 months ago

denchev commented 7 months ago

Summary

The StrorefrontPreview component is supposed to receive a few arguments one of which is onContextChange callback. For version 2.8 the passed callback is ignored and it is not passed to the window.STOREFRONT_PREVIEW object. That's because it is not present in the list of properties for the StorefrontPreview component in pwa-kit-react-sdk (https://github.com/SalesforceCommerceCloud/pwa-kit/blob/release-2.8.x/packages/pwa-kit-react-sdk/src/ssr/universal/components/storefront-preview/index.jsx)

The version 3 of the component has the onContextChange as property though https://github.com/SalesforceCommerceCloud/pwa-kit/blob/94af623ad8028a0f9bf6776f24d8a1f5e5a37c46/packages/commerce-sdk-react/src/components/StorefrontPreview/storefront-preview.tsx#L30

I needed the callback so I created a workaround. Added in app/components/_app/index.jsx

useEffect(() => { if (typeof window !== 'undefined' && detectStorefrontPreview()) { window.STOREFRONT_PREVIEW.onContextChange = (context) => { console.log('Force onContextChange for 2.8', context) handleContextChange(context) } } }, [])

This workaround works when deployed to MRT.

The documentation clearly says that this callback can be passed when setting it up for version 2.8. Which leads me to believe that the missing onContextChange in version 2.8 is a bug.

I see two options: 1) Add the callback to the STOREFRONT_PREVIEW props. 2) Remove the onContextChange from the documentation for version 2.8

But since it works on MRT, I think it is better to go with 1)

Steps To Reproduce

Pass onContextChange callback with simple console.log Set preview options from Managed Runtime

Expected result

See the console.log in the browser console showing that callback is called.

Actual result

Nothing happens.

System Information (as applicable)

Browser: Any Node version: Any pwa-kit version: 2.8.2 Desktop OS: Any Mobile Device Info: Any

Additional information

vcua-mobify commented 7 months ago

Thanks for letting us know about this @denchev. I'll add a ticket for us to follow up on this.

echessman commented 3 months ago

Thank you for submitting this issue. We have reviewed the issue, but based on the severity of the issue described and the workaround you provided, we do not have plans to fix this issue at this time. Please comment and reopen the issue if you’d like to propose increasing the priority level.