Skip to content

Commit 3a9edd8

Browse files
committed
build: Use Loader Script instead of @sentry/gatsby
1 parent 9d4c3c7 commit 3a9edd8

File tree

10 files changed

+105
-108
lines changed

10 files changed

+105
-108
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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';
@@ -85,7 +84,7 @@ export default function TableOfContents({contentRef, pageContext}: Props) {
8584
try {
8685
setItems(getHeadings(contentRef.current));
8786
} catch (err) {
88-
Sentry.captureException(err);
87+
window.Sentry.captureException(err);
8988
setItems([]);
9089
}
9190
}

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: 9 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,11 @@ export const marketingUrlParams = (): URLQueryObject => {
8182

8283
return marketingParams;
8384
};
85+
86+
export function getCurrentTransaction(): Transaction | undefined {
87+
// getCurrentHub() can actually return undefined, as we are using the Loader Script
88+
// so we guard defensively against all of these existing.
89+
const hub = window.Sentry.getCurrentHub();
90+
const scope = hub && hub.getScope();
91+
return scope && scope.getTransaction();
92+
}

yarn.lock

Lines changed: 41 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,29 +2072,29 @@
20722072
htmlparser2 "^4.1.0"
20732073
title-case "^3.0.2"
20742074

2075-
"@sentry-internal/tracing@7.46.0":
2076-
version "7.46.0"
2077-
resolved "https://registry.yarnpkg.com/@sentry-internal/tracing/-/tracing-7.46.0.tgz#26febabe21a2c2cab45a3de75809d88753ec07eb"
2078-
integrity sha512-KYoppa7PPL8Er7bdPoxTNUfIY804JL7hhOEomQHYD22rLynwQ4AaLm3YEY75QWwcGb0B7ZDMV+tSumW7Rxuwuw==
2079-
dependencies:
2080-
"@sentry/core" "7.46.0"
2081-
"@sentry/types" "7.46.0"
2082-
"@sentry/utils" "7.46.0"
2075+
"@sentry-internal/tracing@7.51.0":
2076+
version "7.51.0"
2077+
resolved "https://registry.yarnpkg.com/@sentry-internal/tracing/-/tracing-7.51.0.tgz#574bd62128aa98656a847332c627663975413fda"
2078+
integrity sha512-mhXl4B02OQq6/vevjX04OchmQbxPRaLci9vTTPcPcIz/n+wkum29ze35gHcJsPJUesScjd0m19Xou3C8fNnZRA==
2079+
dependencies:
2080+
"@sentry/core" "7.51.0"
2081+
"@sentry/types" "7.51.0"
2082+
"@sentry/utils" "7.51.0"
20832083
tslib "^1.9.3"
20842084

2085-
"@sentry/browser@7.46.0":
2086-
version "7.46.0"
2087-
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.46.0.tgz#27b291ddd3c61cc1073cbbb5c48c450b438ed83c"
2088-
integrity sha512-4rX9hKPjxzfH5LhZzO5DlS5NXQ8qZg2ibepaqEgcDHrpYh5813mjjnE4OQA8wiZ6WuG3xKFgHBrGeliD5jXz9w==
2085+
"@sentry/browser@7.51.0":
2086+
version "7.51.0"
2087+
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.51.0.tgz#c132470ffa658ec5b4c0bdc44c9a72a479e6edb1"
2088+
integrity sha512-SqaXM9qhGnSqEcdWEnzHKKkCLcMzE0cAc/Y6VQOttGjkP3KRW8INdWrN7F0ySBdy6BMar6ViDJKhB6cMKsuCIg==
20892089
dependencies:
2090-
"@sentry-internal/tracing" "7.46.0"
2091-
"@sentry/core" "7.46.0"
2092-
"@sentry/replay" "7.46.0"
2093-
"@sentry/types" "7.46.0"
2094-
"@sentry/utils" "7.46.0"
2090+
"@sentry-internal/tracing" "7.51.0"
2091+
"@sentry/core" "7.51.0"
2092+
"@sentry/replay" "7.51.0"
2093+
"@sentry/types" "7.51.0"
2094+
"@sentry/utils" "7.51.0"
20952095
tslib "^1.9.3"
20962096

