forked from newrelic/developer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.js
72 lines (63 loc) · 2.17 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Implement Gatsby's Browser APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/browser-apis/
*/
import wrapPageElement from './gatsby/wrap-page-element';
const onInitialClientRender = () => {
function destyleMktoForm(mktoForm, options) {
const formEl = mktoForm.getFormElem()[0];
const arrayFrom = Function.prototype.call.bind(Array.prototype.slice);
options = options || {};
// remove element styles from <form> and children
if (!options.keepInline) {
const styledEls = arrayFrom(formEl.querySelectorAll('[style]')).concat(
formEl
);
styledEls.forEach(function (el) {
el.removeAttribute('style');
});
}
// disable remote stylesheets and local styles
if (!options.keepSheets) {
const styleSheets = arrayFrom(document.styleSheets);
styleSheets.forEach(function (ss) {
if (
// eslint-disable-next-line no-undef
[mktoForms2BaseStyle, mktoForms2ThemeStyle].indexOf(ss.ownerNode) !=
-1 ||
formEl.contains(ss.ownerNode)
) {
ss.disabled = true;
}
});
}
if (!options.moreStyles) {
formEl.setAttribute('data-styles-ready', 'true');
}
}
if (typeof window !== 'undefined' && window.MktoForms2) {
// eslint-disable-next-line no-undef
MktoForms2.whenRendered(function (form) {
destyleMktoForm(form);
});
}
};
const onRouteUpdate = ({ location }) => {
if (process.env.NODE_ENV !== `production` || typeof ga !== `function`) {
return null;
}
// wrap inside a timeout to make sure react-helmet is done with it's changes (https://github.com/gatsbyjs/gatsby/issues/9139)
// reactHelmet is using requestAnimationFrame: https://github.com/nfl/react-helmet/blob/5.2.0/src/HelmetUtils.js#L296-L299
const sendPageView = () => {
const pagePath = location
? location.pathname + location.search + location.hash
: undefined;
window.ga(`set`, `page`, pagePath);
window.ga(`send`, `pageview`);
};
// Minimum delay for reactHelmet's requestAnimationFrame
setTimeout(sendPageView, 32);
return null;
};
export { onRouteUpdate, wrapPageElement, onInitialClientRender };