Skip to content

Commit ec1aacf

Browse files
Lms24coolguyzone
authored andcommitted
ref(sveltekit): Adjust Cloudflare setup to only use SvelteKit SDK (#12801)
* ref(sveltekit): Adjust cloudflare setup to only use SvelteKit SDK * more linking, rewording * Apply suggestions from code review Co-authored-by: Alex Krawiec <[email protected]> --------- Co-authored-by: Alex Krawiec <[email protected]>
1 parent 30fe14b commit ec1aacf

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

docs/platforms/javascript/guides/cloudflare/frameworks/sveltekit.mdx

+26-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ title: Cloudflare Sveltekit Framework Guide
33
description: "Learn how to add Cloudflare instrumentation to your SvelteKit app."
44
---
55

6-
If you're running your SvelteKit app on Cloudflare Pages, you can use the Sentry SvelteKit SDK in combination with the Sentry Cloudflare SDK to add Sentry instrumentation.
6+
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.
7+
8+
## 1. Install the SDK
79

810
First, install the Sentry SvelteKit SDK in your application. We recommend using the Sentry wizard to automatically install the SDK:
911

@@ -13,9 +15,13 @@ npx @sentry/wizard@latest -i sveltekit
1315

1416
If the setup through the wizard doesn't work for you, you can also [set up the SvelteKit SDK manually](/platforms/javascript/guides/sveltekit/manual-setup/).
1517

16-
After installing the Sentry SvelteKit SDK, you can move on to setting up the Cloudflare SDK. First, install the SDK with your package manager:
18+
<Alert>
19+
20+
If installed the SDK before, make sure that `@sentry/sveltekit` version `9.2.0` or newer is installed.
21+
22+
</Alert>
1723

18-
<PlatformContent includePath="getting-started-install" />
24+
## 2. Cloudflare configuration
1925

2026
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
2127
needs access to the `AsyncLocalStorage` API to work correctly.
@@ -25,13 +31,15 @@ compatibility_flags = ["nodejs_compat"]
2531
# compatibility_flags = ["nodejs_als"]
2632
```
2733

28-
To use this SDK, update your `src/hooks.server.(ts|js)` file to use the `Sentry.wrapRequestHandler` method from the Sentry Cloudflare SDK and remove the `Sentry.init` call from `@sentry/sveltekit`.
34+
## 3. Update Your Server Hooks File
35+
36+
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`.
2937

3038
```typescript diff {filename:hooks.server.(ts|js)}
31-
import { sequence } from "@sveltejs/kit/hooks";
32-
import { handleErrorWithSentry, sentryHandle } from "@sentry/sveltekit";
33-
-import * as Sentry from '@sentry/sveltekit';
34-
+import * as Sentry from "@sentry/cloudflare";
39+
-import { handleErrorWithSentry, sentryHandle } from "@sentry/sveltekit";
40+
+import { handleErrorWithSentry, sentryHandle, initCloudflareSentryHandle } from "@sentry/sveltekit";
41+
+import { sequence } from "@sveltejs/kit/hooks";
42+
import * as Sentry from '@sentry/sveltekit';
3543

3644
-Sentry.init({
3745
- dsn: '___PUBLIC_DSN___',
@@ -41,26 +49,16 @@ To use this SDK, update your `src/hooks.server.(ts|js)` file to use the `Sentry.
4149
- // spotlight: import.meta.env.DEV,
4250
-});
4351
-
44-
+const handleInitSentry = ({ event, resolve }) => {
45-
+ return event.platform
46-
+ ? Sentry.wrapRequestHandler(
47-
+ {
48-
+ options: {
49-
+ dsn: '___PUBLIC_DSN___',
50-
+ tracesSampleRate: 1.0,
51-
+ },
52-
+ request: event.request,
53-
+ context: event.platform.ctx,
54-
+ },
55-
+ () => resolve(event)
56-
+ )
57-
+ : resolve(event);
58-
+};
59-
-// If you have custom handlers, make sure to place them after `sentryHandle()` in the `sequence` function.
60-
-export const handle = sequence(sentryHandle());
61-
+export const handle = sequence(handleInitSentry, sentryHandle());
62-
63-
export const handleError = handleErrorWithSentry(myErrorHandler);
52+
-export const handle = sentryHandle();
53+
+export const handle = sequence(
54+
+ initCloudflareSentryHandle({
55+
+ dsn: '___PUBLIC_DSN___',
56+
+ tracesSampleRate: 1.0,
57+
+ }),
58+
+ sentryHandle()
59+
+);
60+
61+
export const handleError = handleErrorWithSentry(myErrorHandler);
6462
```
6563

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

docs/platforms/javascript/guides/sveltekit/manual-setup.mdx

+5
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,8 @@ export const GET = wrapServerRouteWithSentry(async () => {
367367
return new Response("Hello World");
368368
});
369369
```
370+
371+
## Cloudflare Pages Configuration
372+
373+
If you're deploying your application to Cloudflare Pages, you need to adjust your server-side setup.
374+
Follow this guide to [configure Sentry for Cloudflare](/platforms/javascript/guides/cloudflare/frameworks/sveltekit/).

platform-includes/getting-started-primer/javascript.sveltekit.mdx

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ The SvelteKit SDK is designed to work out of the box with the following SvelteKi
1111

1212
- [Adapter-auto](https://kit.svelte.dev/docs/adapter-auto) - for Vercel; other platforms might work but we don't guarantee compatibility at this time.
1313
- [Adapter-vercel](https://kit.svelte.dev/docs/adapter-vercel) - only for Node (Lambda) runtimes, not yet Vercel's edge runtime.
14+
- [Adapter-cloudflare](https://kit.svelte.dev/docs/adapter-cloudflare) - supported but requires [additional Setup](/platforms/javascript/guides/cloudflare/frameworks/sveltekit/).
1415
- [Adapter-node](https://kit.svelte.dev/docs/adapter-node).
1516

1617
Other adapters may work but aren't currently supported.
1718
We're looking into extending first-class support to [more adapters](https://kit.svelte.dev/docs/adapters) in the future.
1819

19-
The SvelteKit SDK does not yet work with non-node server runtimes, such as Vercel's edge runtime or Cloudflare Workers.
20+
The SvelteKit SDK does not yet work with all non-node server runtimes, such as Vercel's edge runtime.
2021

2122
</Alert>

0 commit comments

Comments
 (0)