
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
@sentry/sveltekit
Advanced tools
This SDK is currently in Alpha state and we're still experimenting with APIs and functionality. We therefore make no guarantees in terms of semver or breaking changes. If you want to try this SDK and come across a problem, please open a GitHub Issue.
Currently, the minimum supported version of SvelteKit is 1.0.0
.
This package is a wrapper around @sentry/node
for the server and @sentry/svelte
for the client side, with added functionality related to SvelteKit.
Although the SDK is not yet stable, you're more than welcome to give it a try and provide us with early feedback.
Here's how to get started:
Ensure you've set up the @sveltejs/adapter-node
adapter
Install the Sentry SvelteKit SDK:
# Using npm
npm install @sentry/sveltekit
# Using yarn
yarn add @sentry/sveltekit
The Sentry SvelteKit SDK mostly relies on SvelteKit Hooks to capture error and performance data.
If you don't already have a client hooks file, create a new one in src/hooks.client.(js|ts)
.
On the top of your hooks.client.(js|ts)
file, initialize the Sentry SDK:
// hooks.client.(js|ts)
import * as Sentry from '@sentry/sveltekit';
Sentry.init({
dsn: '__DSN__',
tracesSampleRate: 1.0,
// For instance, initialize Session Replay:
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
integrations: [new Sentry.Replay()],
});
Add our handleErrorWithSentry
function to the handleError
hook:
// hooks.client.(js|ts)
import { handleErrorWithSentry } from '@sentry/sveltekit';
const myErrorHandler = (({ error, event }) => {
console.error('An error occurred on the client side:', error, event);
});
export const handleError = handleErrorWithSentry(myErrorHandler);
// or alternatively, if you don't have a custom error handler:
// export const handleError = handleErrorWithSentry();
If you don't already have a server hooks file, create a new one in src/hooks.server.(js|ts)
.
On the top of your hooks.server.(js|ts)
file, initialize the Sentry SDK:
// hooks.server.(js|ts)
import * as Sentry from '@sentry/sveltekit';
Sentry.init({
dsn: '__DSN__',
tracesSampleRate: 1.0,
});
Add our handleErrorWithSentry
function to the handleError
hook:
// hooks.server.(js|ts)
import { handleErrorWithSentry } from '@sentry/sveltekit';
const myErrorHandler = (({ error, event }) => {
console.error('An error occurred on the server side:', error, event);
});
export const handleError = handleErrorWithSentry(myErrorHandler);
// or alternatively, if you don't have a custom error handler:
// export const handleError = handleErrorWithSentry();
Add our request handler to the handle
hook in hooks.server.ts
:
// hooks.server.(js|ts)
import { sentryHandle } from '@sentry/sveltekit';
export const handle = sentryHandle;
// or alternatively, if you already have a handler defined, use the `sequence` function
// see: https://kit.svelte.dev/docs/modules#sveltejs-kit-hooks-sequence
// export const handle = sequence(sentryHandle, yourHandler);
load
FunctionsTo catch errors and performance data in your universal load
functions (e.g. in +page.(js|ts)
), wrap our wrapLoadWithSentry
function around your load code:
// +page.(js|ts)
import { wrapLoadWithSentry } from '@sentry/sveltekit';
export const load = wrapLoadWithSentry((event) => {
//... your load code
});
To catch errors and performance data in your server load
functions (e.g. in +page.server.(js|ts)
), wrap our wrapServerLoadWithSentry
function around your load code:
// +page.server.(js|ts)
import { wrapServerLoadWithSentry } from '@sentry/sveltekit';
export const load = wrapServerLoadWithSentry((event) => {
//... your server load code
});
Add our sentrySvelteKit
plugins to your vite.config.(js|ts)
file so that the Sentry SDK can apply build-time features.
Make sure that it is added before the sveltekit
plugin:
// vite.config.(js|ts)
import { sveltekit } from '@sveltejs/kit/vite';
import { sentrySvelteKit } from '@sentry/sveltekit';
export default {
plugins: [sentrySvelteKit(), sveltekit()],
// ... rest of your Vite config
};
This adds the Sentry Vite Plugin to your Vite config to automatically upload source maps to Sentry.
After completing the Vite Setup, the SDK will automatically upload source maps to Sentry, when you
build your project. However, you still need to specify your Sentry auth token as well as your org and project slugs. You
can either set them as env variables (for example in a .env
file):
SENTRY_ORG
your Sentry org slugSENTRY_PROJECT
your Sentry project slugSENTRY_AUTH_TOKEN
your Sentry auth tokenOr you can pass them in whatever form you prefer to sentrySvelteKit
:
// vite.config.(js|ts)
import { sveltekit } from '@sveltejs/kit/vite';
import { sentrySvelteKit } from '@sentry/sveltekit';
export default {
plugins: [
sentrySvelteKit({
sourceMapsUploadOptions: {
org: 'my-org-slug',
project: 'my-project-slug',
authToken: process.env.SENTRY_AUTH_TOKEN,
},
}),
sveltekit(),
],
// ... rest of your Vite config
};
Under sourceMapsUploadOptions
, you can also specify all additional options supported by the
Sentry Vite Plugin.
This might be useful if you're using adapters other than the Node adapter or have a more customized build setup.
// vite.config.(js|ts)
import { sveltekit } from '@sveltejs/kit/vite';
import { sentrySvelteKit } from '@sentry/sveltekit';
export default {
plugins: [
sentrySvelteKit({
sourceMapsUploadOptions: {
org: 'my-org-slug',
project: 'my-project-slug',
authToken: 'process.env.SENTRY_AUTH_TOKEN',
include: ['dist'],
cleanArtifacts: true,
setCommits: {
auto: true,
},
},
}),
sveltekit(),
],
// ... rest of your Vite config
};
If you don't want to upload source maps automatically, you can disable it as follows:
// vite.config.(js|ts)
import { sveltekit } from '@sveltejs/kit/vite';
import { sentrySvelteKit } from '@sentry/sveltekit';
export default {
plugins: [
sentrySvelteKit({
autoUploadSourceMaps: false,
}),
sveltekit(),
],
// ... rest of your Vite config
};
This SDK is still under active development and several features are missing. Take a look at our SvelteKit SDK Development Roadmap to follow the progress:
Adapters other than @sveltejs/adapter-node
are currently not supported.
We haven't yet tested other platforms like Vercel.
This is on our roadmap but it will come at a later time.
We're aiming to simplify SDK setup in the future so that you don't have to go in and manually add our wrappers to all your load
functions.
This will be addressed once the SDK supports all Sentry features.
FAQs
Official Sentry SDK for SvelteKit
The npm package @sentry/sveltekit receives a total of 45,329 weekly downloads. As such, @sentry/sveltekit popularity was classified as popular.
We found that @sentry/sveltekit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.