Skip to content

Commit a70f432

Browse files
inventarSarahbitsandfoxes
authored andcommitted
docs(js): Rework SvelteKit manual setup into quick start guide (#13632)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR We've reworked the Manual Setup page for SvelteKit into a Quick Start guide. This page covers many in-depth topics that should be handled on other pages of the SvelteKit documentation. We will update and move this content in a follow-up issue/PR. This affects the following content: - "Source Maps Upload" - everything in "Step3: Optional Configuration" Closes: #13399 ## 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 da52a06 commit a70f432

File tree

2 files changed

+179
-47
lines changed

2 files changed

+179
-47
lines changed

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

Lines changed: 167 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
---
22
title: Manual Setup
33
sidebar_order: 1
4-
description: "Learn how to manually set up Sentry in your Angular app and capture your first errors."
4+
description: "Learn how to manually set up Sentry in your SvelteKit app and capture your first errors."
55
---
66

7-
If you can't (or prefer not to) run the <PlatformLink to="/#install">automatic setup</PlatformLink>, you can follow the instructions below to configure the Sentry SvelteKit SDK in your application. This guide is also useful to adjust the pre-set configuration if you used the installation wizard for automatic setup.
7+
<Alert type="info">
8+
For the fastest setup, we recommend using the [wizard
9+
installer](/platforms/javascript/guides/sveltekit).
10+
</Alert>
11+
12+
<PlatformContent includePath="getting-started-prerequisites" />
13+
14+
## Step 1: Install
15+
16+
Choose the features you want to configure, and this guide will show you how:
17+
18+
<OnboardingOptionButtons
19+
options={["error-monitoring", "performance", "session-replay"]}
20+
/>
21+
22+
<PlatformContent includePath="getting-started-features-expandable" />
23+
24+
### Install the Sentry SDK
825

9-
## Install
26+
Run the command for your preferred package manager to add the Sentry SDK to your application:
1027

1128
```bash {tabTitle:npm}
1229
npm install @sentry/sveltekit --save
@@ -22,18 +39,15 @@ pnpm add @sentry/sveltekit
2239

2340
If you're updating your Sentry SDK to the latest version, check out our [migration guide](https://github.com/getsentry/sentry-javascript/blob/master/MIGRATION.md) to learn more about breaking changes.
2441

25-
## Configure
42+
## Step 2: Configure
2643

27-
The Sentry SDK needs to be initialized and configured in three places: On the client-side, server-side and in your Vite config.
44+
You need to initialize and configure the Sentry SDK in three places: the client side, the server side, and your Vite config.
2845

29-
### Client-side Setup
46+
### Configure Client-Side Sentry
3047

31-
If you don't already have a [client hooks](https://kit.svelte.dev/docs/hooks#shared-hooks) file, create a new one in `src/hooks.client.(js|ts)`.
48+
Create a client hooks file `src/hooks.client.(js|ts)` in the `src` folder of your project if you don't have one already. In this file, import and initialize the Sentry SDK and add the `handleErrorWithSentry` function to the [`handleError` hook](https://svelte.dev/docs/kit/hooks#Shared-hooks-handleError).
3249

33-
At the top of your client hooks file, import and initialize the Sentry SDK as shown in the snippet below. See the [Basic Options](../configuration/options/) page to view other SDK configuration options.
34-
Also, add the `handleErrorWithSentry` function to the [`handleError` hook](https://kit.svelte.dev/docs/hooks#shared-hooks-handleerror):
35-
36-
```javascript {filename:hooks.client.(js|ts)} {1, 3-18, 24}
50+
```javascript {filename:hooks.client.(js|ts)} {1, 3-28, 34}
3751
import * as Sentry from "@sentry/sveltekit";
3852

3953
Sentry.init({
@@ -42,15 +56,25 @@ Sentry.init({
4256
// Adds request headers and IP for users, for more info visit:
4357
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
4458
sendDefaultPii: true,
59+
// ___PRODUCT_OPTION_START___ performance
4560

46-
// We recommend adjusting this value in production, or using tracesSampler
47-
// for finer control
61+
// Set tracesSampleRate to 1.0 to capture 100%
62+
// of transactions for tracing.
63+
// We recommend adjusting this value in production
64+
// Learn more at
65+
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
4866
tracesSampleRate: 1.0,
49-
50-
// Optional: Initialize Session Replay:
67+
// ___PRODUCT_OPTION_END___ performance
68+
// ___PRODUCT_OPTION_START___ session-replay
5169
integrations: [Sentry.replayIntegration()],
70+
71+
// Capture Replay for 10% of all sessions,
72+
// plus for 100% of sessions with an error
73+
// Learn more at
74+
// https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
5275
replaysSessionSampleRate: 0.1,
5376
replaysOnErrorSampleRate: 1.0,
77+
// ___PRODUCT_OPTION_END___ session-replay
5478
});
5579

5680
const myErrorHandler = ({ error, event }) => {
@@ -63,15 +87,11 @@ export const handleError = Sentry.handleErrorWithSentry(myErrorHandler);
6387
// export const handleError = handleErrorWithSentry();
6488
```
6589

66-
### Server-side Setup
67-
68-
If you don't already have a [server hooks](https://kit.svelte.dev/docs/hooks#server-hooks) file, create a new one in `src/hooks.server.(js|ts)`.
90+
### Configure Server-Side Sentry
6991

70-
At the top of your server hooks file, import and initialize the Sentry SDK as shown in the snippet below. See the [Basic Options](../configuration/options/) page to view other SDK configuration options.
71-
Add the `handleErrorWithSentry` function to the [`handleError` hook](https://kit.svelte.dev/docs/hooks#shared-hooks-handleerror) and add the Sentry request handler to the [`handle` hook](https://kit.svelte.dev/docs/hooks#server-hooks-handle).
72-
If you're already using your own handler(s), use SvelteKit's [`sequence`](https://kit.svelte.dev/docs/modules#sveltejs-kit-hooks-sequence) function to add the Sentry handler _before_ your handler(s).
92+
Create a server hooks file `src/hooks.server.(js|ts)` in the `src` folder of your project if you don't have one already. In this file, import and initialize the Sentry SDK and add the `handleErrorWithSentry` function to the [`handleError` hook](https://svelte.dev/docs/kit/hooks#Shared-hooks-handleError) and the Sentry request handler to the [`handle` hook](https://kit.svelte.dev/docs/hooks#server-hooks-handle).
7393

74-
```javascript {filename:hooks.server.(js|ts)} {1, 3-13, 19, 23}
94+
```javascript {filename:hooks.server.(js|ts)} {1, 3-18, 24, 28}
7595
import * as Sentry from "@sentry/sveltekit";
7696

7797
Sentry.init({
@@ -80,10 +100,15 @@ Sentry.init({
80100
// Adds request headers and IP for users, for more info visit:
81101
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
82102
sendDefaultPii: true,
103+
// ___PRODUCT_OPTION_START___ performance
83104

84-
// We recommend adjusting this value in production, or using tracesSampler
85-
// for finer control
105+
// Set tracesSampleRate to 1.0 to capture 100%
106+
// of transactions for tracing.
107+
// We recommend adjusting this value in production
108+
// Learn more at
109+
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
86110
tracesSampleRate: 1.0,
111+
// ___PRODUCT_OPTION_END___ performance
87112
});
88113

89114
const myErrorHandler = ({ error, event }) => {
@@ -95,30 +120,25 @@ export const handleError = Sentry.handleErrorWithSentry(myErrorHandler);
95120
// export const handleError = handleErrorWithSentry();
96121

97122
export const handle = Sentry.sentryHandle();
98-
// Or use `sequence`:
123+
// Or use `sequence` if you're using your own handler(s):
99124
// export const handle = sequence(Sentry.sentryHandle(), yourHandler());
100125
```
101126

102-
### Vite Setup
127+
### Configure Vite
103128

104-
Add the `sentrySvelteKit` plugins to your `vite.config.(js|ts)` file so the Sentry SDK can apply build-time features.
105-
Make sure that it is added _before_ the sveltekit plugin:
129+
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.
106130

107-
```javascript {filename:vite.config.(js|ts)} {2,5}
131+
```javascript {filename:vite.config.(js|ts)} {2,6}
108132
import { sveltekit } from "@sveltejs/kit/vite";
109133
import { sentrySvelteKit } from "@sentry/sveltekit";
134+
import { defineConfig } from "vite";
110135

111-
export default {
136+
export default defineConfig({
112137
plugins: [sentrySvelteKit(), sveltekit()],
113138
// ... rest of your Vite config
114-
};
139+
});
115140
```
116141

117-
The `sentrySvelteKit()` function adds Vite plugins to your Vite config to:
118-
119-
- Automatically upload source maps to Sentry
120-
- Automatically instrument `load` functions for tracing
121-
122142
### Source Maps Upload
123143

124144
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.
@@ -207,7 +227,7 @@ export default {
207227

208228
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.
209229

210-
## Optional Configuration
230+
## Step 3: Optional Configuration
211231

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

@@ -376,7 +396,116 @@ export const GET = wrapServerRouteWithSentry(async () => {
376396
});
377397
```
378398

379-
## Cloudflare Pages Configuration
399+
## Step 4: Cloudflare Pages Configuration (Optional)
380400

381401
If you're deploying your application to Cloudflare Pages, you need to adjust your server-side setup.
382402
Follow this guide to [configure Sentry for Cloudflare](/platforms/javascript/guides/cloudflare/frameworks/sveltekit/).
403+
404+
## Step 5: Avoid Ad Blockers With Tunneling (Optional)
405+
406+
You can prevent ad blockers from blocking Sentry events using tunneling. Use the `tunnel` option to add an API endpoint in your application that forwards Sentry events to Sentry servers.
407+
408+
Update `Sentry.init` in your `hooks.client.(js|ts)` file with the following option:
409+
410+
```javascript {filename:hooks.client.(js|ts)}
411+
Sentry.init({
412+
dsn: "___PUBLIC_DSN___",,
413+
tunnel: "/tunnel",
414+
});
415+
```
416+
417+
This will send all events to the `tunnel` endpoint. However, the events need to be parsed and redirected to Sentry, so you need to do additional configuration on the server. You can find a detailed explanation on how to do this on our [Troubleshooting page](/platforms/javascript/guides/sveltekit/troubleshooting/#using-the-tunnel-option).
418+
419+
## Step 6: Verify Your Setup
420+
421+
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
422+
423+
### Issues
424+
425+
To verify that Sentry captures errors and creates issues in your Sentry project, create a test page, for example, at `src/routes/sentry-example/+page.svelte` with a button that throws an error when clicked:
426+
427+
```html {filename:+page.svelte}
428+
<script>
429+
function throwTestError() {
430+
throw new Error("Sentry Example Frontend Error");
431+
}
432+
</script>
433+
434+
<button type="button" onclick="{throwTestError}">Throw error</button>
435+
```
436+
437+
<OnboardingOption optionId="performance" hideForThisOption>
438+
Open the page `sentry-example` in a browser and click the button to trigger a
439+
frontend error.
440+
</OnboardingOption>
441+
442+
<PlatformContent includePath="getting-started-browser-sandbox-warning" />
443+
444+
<OnboardingOption optionId="performance">
445+
### Tracing
446+
To test tracing, create a test API route like `src/routes/sentry-example/+server.(js|ts)`:
447+
448+
```javascript {filename:+server.(js|ts)}
449+
export const GET = async () => {
450+
throw new Error("Sentry Example API Route Error");
451+
};
452+
```
453+
454+
Next, update your test button to call this route and throw an error if the response isn't `ok`:
455+
456+
```html {filename:+page.svelte}
457+
<script>
458+
import * as Sentry from "@sentry/sveltekit";
459+
460+
function throwTestError() {
461+
Sentry.startSpan(
462+
{
463+
name: "Example Frontend Span",
464+
op: "test",
465+
},
466+
async () => {
467+
const res = await fetch("/sentry-example");
468+
if (!res.ok) {
469+
throw new Error("Sentry Example Frontend Error");
470+
}
471+
}
472+
);
473+
}
474+
</script>
475+
476+
<button type="button" onclick="{throwTestError}">Throw error with trace</button>
477+
```
478+
479+
Open the page `sentry-example` in a browser and click the button to trigger two errors:
480+
481+
- a frontend error
482+
- an error within the API route
483+
484+
Additionally, this starts a trace to measure the time it takes for the API request to complete.
485+
486+
</OnboardingOption>
487+
488+
### View Captured Data in Sentry
489+
490+
Now, head over to your project on [Sentry.io](https://sentry.io) to view the collected data (it takes a couple of moments for the data to appear).
491+
492+
<PlatformContent includePath="getting-started-verify-locate-data" />
493+
494+
## Next Steps
495+
496+
At this point, you should have integrated Sentry into your SvelteKit application and should already be sending data to your Sentry project.
497+
498+
Now's a good time to customize your setup and look into more advanced topics.
499+
Our next recommended steps for you are:
500+
501+
- Learn how to [manually capture errors](/platforms/javascript/guides/sveltekit/usage/)
502+
- Continue to [customize your configuration](/platforms/javascript/guides/sveltekit/configuration/)
503+
- Get familiar with [Sentry's product features](/product/) like tracing, insights, and alerts
504+
505+
<Expandable permalink={false} title="Are you having problems setting up the SDK?">
506+
507+
- If you encountered issues with setting up Sentry manually, [try our installation wizard](/platforms/javascript/guides/sveltekit/)
508+
- Find various topics in [Troubleshooting](/platforms/javascript/guides/sveltekit/troubleshooting/)
509+
- [Get support](https://sentry.zendesk.com/hc/en-us/)
510+
511+
</Expandable>

platform-includes/getting-started-prerequisites/javascript.sveltekit.mdx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ You need:
88
- Vite version `4.2`+
99

1010
<Expandable title="Notes on SvelteKit adapter compatibility">
11-
The SvelteKit Sentry SDK is designed to work out of the box with the following SvelteKit adapters:
11+
The SvelteKit Sentry SDK is designed to work out of the box with several SvelteKit adapters and their underlying server runtimes.
12+
Here's an overview of the current support:
1213

13-
- [Adapter-auto](https://kit.svelte.dev/docs/adapter-auto) – for Vercel; other platforms might work but we don't guarantee compatibility at this time
14-
- [Adapter-vercel](https://kit.svelte.dev/docs/adapter-vercel) – only for Node (Lambda) runtimes, not yet Vercel's edge runtime
15-
- [Adapter-cloudflare](https://kit.svelte.dev/docs/adapter-cloudflare) – supported but requires [additional setup](https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/sveltekit/)
16-
- [Adapter-node](https://kit.svelte.dev/docs/adapter-node)
17-
18-
Other adapters may work but aren't currently supported. We're looking into extending first-class support to more adapters in the future.
19-
20-
Also, Sentry's SvelteKit SDK does not yet work with all non-node server runtimes, such as Vercel's edge runtime.
14+
- **Fully supported Node.js runtimes**
15+
- [Adapter-auto](https://kit.svelte.dev/docs/adapter-auto) for Vercel; other Node.js-based platforms might work, but we don't guarantee compatibility at this time
16+
- [Adapter-vercel](https://kit.svelte.dev/docs/adapter-vercel) when used with Vercel's Node.js Lambda runtime
17+
- [Adapter-node](https://kit.svelte.dev/docs/adapter-node)
18+
- **Supported non-Node.js runtimes**
19+
- [Adapter-cloudflare](https://kit.svelte.dev/docs/adapter-cloudflare) requires [additional setup](/platforms/javascript/guides/cloudflare/frameworks/sveltekit/)
20+
- **Currently not supported**
21+
- Non-Node.js server runtimes, such as Vercel's edge runtime, are not yet supported.
22+
- **Other adapters**
23+
- Other SvelteKit adapters might work, but they're not currently officially supported. We're looking into extending first-class support to more adapters in the future.
2124

2225
</Expandable>

0 commit comments

Comments
 (0)