-
Notifications
You must be signed in to change notification settings - Fork 131
Fix: Enhance loadCoreSdkScript #845
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 2 commits
dc861dd
fc8d478
916d386
e1cfe65
47a755e
6325d5c
6093419
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,5 @@ | ||
| --- | ||
| "@paypal/paypal-js": patch | ||
| --- | ||
|
|
||
| Fixes an issue where loadCoreSdkScript would load 2 core scripts. |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ function loadCoreSdkScript(options: LoadCoreSdkScriptOptions = {}) { | |||||||||||||
| validateArguments(options); | ||||||||||||||
| const isServerEnv = isServer(); | ||||||||||||||
|
|
||||||||||||||
| // Early resolve in SSR environments where DOM APIs are unavailable | ||||||||||||||
| if (isServerEnv) { | ||||||||||||||
| return Promise.resolve(null); | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -18,10 +19,41 @@ function loadCoreSdkScript(options: LoadCoreSdkScriptOptions = {}) { | |||||||||||||
| 'script[src*="/web-sdk/v6/core"]', | ||||||||||||||
| ); | ||||||||||||||
|
|
||||||||||||||
| if (window.paypal?.version.startsWith("6") && currentScript) { | ||||||||||||||
| // Script already loaded and namespace is available — return immediately | ||||||||||||||
| if (window.paypal?.version?.startsWith("6") && currentScript) { | ||||||||||||||
| return Promise.resolve(window.paypal as unknown as PayPalV6Namespace); | ||||||||||||||
|
||||||||||||||
| return Promise.resolve(window.paypal as unknown as PayPalV6Namespace); | |
| return Promise.resolve(window[windowNamespace] as unknown as PayPalV6Namespace); |
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.
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.
Do we need to add some tests for this situation? since this impacts the load logic 🤔
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.
Yea great point, I will push some 👍
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.
If we go this route, we should look into sharing these callbacks with what is passed into insertScriptElement with onSuccess and onError. That function uses these same event listeners under the hood
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.
Ok i'll add this to my near-term ToDos
Outdated
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.
This error message seems a bit vague to me. Can we make it more specific? :)
| reject(new Error(`The PayPal SDK script failed to load.`)); | |
| reject(new Error( | |
| `The script "${currentScript.src}" failed to load. | |
| Check the HTTP status code and response body in DevTools to | |
| learn more.` | |
| )); |

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.
Does this need to take into account the custom namespace option?
Uh oh!
There was an error while loading. Please reload this page.
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.
Great catch! Same doubt here. But don't we always load at window.paypal internally? 🤔 Correct me if I am wrong :)
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.
In V6 we use
window__paypal_sdk__as the internal namespace. Here's an example when usingdata-namespace="paypalV6":Ex:
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.
Thanks for the clarification! TIL