feat: load scripts conditionally based on path (#37344)

This commit is contained in:
Ahmad Abdolsaheb
2019-10-16 22:14:53 +03:00
committed by mrugesh
parent 6ce9482b22
commit 87be96a244
2 changed files with 17 additions and 6 deletions

View File

@ -38,12 +38,6 @@ export const onRenderBody = ({ setHeadComponents, setPostBodyComponents }) => {
` `
}} }}
key='gtag-dataLayer' key='gtag-dataLayer'
/>,
<script
async={true}
id='stripe-js'
key='stripe-js'
src='https://js.stripe.com/v3/'
/> />
].filter(Boolean) ].filter(Boolean)
); );

View File

@ -7,6 +7,19 @@ import {
} from '../../src/components/layouts'; } from '../../src/components/layouts';
import FourOhFourPage from '../../src/pages/404'; import FourOhFourPage from '../../src/pages/404';
const scriptAdd = (id, key, async, src) => {
var s = document.createElement('script');
s.type = 'text/javascript';
s.id = id;
s.key = key;
s.async = async;
s.onload = function() {
console.log('Stripe injected');
};
s.src = src;
document.getElementsByTagName('head')[0].appendChild(s);
};
export default function layoutSelector({ element, props }) { export default function layoutSelector({ element, props }) {
const { const {
location: { pathname } location: { pathname }
@ -27,6 +40,10 @@ export default function layoutSelector({ element, props }) {
</DefaultLayout> </DefaultLayout>
); );
} }
if (/^\/donate(\/.*)*/.test(pathname)) {
scriptAdd('stripe-js', 'stripe-js', false, 'https://js.stripe.com/v3/');
return <DefaultLayout pathname={pathname}>{element}</DefaultLayout>;
}
return <DefaultLayout pathname={pathname}>{element}</DefaultLayout>; return <DefaultLayout pathname={pathname}>{element}</DefaultLayout>;
} }