Skip to content

Commit df8dc22

Browse files
committed
build: Use Loader Script instead of @sentry/gatsby
1 parent a0ab59e commit df8dc22

File tree

11 files changed

+120
-109
lines changed

11 files changed

+120
-109
lines changed

gatsby-ssr.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import React from 'react';
55

66
import PageContext from '~src/components/pageContext';
77

8+
const sentryEnvironment = process.env.GATSBY_ENV || process.env.NODE_ENV || 'development';
9+
const sentryDsn = process.env.GATSBY_SENTRY_DSN;
10+
811
export const wrapPageElement = ({element, props: {pageContext}}) => (
912
<PageContext.Provider value={pageContext}>{element}</PageContext.Provider>
1013
);
@@ -29,7 +32,45 @@ export const onPreRenderHTML = ({getHeadComponents}) => {
2932
});
3033
};
3134

35+
function SentryLoaderScript() {
36+
return (
37+
<script
38+
key="sentry-loader-script"
39+
src={`https://js.sentry-cdn.com/${sentryDsn}.min.js`}
40+
crossOrigin="anonymous"
41+
/>
42+
);
43+
}
44+
45+
function SentryLoaderConfig() {
46+
return (
47+
<script
48+
key="sentry-loader-config"
49+
dangerouslySetInnerHTML={{
50+
__html: `
51+
Sentry.onLoad(function() {
52+
Sentry.init({
53+
integrations: [
54+
new Sentry.Replay({
55+
unmask: ['.hover-card-link'],
56+
}),
57+
],
58+
tracesSampleRate: ${sentryEnvironment === 'development' ? 0 : 1},
59+
replaysSessionSampleRate: ${sentryEnvironment === 'development' ? 0 : 0.1},
60+
replaysOnErrorSampleRate: ${sentryEnvironment === 'development' ? 0 : 1},
61+
});
62+
});`,
63+
}}
64+
/>
65+
);
66+
}
67+
3268
export const onRenderBody = ({setHeadComponents}) => {
69+
// Sentry SDK setup
70+
if (sentryDsn) {
71+
setHeadComponents([SentryLoaderScript(), SentryLoaderConfig()]);
72+
}
73+
3374
setHeadComponents([
3475
// Set up the data layer, enable privacy defaults
3576
<script

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@mdx-js/mdx": "^1.6.18",
1818
"@mdx-js/react": "^1.6.18",
1919
"@sentry-internal/global-search": "^0.5.7",
20-
"@sentry/gatsby": "^7.46.0",
20+
"@sentry/browser": "7.51.0",
2121
"@sentry/webpack-plugin": "^1.20.0",
2222
"@types/dompurify": "^3.0.2",
2323
"@types/node": "^12",

sentry.config.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/components/basePage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {useRef} from 'react';
2-
import * as Sentry from '@sentry/gatsby';
2+
3+
import {getCurrentTransaction} from '../utils';
34

45
import Banner from './banner';
56
import CodeContext, {useCodeContextState} from './codeContext';
@@ -90,7 +91,7 @@ export default function BasePage({
9091
children,
9192
prependToc,
9293
}: Props): JSX.Element {
93-
const tx = Sentry.getCurrentHub().getScope().getTransaction();
94+
const tx = getCurrentTransaction();
9495
if (tx) {
9596
tx.setStatus('ok');
9697
}

src/components/tableOfContents.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React, {useEffect, useState} from 'react';
2-
import * as Sentry from '@sentry/gatsby';
32

43
import {PageContext} from './basePage';
54
import GuideGrid from './guideGrid';
65

6+
import {captureException} from '~src/utils';
7+
78
type Item = {
89
items: Item[];
910
title?: string;
@@ -85,7 +86,7 @@ export default function TableOfContents({contentRef, pageContext}: Props) {
8586
try {
8687
setItems(getHeadings(contentRef.current));
8788
} catch (err) {
88-
Sentry.captureException(err);
89+
captureException(err);
8990
setItems([]);
9091
}
9192
}

src/gatsby/config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ const getPlugins = () => {
8686
].filter(Boolean);
8787

8888
const plugins = [
89-
{
90-
resolve: '@sentry/gatsby',
91-
},
9289
'gatsby-plugin-sharp',
9390
'gatsby-plugin-sass',
9491
'gatsby-plugin-zeit-now',

src/globals.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
declare global {
4+
interface Window {
5+
Sentry: typeof Sentry;
6+
}
7+
}

src/pages/404.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
2-
import * as Sentry from '@sentry/gatsby';
32

43
import Layout from '../components/layout';
54
import SEO from '../components/seo';
5+
import {getCurrentTransaction} from '../utils';
66

77
function NotFoundPage() {
8-
const tx = Sentry.getCurrentHub().getScope().getTransaction();
8+
const tx = getCurrentTransaction();
99
if (tx) {
1010
tx.setStatus('not_found');
1111
}

src/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {useEffect} from 'react';
2+
import type {Transaction} from '@sentry/browser';
23
import qs from 'query-string';
34

45
type ClickOutsideCallback = (event: MouseEvent) => void;
@@ -81,3 +82,23 @@ export const marketingUrlParams = (): URLQueryObject => {
8182

8283
return marketingParams;
8384
};
85+
86+
export function getCurrentTransaction(): Transaction | undefined {
87+
try {
88+
// getCurrentHub() can actually return undefined, as we are using the Loader Script
89+
// so we guard defensively against all of these existing.
90+
return window.Sentry.getCurrentHub().getScope().getTransaction();
91+
} catch {
92+
return undefined;
93+
}
94+
}
95+
96+
export function captureException(exception: unknown): void {
97+
try {
98+
// Sentry may not be available, as we are using the Loader Script
99+
// so we guard defensively against all of these existing.
100+
window.Sentry.captureException(exception);
101+
} catch {
102+
// ignore
103+
}
104+
}

vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
{
1919
"key": "Content-Security-Policy-Report-Only",
20-
"value": "upgrade-insecure-requests; default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' googleads.g.doubleclick.net m.servedby-buysellads.com www.googletagmanager.com www.google-analytics.com; connect-src 'self' o1.ingest.sentry.io sentry.io adservice.google.com *.algolia.net *.algolianet.com *.algolia.io *.google-analytics.com analytics.google.com region1.analytics.google.com reload.getsentry.net stats.g.doubleclick.net vitals.vercel-analytics.com; img-src * 'self' data: www.google.com www.google-analytics.com www.googletagmanager.com; style-src 'self' 'unsafe-inline'; font-src 'self' fonts.gstatic.com; worker-src blob:; report-uri https://sentry.io/api/1297620/security/?sentry_key=b3cfba5788cb4c138f855c8120f70eab"
20+
"value": "upgrade-insecure-requests; default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' *.sentry-cdn.com googleads.g.doubleclick.net m.servedby-buysellads.com www.googletagmanager.com www.google-analytics.com; connect-src 'self' *.sentry.io sentry.io adservice.google.com *.algolia.net *.algolianet.com *.algolia.io *.google-analytics.com analytics.google.com region1.analytics.google.com reload.getsentry.net stats.g.doubleclick.net vitals.vercel-analytics.com; img-src * 'self' data: www.google.com www.google-analytics.com www.googletagmanager.com; style-src 'self' 'unsafe-inline'; font-src 'self' fonts.gstatic.com; worker-src blob:; report-uri https://sentry.io/api/1297620/security/?sentry_key=b3cfba5788cb4c138f855c8120f70eab"
2121
}
2222
]
2323
}

0 commit comments

Comments
 (0)