SvelteKit on Cloudflare
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 you installed the SDK before, make sure that @sentry/sveltekit version 9.2.0 or newer is installed.
Now you can install the Sentry Cloudflare SDK. First, install the SDK with your package manager:
npm install @sentry/cloudflare --save
Configuration should happen as early as possible in your application's lifecycle.
To use the SDK, you'll need to set either the nodejs_compat or nodejs_als compatibility flags in your wrangler.jsonc / wrangler.toml config. This is because the SDK needs access to the AsyncLocalStorage API to work correctly.
wrangler.jsonc{
"compatibility_flags": [
"nodejs_als",
// "nodejs_compat"
],
}
You will also want to add the CF_VERSION_METADATA binding:
wrangler.jsonc{
// ...
"version_metadata": {
"binding": "CF_VERSION_METADATA"
},
}
Next, 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").