Connect SvelteKit to Bugsink
SvelteKit runs your application in the browser and on the server. Bugsink can capture failures from both sides and show the source and request that led to them.
This guide adds the SvelteKit SDK, connects its client and server hooks, and uploads production source maps. To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.
Install the SvelteKit SDK
Install the open-source Sentry SDK for SvelteKit. Bugsink accepts the Sentry event protocol, so the same SDK can send browser and server errors directly to your Bugsink project:
npm install @sentry/sveltekitnpm install @sentry/sveltekit
Make the project DSN available
The client bundle and server process both need the project DSN. SvelteKit 3
declares public runtime variables in src/env.js; this makes
PUBLIC_SENTRY_DSN available through $app/env/public without hard-coding it
in the application:
import { defineEnvVars } from "@sveltejs/kit/env"; export const variables = defineEnvVars({ PUBLIC_SENTRY_DSN: { public: true }, });import { defineEnvVars } from "@sveltejs/kit/env"; export const variables = defineEnvVars({ PUBLIC_SENTRY_DSN: { public: true }, });
A DSN identifies the destination project and is safe to include in browser code. It is not an administrative or source-map upload token.
Add the browser error hook
Initialize the SDK in src/hooks.client.js. The exported error hook reports
errors from SvelteKit’s client-side lifecycle; the SDK also installs its normal
browser handlers for uncaught errors and unhandled promise rejections:
import * as Sentry from "@sentry/sveltekit"; import { PUBLIC_SENTRY_DSN } from "$app/env/public"; Sentry.init({ dsn: PUBLIC_SENTRY_DSN, }); export const handleError = Sentry.handleErrorWithSentry();import * as Sentry from "@sentry/sveltekit"; import { PUBLIC_SENTRY_DSN } from "$app/env/public"; Sentry.init({ dsn: PUBLIC_SENTRY_DSN, }); export const handleError = Sentry.handleErrorWithSentry();
Add the server hooks
Initialize the same SDK from src/hooks.server.js. sentryHandle adds request
context around SvelteKit’s server lifecycle, while handleErrorWithSentry
reports unhandled route and rendering failures:
import * as Sentry from "@sentry/sveltekit"; import { PUBLIC_SENTRY_DSN } from "$app/env/public"; Sentry.init({ dsn: PUBLIC_SENTRY_DSN, }); export const handle = Sentry.sentryHandle(); export const handleError = Sentry.handleErrorWithSentry();import * as Sentry from "@sentry/sveltekit"; import { PUBLIC_SENTRY_DSN } from "$app/env/public"; Sentry.init({ dsn: PUBLIC_SENTRY_DSN, }); export const handle = Sentry.sentryHandle(); export const handleError = Sentry.handleErrorWithSentry();
If you already export a server handle hook, combine it with sentryHandle
using SvelteKit’s sequence helper instead of replacing it.
Prepare production source maps
Production browser code is split, bundled, and minified. Add the SDK’s Vite
plugin before sveltekit() so the build emits maps and puts matching debug IDs
in the browser bundles before adapter-node compresses them:
import { sentrySvelteKit } from "@sentry/sveltekit/vite"; import { sveltekit } from "@sveltejs/kit/vite"; import { defineConfig } from "vite"; export default defineConfig({ plugins: [ sentrySvelteKit({ telemetry: false, release: { name: "", create: false, inject: false, }, sourcemaps: { upload: false, injectDebugIds: true, delete: false, }, }), sveltekit(), ], });import { sentrySvelteKit } from "@sentry/sveltekit/vite"; import { sveltekit } from "@sveltejs/kit/vite"; import { defineConfig } from "vite"; export default defineConfig({ plugins: [ sentrySvelteKit({ telemetry: false, release: { name: "", create: false, inject: false, }, sourcemaps: { upload: false, injectDebugIds: true, delete: false, }, }), sveltekit(), ], });
Keep your existing sveltekit() options and adapter configuration; only add
the sentrySvelteKit() import and put that plugin first. The empty release
configuration avoids Sentry’s release-management API, which is not needed for
Bugsink’s debug-ID source maps. telemetry: false prevents the build plugin
from sending its own usage data to Sentry, and delete: false keeps the maps
available for the separate upload step.
The upload is deliberately disabled in the plugin. Its current uploader is not
compatible with Bugsink; the next step uses sentry-cli, while the plugin still
does the SvelteKit-specific map generation and debug-ID injection.
Build and upload the source maps
Supply the project DSN when you create the production build:
PUBLIC_SENTRY_DSN=https://key@bugsink.example/1 npm run buildPUBLIC_SENTRY_DSN=https://key@bugsink.example/1 npm run build
Create a project-bound Bugsink token with the debug-files:upload capability,
then upload the completed build. Keep this token in your release environment,
never in the browser bundle. Bugsink has no organization object, so --org is
only a compatibility value:
SENTRY_AUTH_TOKEN=your-project-token \ npx --yes @sentry/cli \ --url https://bugsink.example \ sourcemaps --org bugsink --project your-project-slug upload ./buildSENTRY_AUTH_TOKEN=your-project-token \ npx --yes @sentry/cli \ --url https://bugsink.example \ sourcemaps --org bugsink --project your-project-slug upload ./build
Do not run sourcemaps inject after this build: the SvelteKit plugin already
injected the debug IDs before adapter-node created its compressed assets.
Upload the build’s maps before that build can receive production traffic.
Trigger a browser error
Start the production server with the same public DSN. Adapter-node uses
PORT=3000 by default, but you can choose any port:
PORT=3000 \ PUBLIC_SENTRY_DSN=https://key@bugsink.example/1 \ node buildPORT=3000 \ PUBLIC_SENTRY_DSN=https://key@bugsink.example/1 \ node build
For this setup check, the page’s button deliberately throws from ordinary Svelte component code:
function testBrowserCheckout() { throw new Error("SvelteKit browser checkout failed [sveltekit-browser]"); }function testBrowserCheckout() { throw new Error("SvelteKit browser checkout failed [sveltekit-browser]"); }
Open http://127.0.0.1:3000/ and click Test a browser checkout failure.
The uncaught exception still follows normal browser behavior; the SDK reports
it in the background.
See the original component in Bugsink
Open the new issue in Bugsink. The uploaded map translates the production bundle back to the Svelte component and shows the exact line that threw.
Error
That’s it: browser failures from the production build now reach Bugsink with their original Svelte source.
Check a server route
The same SDK also reports failures from SvelteKit’s server hooks. This route looks up an inventory item and deliberately raises an application exception when the requested SKU does not exist:
export function GET({ params }) { const product = products[params.sku]; if (!product) { throw new ProductNotFound( `Product ${params.sku} is not in inventory [sveltekit-server]`, ); } return json(product); }export function GET({ params }) { const product = products[params.sku]; if (!product) { throw new ProductNotFound( `Product ${params.sku} is not in inventory [sveltekit-server]`, ); } return json(product); }
Open http://127.0.0.1:3000/api/products/missing. SvelteKit returns its normal
500 response, and the server error hook reports the exception with the request
and original route source:
Error
Server route failures now reach the same Bugsink project with their request and original route source.
Limitations
The automatic uploader bundled with @sentry/sveltekit 11.15.0 currently fails
before uploading to Bugsink. Keep sourcemaps.upload: false and use the
sentry-cli command above. The rest of the plugin integration—map generation,
debug-ID injection, client instrumentation, and server instrumentation—remains
part of the setup above.
For SDK-wide capture options and other JavaScript frameworks, return to the JavaScript guide.
