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

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
}

yarn.lock

Lines changed: 41 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,29 +2443,29 @@
24432443
htmlparser2 "^4.1.0"
24442444
title-case "^3.0.2"
24452445

2446-
"@sentry-internal/tracing@7.46.0":
2447-
version "7.46.0"
2448-
resolved "https://registry.yarnpkg.com/@sentry-internal/tracing/-/tracing-7.46.0.tgz#26febabe21a2c2cab45a3de75809d88753ec07eb"
2449-
integrity sha512-KYoppa7PPL8Er7bdPoxTNUfIY804JL7hhOEomQHYD22rLynwQ4AaLm3YEY75QWwcGb0B7ZDMV+tSumW7Rxuwuw==
2450-
dependencies:
2451-
"@sentry/core" "7.46.0"
2452-
"@sentry/types" "7.46.0"
2453-
"@sentry/utils" "7.46.0"
2446+
"@sentry-internal/tracing@7.51.2":
2447+
version "7.51.2"
2448+
resolved "https://registry.yarnpkg.com/@sentry-internal/tracing/-/tracing-7.51.2.tgz#17833047646426ca71445327018ffcb33506a699"
2449+
integrity sha512-OBNZn7C4CyocmlSMUPfkY9ORgab346vTHu5kX35PgW5XR51VD2nO5iJCFbyFcsmmRWyCJcZzwMNARouc2V4V8A==
2450+
dependencies:
2451+
"@sentry/core" "7.51.2"
2452+
"@sentry/types" "7.51.2"
2453+
"@sentry/utils" "7.51.2"
24542454
tslib "^1.9.3"
24552455

2456-
"@sentry/browser@7.46.0":
2457-
version "7.46.0"
2458-
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.46.0.tgz#27b291ddd3c61cc1073cbbb5c48c450b438ed83c"
2459-
integrity sha512-4rX9hKPjxzfH5LhZzO5DlS5NXQ8qZg2ibepaqEgcDHrpYh5813mjjnE4OQA8wiZ6WuG3xKFgHBrGeliD5jXz9w==
2456+
"@sentry/browser@7.51.2":
2457+
version "7.51.2"
2458+
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.51.2.tgz#c01758a54c613be45df58ab503805737256f51a4"
2459+
integrity sha512-FQFEaTFbvYHPQE2emFjNoGSy+jXplwzoM/XEUBRjrGo62lf8BhMvWnPeG3H3UWPgrWA1mq0amvHRwXUkwofk0g==
24602460
dependencies:
2461-
"@sentry-internal/tracing" "7.46.0"
2462-
"@sentry/core" "7.46.0"
2463-
"@sentry/replay" "7.46.0"
2464-
"@sentry/types" "7.46.0"
2465-
"@sentry/utils" "7.46.0"
2461+
"@sentry-internal/tracing" "7.51.2"
2462+
"@sentry/core" "7.51.2"
2463+
"@sentry/replay" "7.51.2"
2464+
"@sentry/types" "7.51.2"
2465+
"@sentry/utils" "7.51.2"
24662466
tslib "^1.9.3"
24672467

2468-
"@sentry/cli@^1.74.4", "@sentry/cli@^1.74.6":
2468+
"@sentry/cli@^1.74.6":
24692469
version "1.75.0"
24702470
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-1.75.0.tgz#4a5e71b5619cd4e9e6238cc77857c66f6b38d86a"
24712471
integrity sha512-vT8NurHy00GcN8dNqur4CMIYvFH3PaKdkX3qllVvi4syybKqjwoz+aWRCvprbYv0knweneFkLt1SmBWqazUMfA==
@@ -2477,66 +2477,37 @@
24772477
proxy-from-env "^1.1.0"
24782478
which "^2.0.2"
24792479

2480-
"@sentry/core@7.46.0":
2481-
version "7.46.0"
2482-
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.46.0.tgz#f377e556d8679f29bde1cce15b1682b6c689d6b7"
2483-
integrity sha512-BnNHGh/ZTztqQedFko7vb2u6yLs/kWesOQNivav32ZbsEpVCjcmG1gOJXh2YmGIvj3jXOC9a4xfIuh+lYFcA6A==
2480+
"@sentry/core@7.51.2":
2481+
version "7.51.2"
2482+
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.51.2.tgz#f2c938de334f9bf26f4416079168275832423964"
2483+
integrity sha512-p8ZiSBxpKe+rkXDMEcgmdoyIHM/1bhpINLZUFPiFH8vzomEr7sgnwRhyrU8y/ADnkPeNg/2YF3QpDpk0OgZJUA==
24842484
dependencies:
2485-
"@sentry/types" "7.46.0"
2486-
"@sentry/utils" "7.46.0"
2485+
"@sentry/types" "7.51.2"
2486+
"@sentry/utils" "7.51.2"
24872487
tslib "^1.9.3"
24882488

