Skip to content

Commit 7e12d0c

Browse files
author
Luca Forstner
authored
ref: Use JS SDK loader (undoing #6916) (#6949)
1 parent 6e400cc commit 7e12d0c

File tree

12 files changed

+119
-110
lines changed

12 files changed

+119
-110
lines changed

gatsby-ssr.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
/* eslint import/no-nodejs-modules:0 */
33

44
import React from 'react';
5+
56
import PageContext from 'sentry-docs/components/pageContext';
67

8+
const sentryEnvironment = process.env.GATSBY_ENV || process.env.NODE_ENV || 'development';
9+
const sentryLoaderUrl = process.env.SENTRY_LOADER_URL;
10+
711
export const wrapPageElement = ({element, props: {pageContext}}) => (
812
<PageContext.Provider value={pageContext}>{element}</PageContext.Provider>
913
);
@@ -28,7 +32,41 @@ export const onPreRenderHTML = ({getHeadComponents}) => {
2832
});
2933
};
3034

35+
function SentryLoaderScript() {
36+
return (
37+
<script key="sentry-loader-script" src={sentryLoaderUrl} crossOrigin="anonymous" />
38+
);
39+
}
40+
41+
function SentryLoaderConfig() {
42+
return (
43+
<script
44+
key="sentry-loader-config"
45+
dangerouslySetInnerHTML={{
46+
__html: `
47+
Sentry.onLoad(function() {
48+
Sentry.init({
49+
integrations: [
50+
new Sentry.Replay({
51+
unmask: ['.hover-card-link'],
52+
}),
53+
],
54+
tracesSampleRate: ${sentryEnvironment === 'development' ? 0 : 1},
55+
replaysSessionSampleRate: ${sentryEnvironment === 'development' ? 0 : 0.1},
56+
replaysOnErrorSampleRate: ${sentryEnvironment === 'development' ? 0 : 1},
57+
});
58+
});`,
59+
}}
60+
/>
61+
);
62+
}
63+
3164
export const onRenderBody = ({setHeadComponents}) => {
65+
// Sentry SDK setup
66+
if (sentryLoaderUrl) {
67+
setHeadComponents([SentryLoaderScript(), SentryLoaderConfig()]);
68+
}
69+
3270
setHeadComponents([
3371
// Set up the data layer, enable privacy defaults
3472
<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.2",
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';
@@ -56,7 +57,7 @@ export default function BasePage({
5657
children,
5758
prependToc,
5859
}: Props): JSX.Element {
59-
const tx = Sentry.getCurrentHub().getScope().getTransaction();
60+
const tx = getCurrentTransaction();
6061
if (tx) {
6162
tx.setStatus('ok');
6263
}

src/components/tableOfContents.tsx

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

45
import {PageContext} from './basePage';
56
import GuideGrid from './guideGrid';
@@ -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/gatsby/onCreateWebpackConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import path from 'path';
66
import SentryWebpackPlugin from '@sentry/webpack-plugin';
77

88
const getPlugins = reporter => {
9-
const authToken = process.env.SENTRY_AUTH_TOKEN;
9+
const authToken = process.env.SENTRY_WEBPACK_PLUGIN_AUTH_TOKEN;
1010
if (!authToken) {
1111
reporter.warn('SENTRY_AUTH_TOKEN is not set - will not upload source maps');
1212
return [];

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
2-
import * as Sentry from '@sentry/gatsby';
32

43
import Layout from 'sentry-docs/components/layout';
54
import SEO from 'sentry-docs/components/seo';
65

6+
import {getCurrentTransaction} from '../utils';
7+
78
function NotFoundPage() {
8-
const tx = Sentry.getCurrentHub().getScope().getTransaction();
9+
const tx = getCurrentTransaction();
910
if (tx) {
1011
tx.setStatus('not_found');
1112
}

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+
}

0 commit comments

Comments
 (0)