-
Notifications
You must be signed in to change notification settings - Fork 422
fix: resolve browser extension compatibility issue in ShopifyCustomer… #3576
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@shopify/hydrogen': patch | ||
| --- | ||
|
|
||
| Fix browser extension compatibility issue causing "Cannot redefine property: Shopify" error | ||
|
|
||
| Added defensive checks in ShopifyCustomerPrivacy component to handle cases where browser extensions define window.Shopify as non-configurable. The component now detects existing property descriptors and uses polling fallback when property redefinition is not possible, ensuring Hydrogen apps work correctly with extensions. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -340,6 +340,21 @@ export function useCustomerPrivacy(props: CustomerPrivacyApiProps) { | |||||
| let customShopify: {customerPrivacy: CustomerPrivacy} | undefined | object = | ||||||
| window.Shopify || undefined; | ||||||
|
|
||||||
| const shopifyDescriptor = Object.getOwnPropertyDescriptor( | ||||||
| window, | ||||||
| 'Shopify', | ||||||
| ); | ||||||
| if (shopifyDescriptor && !shopifyDescriptor.configurable) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blocking: the fallback block skips two things the normal path does:
Also - the PR description says "App loads normally with warning in console" but there's no I think the polling callback should apply both overrides when it detects |
||||||
| customShopify = window.Shopify || undefined; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
| const pollForCustomerPrivacy = setInterval(() => { | ||||||
| if (window.Shopify?.customerPrivacy) { | ||||||
| setLoaded.customerPrivacy(); | ||||||
| clearInterval(pollForCustomerPrivacy); | ||||||
| } | ||||||
| }, 100); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-blocking: the interval runs indefinitely until |
||||||
| return () => clearInterval(pollForCustomerPrivacy); | ||||||
| } | ||||||
|
|
||||||
| // monitor for when window.Shopify = {} is first set | ||||||
| Object.defineProperty(window, 'Shopify', { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question / possible improvement?: Instead of Then in the catch intercept like you did above with Something like: |
||||||
| configurable: true, | ||||||
|
|
||||||
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.
question Re: line 325 - wouldn't
window.privacyBannerhave the same potential issue. If an extension defines it as non-configurable before Hydrogen initializes, theObject.definePropertyon line 325 might throw too?