2489-
"@sentry/gatsby@^7.46.0":
2490-
version "7.46.0"
2491-
resolved "https://registry.yarnpkg.com/@sentry/gatsby/-/gatsby-7.46.0.tgz#09f495ca1c8b6a9fbbb2d37ac4fad754a42d4cc8"
2492-
integrity sha512-Ek8PN8v3zdeBQxeftzHXbZAAj4YcFk0PjxiTQA/pACHYG8uI5XuPgNjVNS2u+7kZd3gDYl4rTIypv+ztUNLMYg==
2493-
dependencies:
2494-
"@sentry/core" "7.46.0"
2495-
"@sentry/react" "7.46.0"
2496-
"@sentry/types" "7.46.0"
2497-
"@sentry/utils" "7.46.0"
2498-
"@sentry/webpack-plugin" "1.19.0"
2499-
2500-
2501-
version "7.46.0"
2502-
resolved "https://registry.yarnpkg.com/@sentry/react/-/react-7.46.0.tgz#865dbbf6d145cab1a165c573e425190a3f7d9e0c"
2503-
integrity sha512-4U7gZ5XwzCgIAH00SJe2MEjJfZq1vB4M7/YYFTjfo5geVux/c+54xgVCxZiQpCaLJBJ5NoB9Fi47RrHbxauTGA==
2504-
dependencies:
2505-
"@sentry/browser" "7.46.0"
2506-
"@sentry/types" "7.46.0"
2507-
"@sentry/utils" "7.46.0"
2508-
hoist-non-react-statics "^3.3.2"
2509-
tslib "^1.9.3"
2510-
2511-
2512-
version "7.46.0"
2513-
resolved "https://registry.yarnpkg.com/@sentry/replay/-/replay-7.46.0.tgz#c5e595d0c2d8d4db2c95d68f518510c42eb122a3"
2514-
integrity sha512-rHsAFdeEu47JRy6mEwwN+M+zTTWlOFWw9sR/eDCvik2lxAXBN2mXvf/N/MN9zQB3+QnS13ke+SvwVW7CshLOXg==
2489+
2490+
version "7.51.2"
2491+
resolved "https://registry.yarnpkg.com/@sentry/replay/-/replay-7.51.2.tgz#1f54e92b472ab87dfdb4e8cd6b8c8252600fe7b0"
2492+
integrity sha512-W8YnSxkK9LTUXDaYciM7Hn87u57AX9qvH8jGcxZZnvpKqHlDXOpSV8LRtBkARsTwgLgswROImSifY0ic0lyCWg==
25152493
dependencies:
2516-
"@sentry/core" "7.46.0"
2517-
"@sentry/types" "7.46.0"
2518-
"@sentry/utils" "7.46.0"
2494+
"@sentry/core" "7.51.2"
2495+
"@sentry/types" "7.51.2"
2496+
"@sentry/utils" "7.51.2"
25192497

2520-
"@sentry/types@7.46.0":
2521-
version "7.46.0"
2522-
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.46.0.tgz#8573ba8676342c594fcfefff4552123278cfec51"
2523-
integrity sha512-2FMEMgt2h6u7AoELhNhu9L54GAh67KKfK2pJ1kEXJHmWxM9FSCkizjLs/t+49xtY7jEXr8qYq8bV967VfDPQ9g==
2498+
"@sentry/types@7.51.2":
2499+
version "7.51.2"
2500+
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.51.2.tgz#cb742f374d9549195f62c462c915adeafed31d65"
2501+
integrity sha512-/hLnZVrcK7G5BQoD/60u9Qak8c9AvwV8za8TtYPJDUeW59GrqnqOkFji7RVhI7oH1OX4iBxV+9pAKzfYE6A6SA==
25242502

2525-
"@sentry/utils@7.46.0":
2526-
version "7.46.0"
2527-
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.46.0.tgz#7a713724db3d1c8bc0aef6d19a7fe2c76db0bdf2"
2528-
integrity sha512-elRezDAF84guMG0OVIIZEWm6wUpgbda4HGks98CFnPsrnMm3N1bdBI9XdlxYLtf+ir5KsGR5YlEIf/a0kRUwAQ==
2503+
"@sentry/utils@7.51.2":
2504+
version "7.51.2"
2505+
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.51.2.tgz#2a52ac2cfb00ffd128248981279c0a561b39eccb"
2506+
integrity sha512-EcjBU7qG4IG+DpIPvdgIBcdIofROMawKoRUNKraeKzH/waEYH9DzCaqp/mzc5/rPBhpDB4BShX9xDDSeH+8c0A==
25292507
dependencies:
2530-
"@sentry/types" "7.46.0"
2508+
"@sentry/types" "7.51.2"
25312509
tslib "^1.9.3"
25322510

2533-
2534-
version "1.19.0"
2535-
resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-1.19.0.tgz#2b134318f1552ba7f3e3f9c83c71a202095f7a44"
2536-
integrity sha512-qSpdgdGMtdzagGveSWgo2b+t8PdPUscuOjbOyWCsJme9jlTFnNk0rX7JEA55OUozikKHM/+vVh08USLBnPboZw==
2537-
dependencies:
2538-
"@sentry/cli" "^1.74.4"
2539-
25402511
"@sentry/webpack-plugin@^1.20.0":
25412512
version "1.20.0"
25422513
resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-1.20.0.tgz#e7add76122708fb6b4ee7951294b521019720e58"
@@ -8970,13 +8941,6 @@ hmac-drbg@^1.0.1:
89708941
minimalistic-assert "^1.0.0"
89718942
minimalistic-crypto-utils "^1.0.1"
89728943

8973-
hoist-non-react-statics@^3.3.2:
8974-
version "3.3.2"
8975-
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
8976-
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
8977-
dependencies:
8978-
react-is "^16.7.0"
8979-
89808944
hosted-git-info@^2.1.4, hosted-git-info@^2.5.0:
89818945
version "2.8.9"
89828946
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
@@ -13326,7 +13290,7 @@ react-input-autosize@^2.2.2:
1332613290
dependencies:
1332713291
prop-types "^15.5.8"
1332813292

13329-
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:
13293+
react-is@^16.12.0, react-is@^16.13.1, react-is@^16.3.2, react-is@^16.8.6:
1333013294
version "16.13.1"
1333113295
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
1333213296
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

0 commit comments

Comments
 (0)