Skip to content

Commit 8f9f42c

Browse files
SG60Lms24
andauthored
feat(sveltekit): Add Support for Cloudflare (getsentry#14672)
This patch adds a different set of package exports for workers. To use this, you have to use the new `initCloudflareSentryHandle` SvelteKit handler function, before your call to `sentryHandle()`. --------- Co-authored-by: Lukas Stracke <[email protected]>
1 parent e01a428 commit 8f9f42c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+660
-233
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
test-results
2+
node_modules
3+
4+
# Output
5+
.output
6+
.vercel
7+
.netlify
8+
.wrangler
9+
/.svelte-kit
10+
/build
11+
12+
# OS
13+
.DS_Store
14+
Thumbs.db
15+
16+
# Env
17+
.env
18+
.env.*
19+
!.env.example
20+
!.env.test
21+
22+
# Vite
23+
vite.config.js.timestamp-*
24+
vite.config.ts.timestamp-*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@sentry:registry=http://127.0.0.1:4873
2+
@sentry-internal:registry=http://127.0.0.1:4873
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# sv
2+
3+
Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli).
4+
5+
## Creating a project
6+
7+
If you're seeing this, you've probably already done this step. Congrats!
8+
9+
```bash
10+
# create a new project in the current directory
11+
npx sv create
12+
13+
# create a new project in my-app
14+
npx sv create my-app
15+
```
16+
17+
## Developing
18+
19+
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
20+
21+
```bash
22+
npm run dev
23+
24+
# or start the server and open the app in a new browser tab
25+
npm run dev -- --open
26+
```
27+
28+
## Building
29+
30+
To create a production version of your app:
31+
32+
```bash
33+
npm run build
34+
```
35+
36+
You can preview the production build with `npm run preview`.
37+
38+
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "sveltekit-cloudflare-pages",
3+
"private": true,
4+
"version": "0.0.1",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite dev",
8+
"build": "vite build",
9+
"preview": "wrangler pages dev ./.svelte-kit/cloudflare --port 4173",
10+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
11+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12+
"test:e2e": "playwright test",
13+
"test": "pnpm run test:e2e",
14+
"test:build": "pnpm install && pnpm build",
15+
"test:assert": "pnpm run test:e2e"
16+
},
17+
"dependencies": {
18+
"@sentry/sveltekit": "latest || *"
19+
},
20+
"devDependencies": {
21+
"@playwright/test": "^1.45.3",
22+
"@sveltejs/adapter-cloudflare": "^5.0.3",
23+
"@sveltejs/kit": "^2.17.2",
24+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
25+
"svelte": "^5.20.2",
26+
"svelte-check": "^4.1.4",
27+
"typescript": "^5.0.0",
28+
"vite": "^6.1.1",
29+
"wrangler": "3.105.0"
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from '@playwright/test';
2+
3+
export default defineConfig({
4+
webServer: {
5+
command: 'pnpm run build && pnpm run preview',
6+
port: 4173,
7+
},
8+
9+
testDir: 'tests',
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// See https://svelte.dev/docs/kit/types#app.d.ts
2+
// for information about these interfaces
3+
declare global {
4+
namespace App {
5+
// interface Error {}
6+
// interface Locals {}
7+
// interface PageData {}
8+
// interface PageState {}
9+
// interface Platform {}
10+
}
11+
}
12+
13+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
%sveltekit.head%
8+
</head>
9+
<body data-sveltekit-preload-data="hover">
10+
<div style="display: contents">%sveltekit.body%</div>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { env } from '$env/dynamic/public';
2+
import * as Sentry from '@sentry/sveltekit';
3+
4+
Sentry.init({
5+
dsn: env.PUBLIC_E2E_TEST_DSN,
6+
});
7+
8+
export const handleError = Sentry.handleErrorWithSentry();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { E2E_TEST_DSN } from '$env/static/private';
2+
import { handleErrorWithSentry, initCloudflareSentryHandle, sentryHandle } from '@sentry/sveltekit';
3+
import { sequence } from '@sveltejs/kit/hooks';
4+
5+
export const handleError = handleErrorWithSentry();
6+
7+
export const handle = sequence(
8+
initCloudflareSentryHandle({
9+
dsn: E2E_TEST_DSN,
10+
tracesSampleRate: 1.0,
11+
}),
12+
sentryHandle(),
13+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { PageServerLoad } from './$types';
2+
3+
export const load: PageServerLoad = async function load() {
4+
return {
5+
message: 'From server load function.',
6+
};
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
let { data } = $props();
3+
</script>
4+
5+
<h1>Welcome to SvelteKit</h1>
6+
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p>
7+
8+
<a href="/prerender-test">prerender test</a>
9+
10+
<p>{data.message}</p>
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import adapter from '@sveltejs/adapter-cloudflare';
2+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
3+
4+
/** @type {import('@sveltejs/kit').Config} */
5+
const config = {
6+
// Consult https://svelte.dev/docs/kit/integrations
7+
// for more information about preprocessors
8+
preprocess: vitePreprocess(),
9+
10+
kit: {
11+
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
12+
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
13+
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
14+
adapter: adapter(),
15+
prerender: {
16+
handleHttpError: 'ignore',
17+
},
18+
},
19+
};
20+
21+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from '@playwright/test';
2+
3+
test('home page has expected h1', async ({ page }) => {
4+
await page.goto('/');
5+
await expect(page.locator('h1')).toBeVisible();
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"checkJs": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"resolveJsonModule": true,
9+
"skipLibCheck": true,
10+
"sourceMap": true,
11+
"strict": true,
12+
"moduleResolution": "bundler"
13+
}
14+
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
15+
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
16+
//
17+
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
18+
// from the referenced tsconfig.json - TypeScript does not merge them in
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { sentrySvelteKit } from '@sentry/sveltekit';
2+
import { sveltekit } from '@sveltejs/kit/vite';
3+
import { defineConfig } from 'vite';
4+
5+
export default defineConfig({
6+
plugins: [sentrySvelteKit({ autoUploadSourceMaps: false }), sveltekit()],
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
compatibility_date = "2024-12-17"
2+
compatibility_flags = ["nodejs_compat"]

packages/sveltekit/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"./package.json": "./package.json",
1919
".": {
2020
"types": "./build/types/index.types.d.ts",
21+
"worker": {
22+
"import": "./build/esm/index.worker.js",
23+
"require": "./build/cjs/index.worker.js"
24+
},
2125
"browser": {
2226
"import": "./build/esm/index.client.js",
2327
"require": "./build/cjs/index.client.js"
@@ -38,6 +42,7 @@
3842
}
3943
},
4044
"dependencies": {
45+
"@sentry/cloudflare": "9.1.0",
4146
"@sentry/core": "9.1.0",
4247
"@sentry/node": "9.1.0",
4348
"@sentry/opentelemetry": "9.1.0",

packages/sveltekit/rollup.npm.config.mjs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollu
22

33
export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
5-
entrypoints: ['src/index.server.ts', 'src/index.client.ts', 'src/client/index.ts', 'src/server/index.ts'],
5+
entrypoints: [
6+
'src/index.server.ts',
7+
'src/index.client.ts',
8+
'src/index.worker.ts',
9+
'src/client/index.ts',
10+
'src/server/index.ts',
11+
'src/worker/index.ts',
12+
],
613
packageSpecificConfig: {
714
external: ['$app/stores'],
815
output: {

packages/sveltekit/src/common/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { HttpError, Redirect } from '@sveltejs/kit';
22

3+
export const WRAPPED_MODULE_SUFFIX = '?sentry-auto-wrap';
4+
35
export type SentryWrappedFlag = {
46
/**
57
* If this flag is set, we know that the load event was already wrapped once

packages/sveltekit/src/index.types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
export * from './client';
55
export * from './vite';
66
export * from './server';
7+
export * from './worker';
8+
9+
// Use the ./server version of some functions that are also exported from ./worker
10+
export { sentryHandle } from './server';
11+
// Use the ./worker version of some functions that are also exported from ./server
12+
export { initCloudflareSentryHandle } from './worker';
713

814
import type { Client, Integration, Options, StackParser } from '@sentry/core';
915
import type { HandleClientError, HandleServerError } from '@sveltejs/kit';
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './worker';
2+
// export * from './vite';

0 commit comments

Comments
 (0)