Skip to content

Commit a4857f7

Browse files
docs(js): Update content in SvelteKit's manual quick start guide (source maps, csp) (#13843)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR In this PR I have: - reduced the source maps content in SvelteKit's manual quick start guide and moved it to the Source Maps page - moved content about CSP to the Troubleshooting page and added an Expandable to the manual quick start guide I will move the build options and API content in a new PR (or two) to keep things focused. Belongs to: #13634 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/)
1 parent ee8679d commit a4857f7

File tree

5 files changed

+189
-107
lines changed

5 files changed

+189
-107
lines changed

docs/platforms/javascript/common/troubleshooting/index.mdx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,63 @@ Most community CDNs properly set an `Access-Control-Allow-Origin` header.
9393

9494
</PlatformCategorySection>
9595

96+
<PlatformSection supported={["javascript.sveltekit"]}>
97+
98+
<Expandable permalink title="Configure CSP for Client-side `fetch` Instrumentation">
99+
100+
If you're using a SvelteKit version older than `sveltejs/[email protected]`, the Sentry SDK injects an inline `<script>` tag into the HTML response of the server.
101+
This script proxies all client-side `fetch` calls so that `fetch` calls inside your `load` functions are captured by the SDK.
102+
However, if you configured CSP rules to block inline fetch scripts by default, this script will be [blocked by the browser](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_inline_script).
103+
104+
You have multiple options to solve this CSP issue:
105+
106+
#### Option 1: Upgrade SvelteKit
107+
108+
The easiest option is to update `@sveltejs/kit` to at least version `2.16.0` (or newer). The SDK will not inject the fetch proxy script as it's no longer necessary.
109+
110+
#### Option 2: Add Script Hash to CSP Policy
111+
112+
To enable the script, add an exception for the `sentryHandle` script by adding the hash of the script to your CSP script source rules.
113+
114+
If your CSP is defined in `svelte.config.js`, you can add the hash to the `csp.directives.script-src` array:
115+
116+
```javascript {filename:svelte.config.js} {5-7}
117+
const config = {
118+
kit: {
119+
csp: {
120+
directives: {
121+
"script-src": ["sha256-y2WkUILyE4eycy7x+pC0z99aZjTZlWfVwgUAfNc1sY8="], // + rest of your values
122+
},
123+
},
124+
},
125+
};
126+
```
127+
128+
For external CSP configurations, add the following hash to your `script-src` directive:
129+
130+
```txt
131+
'sha256-y2WkUILyE4eycy7x+pC0z99aZjTZlWfVwgUAfNc1sY8='
132+
```
133+
134+
<Alert>
135+
We will not change this script any time soon, so the hash will stay
136+
consistent.
137+
</Alert>
138+
139+
#### Option 3: Disable the `fetch` Proxy Script
140+
141+
If you don't want to inject the script responsible for instrumenting client-side `fetch` calls, you can disable injection by passing `injectFetchProxyScript: false` to `sentryHandle`:
142+
143+
```javascript {filename:hooks.server.(js|ts)}
144+
export const handle = sentryHandle({ injectFetchProxyScript: false });
145+
```
146+
147+
Note that if you disable the `fetch` proxy script, the SDK will not be able to capture spans for `fetch` calls made by SvelteKit when your route has a server load function. This also means that potential spans created on the server for these `fetch` calls will not be connected to the client-side trace.
148+
149+
</Expandable>
150+
151+
</PlatformSection>
152+
96153
<PlatformCategorySection supported={['browser']}>
97154

98155
<Expandable permalink title="Unexpected OPTIONS request">

docs/platforms/javascript/guides/sveltekit/manual-setup.mdx

Lines changed: 21 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ export const handle = Sentry.sentryHandle();
124124
// export const handle = sequence(Sentry.sentryHandle(), yourHandler());
125125
```
126126

127+
<Expandable title="Having CSP issues with `fetch` instrumentation on older SvelteKit versions?">
128+
129+
If you're using SvelteKit versions older than `2.16.0` and encounter Content Security Policy (CSP) errors related to Sentry's `fetch` instrumentation, you can find support in our [Troubleshooting guide](/platforms/javascript/guides/sveltekit/troubleshooting/#configure-csp-for-client-side-fetch-instrumentation).
130+
131+
</Expandable>
132+
127133
### Configure Vite
128134

129135
Add the `sentrySvelteKit` plugin **before** `sveltekit` in your `vite.config.(js|ts)` file to automatically upload source maps to Sentry and instrument `load` functions for tracing if it's configured.
@@ -139,28 +145,11 @@ export default defineConfig({
139145
});
140146
```
141147