2097-
"@sentry/cli@^1.74.4", "@sentry/cli@^1.74.6":
2097+
"@sentry/cli@^1.74.6":
20982098
version "1.75.0"
20992099
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.75.0.tgz#4a5e71b5619cd4e9e6238cc77857c66f6b38d86a"
21002100
integrity sha512-vT8NurHy00GcN8dNqur4CMIYvFH3PaKdkX3qllVvi4syybKqjwoz+aWRCvprbYv0knweneFkLt1SmBWqazUMfA==
@@ -2106,66 +2106,37 @@
21062106
proxy-from-env "^1.1.0"
21072107
which "^2.0.2"
21082108

2109-
"@sentry/core@7.46.0":
2110-
version "7.46.0"
2111-
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.46.0.tgz#f377e556d8679f29bde1cce15b1682b6c689d6b7"
2112-
integrity sha512-BnNHGh/ZTztqQedFko7vb2u6yLs/kWesOQNivav32ZbsEpVCjcmG1gOJXh2YmGIvj3jXOC9a4xfIuh+lYFcA6A==
2109+
"@sentry/core@7.51.0":
2110+
version "7.51.0"
2111+
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.51.0.tgz#f9949d129ea2683bf70edccacdfeafb7f492260c"
2112+
integrity sha512-GgYwlXU8Y1kDEHsJO1Bmr2CNan5BzoNRR0TDBmxRgI/DgTNNSYrXeFDELgPi9/p/0XENeuttzDZ3iYd1nF7meA==
21132113
dependencies:
2114-
"@sentry/types" "7.46.0"
2115-
"@sentry/utils" "7.46.0"
2114+
"@sentry/types" "7.51.0"
2115+
"@sentry/utils" "7.51.0"
21162116
tslib "^1.9.3"
21172117

2118-
"@sentry/gatsby@^7.46.0":
2119-
version "7.46.0"
2120-
resolved "https://registry.yarnpkg.com/@sentry/gatsby/-/gatsby-7.46.0.tgz#09f495ca1c8b6a9fbbb2d37ac4fad754a42d4cc8"
2121-
integrity sha512-Ek8PN8v3zdeBQxeftzHXbZAAj4YcFk0PjxiTQA/pACHYG8uI5XuPgNjVNS2u+7kZd3gDYl4rTIypv+ztUNLMYg==
2122-
dependencies:
2123-
"@sentry/core" "7.46.0"
2124-
"@sentry/react" "7.46.0"
2125-
"@sentry/types" "7.46.0"
2126-
"@sentry/utils" "7.46.0"
2127-
"@sentry/webpack-plugin" "1.19.0"
2128-
2129-
2130-
version "7.46.0"
2131-
resolved "https://registry.yarnpkg.com/@sentry/react/-/react-7.46.0.tgz#865dbbf6d145cab1a165c573e425190a3f7d9e0c"
2132-
integrity sha512-4U7gZ5XwzCgIAH00SJe2MEjJfZq1vB4M7/YYFTjfo5geVux/c+54xgVCxZiQpCaLJBJ5NoB9Fi47RrHbxauTGA==
2133-
dependencies:
2134-
"@sentry/browser" "7.46.0"
2135-
"@sentry/types" "7.46.0"
2136-
"@sentry/utils" "7.46.0"
2137-
hoist-non-react-statics "^3.3.2"
2138-
tslib "^1.9.3"
2139-
2140-
2141-
version "7.46.0"
2142-
resolved "https://registry.yarnpkg.com/@sentry/replay/-/replay-7.46.0.tgz#c5e595d0c2d8d4db2c95d68f518510c42eb122a3"
2143-
integrity sha512-rHsAFdeEu47JRy6mEwwN+M+zTTWlOFWw9sR/eDCvik2lxAXBN2mXvf/N/MN9zQB3+QnS13ke+SvwVW7CshLOXg==
2118+
2119+
version "7.51.0"
2120+
resolved "https://registry.yarnpkg.com/@sentry/replay/-/replay-7.51.0.tgz#d37f19d9201f63094f665ed7813c1c82b3be8c30"
2121+
integrity sha512-3jv+chhhlOVFjPKYJOEJy+J+9yTUEHxybZ0tTwGZYOJp9T8HdO21L6NjYk5b9wEqJonhZHp1BiQnzG82NLDkSQ==
21442122
dependencies:
2145-
"@sentry/core" "7.46.0"
2146-
"@sentry/types" "7.46.0"
2147-
"@sentry/utils" "7.46.0"
2123+
"@sentry/core" "7.51.0"
2124+
"@sentry/types" "7.51.0"
2125+
"@sentry/utils" "7.51.0"
21482126

