Skip to content

Commit

Permalink
Fix the setConnector race condition in next.js strictmode (#105) (#106
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jorgea-stripe authored Apr 2, 2024
1 parent 38065b6 commit faa3c70
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ export const initStripeConnect = (
}

stripeConnectInstance.then(instance => {
if (!element.isConnected && !(element as any).setConnector) {
// If the element is not connected to the DOM and the `setConnector` method is not
// defined, this indicates the element was created before connect.js was loaded, and has
// not been transformed into a custom element yet

// To load the custom element code on it, we need to connect and disconnect it to the DOM
// This isn't a problem, as the element will be invisible, and we know the element is already
// not currently connected to the DOM

const oldDisplay = element.style.display;
element.style.display = "none";
document.body.appendChild(element);
document.body.removeChild(element);
element.style.display = oldDisplay;
}

(element as any).setConnector((instance as any).connect);
});

Expand Down

0 comments on commit faa3c70

Please sign in to comment.