forked from getsentry/sentry-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.tsx
97 lines (86 loc) · 2.67 KB
/
gatsby-ssr.tsx
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* eslint-env node */
/* eslint import/no-nodejs-modules:0 */
import React from 'react';
import {GatsbySSR} from 'gatsby';
import {PageContext} from 'sentry-docs/components/pageContext';
const sentryEnvironment = process.env.GATSBY_ENV || process.env.NODE_ENV || 'development';
const sentryLoaderUrl = process.env.SENTRY_LOADER_URL;
export const wrapPageElement: GatsbySSR['wrapPageElement'] = ({
element,
props: {pageContext},
}) => <PageContext.Provider value={pageContext}>{element}</PageContext.Provider>;
export const onPreRenderHTML: GatsbySSR['onPreRenderHTML'] = ({getHeadComponents}) => {
if (process.env.NODE_ENV !== 'production') {
return;
}
// TODO(epurkhiser): We should figure out if this is actually still necessary
getHeadComponents().forEach((el: any) => {
// Remove inline css. https://github.com/gatsbyjs/gatsby/issues/1526
if (el.type === 'style') {
el.type = 'link';
el.props.href = el.props['data-href'];
el.props.rel = 'stylesheet';
el.props.type = 'text/css';
delete el.props['data-href'];
delete el.props.dangerouslySetInnerHTML;
delete el.props.children;
}
});
};
function SentryLoaderScript() {
return (
<script key="sentry-loader-script" src={sentryLoaderUrl} crossOrigin="anonymous" />
);
}
function SentryLoaderConfig() {
return (
<script
key="sentry-loader-config"
dangerouslySetInnerHTML={{
__html: `
Sentry.onLoad(function() {
Sentry.init({
integrations: [
new Sentry.Replay({
unmask: ['.hover-card-link'],
}),
],
tracesSampleRate: ${sentryEnvironment === 'development' ? 0 : 1},
replaysSessionSampleRate: ${sentryEnvironment === 'development' ? 0 : 0.1},
replaysOnErrorSampleRate: ${sentryEnvironment === 'development' ? 0 : 1},
});
});`,
}}
/>
);
}
export const onRenderBody: GatsbySSR['onRenderBody'] = ({setHeadComponents}) => {
// Sentry SDK setup
if (sentryLoaderUrl) {
setHeadComponents([SentryLoaderScript(), SentryLoaderConfig()]);
}
setHeadComponents([
// Set up the data layer, enable privacy defaults
<script
key="gtm-data-layer"
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `
(function(w, l) {
w[l] = w[l] || [];
if(/in-app/.test(window.location.pathname)){
w[l].push({ 'disableDrift': 'true' })
}
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' })
})(window, 'dataLayer');
`,
}}
/>,
// Fetch Tag Manager
<script
key="gtm-script"
src="https://www.googletagmanager.com/gtm.js?id=GTM-N72TJRH"
async
/>,
]);
};