Cloudflare Sveltekit Framework Guide
Learn how to add Cloudflare instrumentation to your SvelteKit app.
If you're running your SvelteKit app on Cloudflare Pages, you need to configure the SDK a little differently to the default setup. This guide will show you how to set up the Sentry SvelteKit SDK for Cloudflare Pages.
First, install the Sentry SvelteKit SDK in your application. We recommend using the Sentry wizard to automatically install the SDK:
npx @sentry/wizard@latest -i sveltekit
If the setup through the wizard doesn't work for you, you can also set up the SvelteKit SDK manually.
If installed the SDK before, make sure that @sentry/sveltekit
version 9.2.0
or newer is installed.
To use the SDK, you'll need to set either the nodejs_compat
or nodejs_als
compatibility flags in your wrangler.toml
. This is because the SDK needs access to the AsyncLocalStorage
API to work correctly.
wrangler.toml
compatibility_flags = ["nodejs_compat"]
# compatibility_flags = ["nodejs_als"]
To use this SDK, update your src/hooks.server.(ts|js)
file to use the initCloudflareSentryHandle
method from the Sentry Cloudflare SDK and remove the Sentry.init
call from @sentry/sveltekit
.
hooks.server.(ts|js)
-import { handleErrorWithSentry, sentryHandle } from "@sentry/sveltekit";
+import { handleErrorWithSentry, sentryHandle, initCloudflareSentryHandle } from "@sentry/sveltekit";
+import { sequence } from "@sveltejs/kit/hooks";
import * as Sentry from '@sentry/sveltekit';
-Sentry.init({
- dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',
- tracesSampleRate: 1.0,
-
- // uncomment the line below to enable Spotlight (https://spotlightjs.com)
- // spotlight: import.meta.env.DEV,
-});
-
-export const handle = sentryHandle();
+export const handle = sequence(
+ initCloudflareSentryHandle({
+ dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',
+ tracesSampleRate: 1.0,
+ }),
+ sentryHandle()
+);
export const handleError = handleErrorWithSentry(myErrorHandler);
If you need to use environmental variables, you can access them using event.platform.env
.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").