stripe-rails icon indicating copy to clipboard operation
stripe-rails copied to clipboard

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`

Open jasonfb opened this issue 2 years ago • 1 comments

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 avatar Jan 04 '23 16:01 jasonfb

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.

jasonfb avatar Jan 04 '23 16:01 jasonfb