2149-
"@sentry/types@7.46.0":
2150-
version "7.46.0"
2151-
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.46.0.tgz#8573ba8676342c594fcfefff4552123278cfec51"
2152-
integrity sha512-2FMEMgt2h6u7AoELhNhu9L54GAh67KKfK2pJ1kEXJHmWxM9FSCkizjLs/t+49xtY7jEXr8qYq8bV967VfDPQ9g==
2127+
"@sentry/types@7.51.0":
2128+
version "7.51.0"
2129+
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.51.0.tgz#78ea083c329b29deda0a4f19cda39d89ac8c1e38"
2130+
integrity sha512-8REzzY0DslDryp6Yxj+tJ4NkXFHulLW9k8dgZV2Qo/0rBDMKir8g0IHYeN8ZBcnWrx2F+6rQb6uN6BjyLZY7Dg==
21532131

2154-
"@sentry/utils@7.46.0":
2155-
version "7.46.0"
2156-
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.46.0.tgz#7a713724db3d1c8bc0aef6d19a7fe2c76db0bdf2"
2157-
integrity sha512-elRezDAF84guMG0OVIIZEWm6wUpgbda4HGks98CFnPsrnMm3N1bdBI9XdlxYLtf+ir5KsGR5YlEIf/a0kRUwAQ==
2132+
"@sentry/utils@7.51.0":
2133+
version "7.51.0"
2134+
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.51.0.tgz#5720437fd319a676bd634a90189a636719894cdb"
2135+
integrity sha512-y5zq4IfZDCm6cg0EQJMghUM4YjZToFni7J5OKopLXKVtc9YtRtkYoFuFqEWm4HBuBwplreiS/KkDQgWn3FVn7A==
21582136
dependencies:
2159-
"@sentry/types" "7.46.0"
2137+
"@sentry/types" "7.51.0"
21602138
tslib "^1.9.3"
21612139

2162-
2163-
version "1.19.0"
2164-
resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-1.19.0.tgz#2b134318f1552ba7f3e3f9c83c71a202095f7a44"
2165-
integrity sha512-qSpdgdGMtdzagGveSWgo2b+t8PdPUscuOjbOyWCsJme9jlTFnNk0rX7JEA55OUozikKHM/+vVh08USLBnPboZw==
2166-
dependencies:
2167-
"@sentry/cli" "^1.74.4"
2168-
21692140
"@sentry/webpack-plugin@^1.20.0":
21702141
version "1.20.0"
21712142
resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-1.20.0.tgz#e7add76122708fb6b4ee7951294b521019720e58"
@@ -8697,13 +8668,6 @@ hmac-drbg@^1.0.1:
86978668
minimalistic-assert "^1.0.0"
86988669
minimalistic-crypto-utils "^1.0.1"
86998670

8700-
hoist-non-react-statics@^3.3.2:
8701-
version "3.3.2"
8702-
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
8703-
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
8704-
dependencies:
8705-
react-is "^16.7.0"
8706-
87078671
hosted-git-info@^2.1.4, hosted-git-info@^2.5.0:
87088672
version "2.8.9"
87098673
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
@@ -12922,7 +12886,7 @@ react-input-autosize@^2.2.2:
1292212886
dependencies:
1292312887
prop-types "^15.5.8"
1292412888

12925-
react-is@^16.12.0, react-is@^16.13.1, react-is@^16.3.2, react-is@^16.7.0, react-is@^16.8.6:
12889+
react-is@^16.12.0, react-is@^16.13.1, react-is@^16.3.2, react-is@^16.8.6:
1292612890
version "16.13.1"
1292712891
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
1292812892
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

0 commit comments

Comments
 (0)