Skip to content

Commit 53185a4

Browse files
committed
sveltekit quick start guide for manual setup
1 parent 1204b7f commit 53185a4

File tree

1 file changed

+176
-38
lines changed

1 file changed

+176
-38
lines changed

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

Lines changed: 176 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:
817

9-
## Install
18+
<OnboardingOptionButtons
19+
options={["error-monitoring", "performance", "session-replay"]}
20+
/>
21+
22+
<PlatformContent includePath="getting-started-features-expandable" />
23+
24+
### Install the Sentry SDK
25+
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
26-
27-
The Sentry SDK needs to be initialized and configured in three places: On the client-side, server-side and in your Vite config.
42+
## Step 2: Configure
2843

29-
### Client-side Setup
44+
You need to initialize and configure the Sentry SDK in three places: the client side, the server side, and your Vite config.
3045

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)`.
46+
### Configure Client-side Sentry
3247

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):
48+
Create a client hooks file `src/hooks.client.(js|ts)` if you don't have one already. In this file, import and initialize the Sentry SDK, specifying any SDK options for the client. Add the `handleErrorWithSentry` function to the [`handleError` hook](https://svelte.dev/docs/kit/hooks#Shared-hooks-handleError).
3549

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
90+
### Configure Server-side Sentry
6791

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)`.
92+
Create a server hooks file `src/hooks.server.(js|ts)` if you don't have one already. In this file, import and initialize the Sentry SDK, specifying any SDK options for the client. Additionally, add the `handleErrorWithSentry` function to the [`handleError` hook](https://svelte.dev/docs/kit/hooks#Shared-hooks-handleError) and add the Sentry request handler to the [`handle` hook](https://kit.svelte.dev/docs/hooks#server-hooks-handle).
6993

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).
73-
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,31 @@ 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+
<Alert level="warning" title="Important">
128+
For development, you can include your [Data Source
129+
Name](/concepts/key-terms/dsn-explainer/) (DSN) directly in these files.
130+
However, in production, we strongly recommend you use an environment variable.
131+
</Alert>
103132

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:
133+
### Configure Vite
106134

107-
```javascript {filename:vite.config.(js|ts)} {2,5}
135+
Add `sentrySvelteKit` to your plugins 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.
136+
137+
```javascript {filename:vite.config.(js|ts)} {2,6}
108138
import { sveltekit } from "@sveltejs/kit/vite";
109139
import { sentrySvelteKit } from "@sentry/sveltekit";
140+
import { defineConfig } from "vite";
110141

111-
export default {
142+
export default defineConfig({
112143
plugins: [sentrySvelteKit(), sveltekit()],
113144
// ... rest of your Vite config
114-
};
145+
});
115146
```
116147

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-
122148
### Source Maps Upload
123149

124150
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 +233,7 @@ export default {
207233

208234
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.
209235

210-
## Optional Configuration
236+
## Step 3: Optional Configuration
211237

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

@@ -376,7 +402,119 @@ export const GET = wrapServerRouteWithSentry(async () => {
376402
});
377403
```
378404

379-
## Cloudflare Pages Configuration
405+
## Step 4: Cloudflare Pages Configuration (optional)
380406

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

0 commit comments

Comments
 (0)