142-
### Source Maps Upload
143-
144-
By default, `sentrySvelteKit()` will add an instance of the [Sentry Vite Plugin](https://www.npmjs.com/package/@sentry/vite-plugin), to upload source maps for both server and client builds. This means that when you run a production build (`npm run build`), source maps will be generated and uploaded to Sentry, so that you get readable stack traces in your Sentry issues.
145-
146-
However, you still need to specify your Sentry auth token as well as your org and project slugs. There are two ways to set them.
147-
148-
You can set them as environment variables, for example in a `.env` file:
149-
150-
- `SENTRY_ORG` your Sentry org slug
151-
- `SENTRY_PROJECT` your Sentry project slug
152-
- `SENTRY_AUTH_TOKEN` your Sentry auth token (can be obtained from the [Organization Token Settings](https://sentry.io/orgredirect/organizations/:orgslug/settings/auth-tokens/))
153-
- `SENTRY_URL` your Sentry instance URL. This is only required if you use your own Sentry instance (as opposed to `https://sentry.io`).
148+
## Step 3: Add Readable Stack Traces With Source Maps (Optional)
154149

155-
Or, you can set them by passing a `sourceMapsUploadOptions` object to `sentrySvelteKit`, as seen in the example below. All available options are documented at [the Sentry Vite plugin repo](https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#options).
150+
To upload source maps for clear error stack traces, add your Sentry auth token, organization, and project slug in your `vite.config.(js|ts)` file:
156151

157-
<OrgAuthTokenNote />
158-
159-
```bash {filename:.env}
160-
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
161-
```
162-
163-
```javascript {filename:vite.config.(js|ts)} {7-16}
152+
```javascript {filename:vite.config.(js|ts)} {6-11}
164153
import { sveltekit } from "@sveltejs/kit/vite";
165154
import { sentrySvelteKit } from "@sentry/sveltekit";
166155

@@ -170,12 +159,8 @@ export default {
170159
sourceMapsUploadOptions: {
171160
org: "___ORG_SLUG___",
172161
project: "___PROJECT_SLUG___",
162+
// store your auth token in an environment variable
173163
authToken: process.env.SENTRY_AUTH_TOKEN,
174-
sourcemaps: {
175-
assets: ["./build/*/**/*"],
176-
ignore: ["**/build/client/**/*"],
177-
filesToDeleteAfterUpload: ["./build/**/*.map"],
178-
},
179164
},
180165
}),
181166
sveltekit(),
@@ -184,31 +169,17 @@ export default {
184169
};
185170
```
186171

187-
Using the `sourceMapsUploadOptions` object is useful if the default source maps upload doesn't work out of the box, for instance, if you have a customized build setup or if you're using the SDK with a SvelteKit adapter other than the [supported adapters](../#compatibility).
188-
189-
#### Overriding SvelteKit Adapter detection
172+
To keep your auth token secure, always store it in an environment variable instead of directly in your files:
190173

191-
By default, `sentrySvelteKit` will try to detect your SvelteKit adapter to configure source maps upload correctly.
192-
If you're not using one of the [supported adapters](../#compatibility) or the wrong one is detected, you can override the adapter detection by passing the `adapter` option to `sentrySvelteKit`:
193-
194-
```javascript {filename:vite.config.(js|ts)} {7}
195-
import { sveltekit } from "@sveltejs/kit/vite";
196-
import { sentrySvelteKit } from "@sentry/sveltekit";
174+
<OrgAuthTokenNote />
197175

198-
export default {
199-
plugins: [
200-
sentrySvelteKit({
201-
adapter: "vercel",
202-
}),
203-
sveltekit(),
204-
],
205-
// ... rest of your Vite config
206-
};
176+
```bash {filename:.env}
177+
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
207178
```
208179

209-
#### Disable Source Maps Upload
180+
### Override Adapter Detection
210181

211-
You can disable automatic source maps upload in your Vite config:
182+
`sentrySvelteKit` tries to auto-detect your SvelteKit adapter to configure source maps upload correctly. If you're using an unsupported adapter or the wrong one is detected, set it using the `adapter` option:
212183

213184
```javascript {filename:vite.config.(js|ts)} {7}
214185
import { sveltekit } from "@sveltejs/kit/vite";
@@ -217,17 +188,15 @@ import { sentrySvelteKit } from "@sentry/sveltekit";
217188
export default {
218189
plugins: [
219190
sentrySvelteKit({
220-
autoUploadSourceMaps: false,
191+
adapter: "vercel",
221192
}),
222193
sveltekit(),
223194
],
224195
// ... rest of your Vite config
225196
};
226197
```
227198

228-
If you disable automatic source maps upload, you must explicitly set a `release` value in your `Sentry.init()` configs. You can also skip the `sentry-cli` configuration step below.
229-
230-
## Step 3: Optional Configuration
199+
## Step 4: Optional Configuration
231200

232201
The points explain additional optional configuration or more in-depth customization of your Sentry SvelteKit SDK setup.
233202

@@ -285,60 +254,6 @@ export default {
285254
};
286255
```
287256

288-
### Configure CSP for Client-side `fetch` Instrumentation
289-
290-
<Alert>
291-
292-
Only relevant for SvelteKit versions below `@sveltejs/[email protected]`
293-
294-
</Alert>
295-
296-
If you're using a SvelteKit version older than `sveltejs/[email protected]`, The Sentry SDK injects an inline `<script>` tag into the HTML response of the server.
297-
This script proxies all client-side `fetch` calls, so that `fetch` calls inside your `load` functions are captured by the SDK.
298-
However, if you configured CSP rules to block inline fetch scripts by default, this script will be [blocked by the browser](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_inline_script).
299-
300-
You have multiple options to solve this CSP issue:
301-
302-
#### Upgrade SvelteKit
303-
304-
The easiest option is to update `@sveltejs/kit` to at least version `2.16.0` (or newer). The SDK will not inject the fetch proxy script as it's no longer necessary.
305-
306-
#### Add Script Hash to CSP policy
307-
308-
To enable the script, add an exception for the `sentryHandle` script by adding the hash of the script to your CSP script source rules.
309-
310-
If your CSP is defined in `svelte.config.js`, you can add the hash to the `csp.directives.script-src` array:
311-
312-
```javascript {filename:svelte.config.js} {5-7}
313-
const config = {
314-
kit: {
315-
csp: {
316-
directives: {
317-
"script-src": ["sha256-y2WkUILyE4eycy7x+pC0z99aZjTZlWfVwgUAfNc1sY8="], // + rest of your values
318-
},
319-
},
320-
},
321-
};
322-
```
323-
324-
For external CSP configurations, add the following hash to your `script-src` directive:
325-
326-
```txt
327-
'sha256-y2WkUILyE4eycy7x+pC0z99aZjTZlWfVwgUAfNc1sY8='
328-
```
329-
330-
We will not make changes to this script any time soon, so the hash will stay consistent.
331-
332-
#### Disable `fetch` Proxy Script
333-
334-
If you don't want to inject the script responsible for instrumenting client-side `fetch` calls, you can disable injection by passing `injectFetchProxyScript: false` to `sentryHandle`:
335-
336-
```javascript {filename:hooks.server.(js|ts)}
337-
export const handle = sentryHandle({ injectFetchProxyScript: false });
338-
```
339-
340-
Note that if you disable the `fetch` proxy script, the SDK will not be able to capture spans for `fetch` calls made by SvelteKit when your route has a server load function. This also means that potential spans created on the server for these `fetch` calls will not be connected to the client-side trace.
341-
342257
### Manual Instrumentation
343258

344259
Instead or in addition to [Auto Instrumentation](#auto-instrumentation), you can manually instrument certain SvelteKit-specific features with the SDK:
@@ -396,16 +311,16 @@ export const GET = wrapServerRouteWithSentry(async () => {
396311
});
397312
```
398313

399-
## Step 4: Cloudflare Pages Configuration (Optional)
314+
## Step 5: Cloudflare Pages Configuration (Optional)
400315

401316
If you're deploying your application to Cloudflare Pages, you need to adjust your server-side setup.
402317
Follow this guide to [configure Sentry for Cloudflare](/platforms/javascript/guides/cloudflare/frameworks/sveltekit/).
403318

404-
## Step 5: Avoid Ad Blockers With Tunneling (Optional)
319+
## Step 6: Avoid Ad Blockers With Tunneling (Optional)
405320

406321
<PlatformContent includePath="getting-started-tunneling" />
407322

408-
## Step 6: Verify Your Setup
323+
## Step 7: Verify Your Setup
409324

410325
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
411326

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Source Maps
3+
sidebar_order: 3
4+
description: "Upload your source maps to Sentry to enable readable stack traces in your errors, along with numerous other benefits. Learn more here."
5+
---
6+
7+
<PlatformContent includePath="sourcemaps/primer" />
8+
9+
<PlatformContent includePath="sourcemaps/upload/primer" />
10+
11+
## Additional Resources
12+
13+
- [Using sentry-cli to Upload Source Maps](/cli/releases/#sentry-cli-sourcemaps)
14+
- [4 Reasons Why Your Source Maps Are Broken](https://blog.sentry.io/2018/10/18/4-reasons-why-your-source-maps-are-broken)
15+
- [Debug Your Node.js Projects with Source Maps](https://blog.sentry.io/2019/02/20/debug-node-source-maps)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
`@sentry/sveltekit` will generate and upload source maps automatically, so that errors in Sentry will contain readable stack traces.
22

3-
The SvelteKit SDK uses the [Sentry Vite Plugin](https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2) to upload source maps. See the [Manual Configuration](../manual-setup/#source-maps-upload) page and the Sentry [Vite documentation](https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#configuration) for more details.
3+
The SvelteKit SDK uses the Sentry Vite Plugin to upload source maps. See the [Sentry Vite Plugin documentation](https://www.npmjs.com/package/@sentry/vite-plugin#options) for all available options.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
### Source Maps Upload
2+
3+
By default, the `sentrySvelteKit()` Vite plugin uploads source maps for both server and client builds. This means that when you run a production build (`npm run build`), source maps will be generated and uploaded to Sentry so that you get readable stack traces in your Sentry issues.
4+
5+
However, you still need to specify your Sentry auth token as well as your org and project slugs.
6+
There are two ways to set them:
7+
8+
**Option 1**
9+
10+
You can set all values as environment variables, for example, in a `.env` file:
11+
12+
<OrgAuthTokenNote />
13+
14+
```bash {filename: .env}
15+
# DO NOT commit this file to your repo. The auth token is a secret.
16+
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
17+
SENTRY_ORG=___ORG_SLUG___
18+
SENTRY_PROJECT=___PROJECT_SLUG___
19+
# SENTRY_URL is only necessary if you're using a self-hosted Sentry
20+
# instance (as opposed to `https://sentry.io`)
21+
SENTRY_URL="https://your-sentry-instance.com"
22+
```
23+
24+
**Option 2**
25+
26+
You can also set your org and project slugs by passing a `sourceMapsUploadOptions` object to `sentrySvelteKit`, as seen in the example below. For a full list of available options, see the [Sentry Vite Plugin documentation](https://www.npmjs.com/package/@sentry/vite-plugin#options).
27+
28+
```javascript {filename:vite.config.(js|ts)} {6-12}
29+
import { sveltekit } from "@sveltejs/kit/vite";
30+
import { sentrySvelteKit } from "@sentry/sveltekit";
31+
32+
export default {
33+
plugins: [
34+
sentrySvelteKit({
35+
sourceMapsUploadOptions: {
36+
org: "___ORG_SLUG___",
37+
project: "___PROJECT_SLUG___",
38+
authToken: process.env.SENTRY_AUTH_TOKEN,
39+
// If you're self-hosting Sentry, also add your instance URL:
40+
// url: "https://your-self-hosted-sentry.com/",
41+
},
42+
}),
43+
sveltekit(),
44+
],
45+
// ... rest of your Vite config
46+
};
47+
```
48+
49+
To keep your auth token secure, always store it in an environment variable instead of directly in your files:
50+
51+
<OrgAuthTokenNote />
52+
53+
```bash {filename:.env}
54+
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
55+
```
56+
57+
### Override SvelteKit Adapter Detection
58+
59+
By default, `sentrySvelteKit` will try to detect your SvelteKit adapter to configure the source maps upload correctly. If you're not using one of the [supported adapters](/platforms/javascript/guides/sveltekit) or the wrong one is detected, you can override the adapter detection by passing the `adapter` option to `sentrySvelteKit`:
60+
61+
```javascript {filename:vite.config.(js|ts)} {7}
62+
import { sveltekit } from "@sveltejs/kit/vite";
63+
import { sentrySvelteKit } from "@sentry/sveltekit";
64+
65+
export default {
66+
plugins: [
67+
sentrySvelteKit({
68+
adapter: "vercel",
69+
}),
70+
sveltekit(),
71+
],
72+
// ... rest of your Vite config
73+
};
74+
```
75+
76+
### Disable Source Maps Upload
77+
78+
You can disable automatic source maps upload in your Vite config:
79+
80+
```javascript {filename:vite.config.(js|ts)} {7}
81+
import { sveltekit } from "@sveltejs/kit/vite";
82+
import { sentrySvelteKit } from "@sentry/sveltekit";
83+
84+
export default {
85+
plugins: [
86+
sentrySvelteKit({
87+
autoUploadSourceMaps: false,
88+
}),
89+
sveltekit(),
90+
],
91+
// ... rest of your Vite config
92+
};
93+
```
94+
95+
If you disable automatic source maps upload, you must explicitly set a `release` value in your `Sentry.init()` configuration. This is important for Sentry features like release health and correctly associating errors with specific deployments.

0 commit comments

Comments
 (0)