Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 2.27 KB

File metadata and controls

64 lines (47 loc) · 2.27 KB
title description
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.

1. Install the SDK

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.

2. Cloudflare configuration

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.

compatibility_flags = ["nodejs_compat"]
# compatibility_flags = ["nodejs_als"]

3. Update Your Server Hooks File

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.

-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: '___PUBLIC_DSN___',
-  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: '___PUBLIC_DSN___',
+       tracesSampleRate: 1.0,
+   }),
+   sentryHandle()
+);

 export const handleError = handleErrorWithSentry(myErrorHandler);

If you need to use environmental variables, you can access them using event.platform.env.