-
Notifications
You must be signed in to change notification settings - Fork 158
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
User reports network failure to load Stripe.js fails again after network is restored #518
Changes from 4 commits
30157ae
072ac0a
e917010
157d06c
40a703e
7e75d1e
c24be62
ca49f7d
23dd26a
894a388
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 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -54,6 +54,23 @@ const injectScript = (params: null | LoadParams): HTMLScriptElement => { | |||||||||||||||||||||||||
return script; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
const reloadScript = (params: null | LoadParams): HTMLScriptElement => { | ||||||||||||||||||||||||||
const queryString = | ||||||||||||||||||||||||||
params && !params.advancedFraudSignals ? '?advancedFraudSignals=false' : ''; | ||||||||||||||||||||||||||
const script = [...document.getElementsByTagName('script')].filter( | ||||||||||||||||||||||||||
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. Isn't it possible that we're altering script element that the merchant added themselves here? Is that what we want to be doing? Should we only affect scripts/DOM elements that we added ourselves? Further, if this is the approach we want, why not use 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. In this case, I don't think we are since the filtering is specific to |
||||||||||||||||||||||||||
(element) => element.src === `${V3_URL}${queryString}` | ||||||||||||||||||||||||||
)[0]; | ||||||||||||||||||||||||||
const headOrBody = document.head || document.body; | ||||||||||||||||||||||||||
if (!headOrBody) { | ||||||||||||||||||||||||||
throw new Error( | ||||||||||||||||||||||||||
'Expected document.body not to be null. Stripe.js requires a <body> element.' | ||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
headOrBody.removeChild(script); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return injectScript(params); | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
const registerWrapper = (stripe: any, startTime: number): void => { | ||||||||||||||||||||||||||
if (!stripe || !stripe._registerWrapper) { | ||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||
|
@@ -96,6 +113,10 @@ export const loadScript = ( | |||||||||||||||||||||||||
console.warn(EXISTING_SCRIPT_MESSAGE); | ||||||||||||||||||||||||||
} else if (!script) { | ||||||||||||||||||||||||||
script = injectScript(params); | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
// if script exists, but we are reloading due to an error, | ||||||||||||||||||||||||||
// reload script to trigger 'load' event | ||||||||||||||||||||||||||
script = reloadScript(params); | ||||||||||||||||||||||||||
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. This is only retrying once right after the failure. Should we implement a backoff mechanism of some kind? 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. It's actually not retrying at all unless the user re-calls 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. Instead of querying the DOM for the script again, it would be simpler to remove it from the DOM and then reassign it. Here's my suggestion.
Suggested change
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
script.addEventListener('load', () => { | ||||||||||||||||||||||||||
|
@@ -115,7 +136,12 @@ export const loadScript = ( | |||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return stripePromise; | ||||||||||||||||||||||||||
// set stripePromise to null on error | ||||||||||||||||||||||||||
return stripePromise | ||||||||||||||||||||||||||
.catch((error) => { | ||||||||||||||||||||||||||
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. Isn't this catch redundant? Can it be removed? |
||||||||||||||||||||||||||
throw error; | ||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||
.then((stripePromise = null)); | ||||||||||||||||||||||||||
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. This essentially removes the cache after each successful call. We should cache success and not cache failures. Did you intend to say 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. Yeah, I'm using @fruchtose-stripe's suggestion now. 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. I recommend unsetting the
Suggested change
|
||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export const initStripe = ( | ||||||||||||||||||||||||||
|
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.
The query param logic should be shared with
injectScript
so it gets updated together.