tansengming / stripe-rails

A Rails Engine for integrating with Stripe
MIT License
753 stars 123 forks source link

Rails 7 Turbo using this gem (stripe-rails) produces `Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://js.stripe.com') does not match the recipient window's origin` #223

Open jasonfb opened 1 year ago

jasonfb commented 1 year ago

Rails 7 with Turbo enabled interactions

if this gem is install and the Stripe JS is loaded at all, then all page navigations produce this JS error in the console:

(index):1 Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://js.stripe.com') does not match the recipient window's origin

(the error doesn't actually block anything)

also reported here https://github.com/stripe/react-stripe-js/issues/188

jasonfb commented 1 year ago

a fix has been documented here:

https://github.com/hotwired/turbo/issues/270#issuecomment-1068130327

// persist initial stripe iFrame DOM Object across turbo AJAX page requests
let stripeIFrameQuery = 'iframe[src^="https://js.stripe.com"]';
document.addEventListener('turbo:before-render', function (event) {
  const stripeIFrame = document.querySelector(stripeIFrameQuery);
  const newStripeIFrame = event.detail.newBody.querySelector(stripeIFrameQuery);
  if (stripeIFrame && !newStripeIFrame){
    event.detail.newBody.appendChild(stripeIFrame)
  }
});

I'd recommend either this fix be added directly to this gem or that there be some kind of 3rd gem that glues Turbo and stripe-rails together using the code above.