Skip to content

Commit

Permalink
fix: remove unnecessary stripe iframes (#37428)
Browse files Browse the repository at this point in the history
* fix: Remove stripe iframes when navigating away from /donate while still leaving two that may/may not be essential to Stripe

* Moved MutationObserver code into Header component

* fix: make MutationObserver remove old iframes


Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
  • Loading branch information
2 people authored and raisedadead committed Oct 23, 2019
1 parent 3fb5a2c commit a475ddc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client/src/components/Header/index.js
@@ -1,6 +1,7 @@
import React from 'react';
import Helmet from 'react-helmet';

import stripeObserver from './stripeIframesFix';
import UniversalNav from './components/UniversalNav';

import './header.css';
Expand All @@ -19,6 +20,10 @@ export class Header extends React.Component {

componentDidMount() {
document.addEventListener('click', this.handleClickOutside);

// Remove stacking Stripe iframes with each navigation
// after visiting /donate
stripeObserver();
}

componentWillUnmount() {
Expand Down
30 changes: 30 additions & 0 deletions client/src/components/Header/stripeIframesFix.js
@@ -0,0 +1,30 @@
const stripeObserver = () => {
const config = { attributes: false, childList: true, subtree: false };

const filterNodes = nl =>
Array.from(nl)
.filter(b => b.nodeName === 'IFRAME')
.filter(b => /__privateStripeController/.test(b.name));

const mutationCallback = a => {
const controllerAdded = a.reduce(
(acc, curr) =>
curr.type === 'childList'
? [...acc, ...filterNodes(curr.addedNodes)]
: acc,
[]
)[0];
if (controllerAdded) {
const allControllers = filterNodes(document.body.childNodes);
allControllers.forEach(controller => {
if (controller.name !== controllerAdded.name) {
controller.remove();
}
});
}
};

return new MutationObserver(mutationCallback).observe(document.body, config);
};

export default stripeObserver;
1 change: 1 addition & 0 deletions client/src/pages/donate.js
Expand Up @@ -48,6 +48,7 @@ export class DonatePage extends Component {
this.handleStripeLoad = this.handleStripeLoad.bind(this);
this.toggleOtherOptions = this.toggleOtherOptions.bind(this);
}

componentDidMount() {
if (window.Stripe) {
this.handleStripeLoad();
Expand Down

0 comments on commit a475ddc

Please sign in to comment.