-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(sveltekit): Fix SvelteKit Cloudflare usage #14672
Open
SG60
wants to merge
15
commits into
getsentry:develop
Choose a base branch
from
SG60:patch-1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+603
−215
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fc47659
fix(sveltekit): Don't use node apis in cloudflare workers
SG60 31aa1d4
fix(sveltekit): provide a handle function to init cloudflare workers …
SG60 dc969b9
chore(sveltekit): refactor some common server-side code
SG60 218c2cf
chore(sveltekit): refactor the rest of the common server-side code
SG60 5aa9d54
fix(sveltekit): avoid importing fs and path in workers contexts
SG60 28b2fd3
Merge branch 'develop' into patch-1
SG60 dc510d2
test(sveltekit-cloudflare-pages): add an e2e test for @sentry/sveltek…
SG60 2c443a8
test(sveltekit-cloudflare): simpler e2e test
SG60 a43ecc4
test(sveltekit-cloudflare): rename e2e test dir to tests
SG60 1f27492
test(sveltekit-cloudflare): add test of prerendered page
SG60 db5e5bc
fix(sveltekit): fix prerender failure when using cloudflare workers
SG60 78724a2
test(sveltekit): refactor tests to use new server-common folder
SG60 d0d2dbb
Merge remote-tracking branch 'upstream/develop' into patch-1
SG60 df8f314
fix(sveltekit): remove deprecated API usage
SG60 ef78c1b
fix(sveltekit): use the new unified continueTrace function
SG60 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
test-results | ||
node_modules | ||
|
||
# Output | ||
.output | ||
.vercel | ||
.netlify | ||
.wrangler | ||
/.svelte-kit | ||
/build | ||
|
||
# OS | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# Env | ||
.env | ||
.env.* | ||
!.env.example | ||
!.env.test | ||
|
||
# Vite | ||
vite.config.js.timestamp-* | ||
vite.config.ts.timestamp-* |
3 changes: 3 additions & 0 deletions
3
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/.npmrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@sentry:registry=http://127.0.0.1:4873 | ||
@sentry-internal:registry=http://127.0.0.1:4873 | ||
engine-strict=true |
38 changes: 38 additions & 0 deletions
38
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# sv | ||
|
||
Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli). | ||
|
||
## Creating a project | ||
|
||
If you're seeing this, you've probably already done this step. Congrats! | ||
|
||
```bash | ||
# create a new project in the current directory | ||
npx sv create | ||
|
||
# create a new project in my-app | ||
npx sv create my-app | ||
``` | ||
|
||
## Developing | ||
|
||
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: | ||
|
||
```bash | ||
npm run dev | ||
|
||
# or start the server and open the app in a new browser tab | ||
npm run dev -- --open | ||
``` | ||
|
||
## Building | ||
|
||
To create a production version of your app: | ||
|
||
```bash | ||
npm run build | ||
``` | ||
|
||
You can preview the production build with `npm run preview`. | ||
|
||
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment. |
31 changes: 31 additions & 0 deletions
31
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "sveltekit-cloudflare-pages", | ||
"private": true, | ||
"version": "0.0.1", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite dev", | ||
"build": "vite build", | ||
"preview": "wrangler pages dev ./.svelte-kit/cloudflare --port 4173", | ||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", | ||
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", | ||
"test:e2e": "playwright test", | ||
"test": "npm run test:e2e", | ||
"test:build": "pnpm install && pnpm build", | ||
"test:assert": "npm run test:e2e" | ||
}, | ||
"dependencies": { | ||
"@sentry/sveltekit": "latest || *" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "^1.45.3", | ||
"@sveltejs/adapter-cloudflare": "^4.8.0", | ||
"@sveltejs/kit": "^2.9.0", | ||
"@sveltejs/vite-plugin-svelte": "^5.0.0", | ||
"svelte": "^5.0.0", | ||
"svelte-check": "^4.0.0", | ||
"typescript": "^5.0.0", | ||
"vite": "^6.0.0", | ||
"wrangler": "^3" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/playwright.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from '@playwright/test'; | ||
|
||
export default defineConfig({ | ||
webServer: { | ||
command: 'npm run build && npm run preview', | ||
port: 4173 | ||
}, | ||
|
||
testDir: 'tests' | ||
}); |
13 changes: 13 additions & 0 deletions
13
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/src/app.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// See https://svelte.dev/docs/kit/types#app.d.ts | ||
// for information about these interfaces | ||
declare global { | ||
namespace App { | ||
// interface Error {} | ||
// interface Locals {} | ||
// interface PageData {} | ||
// interface PageState {} | ||
// interface Platform {} | ||
} | ||
} | ||
|
||
export {}; |
12 changes: 12 additions & 0 deletions
12
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/src/app.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="icon" href="%sveltekit.assets%/favicon.png" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
%sveltekit.head% | ||
</head> | ||
<body data-sveltekit-preload-data="hover"> | ||
<div style="display: contents">%sveltekit.body%</div> | ||
</body> | ||
</html> |
8 changes: 8 additions & 0 deletions
8
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/src/hooks.client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { env } from '$env/dynamic/public'; | ||
import * as Sentry from '@sentry/sveltekit'; | ||
|
||
Sentry.init({ | ||
dsn: env.PUBLIC_E2E_TEST_DSN, | ||
}); | ||
|
||
export const handleError = Sentry.handleErrorWithSentry(); |
12 changes: 12 additions & 0 deletions
12
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/src/hooks.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { E2E_TEST_DSN } from '$env/static/private'; | ||
import { handleErrorWithSentry, initCloudflareSentryHandle, sentryHandle } from '@sentry/sveltekit'; | ||
import { sequence } from '@sveltejs/kit/hooks'; | ||
|
||
export const handleError = handleErrorWithSentry(); | ||
|
||
export const handle = sequence( | ||
initCloudflareSentryHandle({ | ||
dsn: E2E_TEST_DSN, | ||
}), | ||
sentryHandle(), | ||
); |
1 change: 1 addition & 0 deletions
1
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/src/lib/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// place files you want to import through the `$lib` alias in this folder. |
7 changes: 7 additions & 0 deletions
7
...ackages/e2e-tests/test-applications/sveltekit-cloudflare-pages/src/routes/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { PageServerLoad } from './$types'; | ||
|
||
export const load: PageServerLoad = async function load() { | ||
return { | ||
message: 'From server load function.', | ||
}; | ||
}; |
10 changes: 10 additions & 0 deletions
10
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/src/routes/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script lang="ts"> | ||
let { data } = $props(); | ||
</script> | ||
|
||
<h1>Welcome to SvelteKit</h1> | ||
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p> | ||
|
||
<a href="/prerender-test">prerender test</a> | ||
|
||
<p>{data.message}</p> |
9 changes: 9 additions & 0 deletions
9
...ts/test-applications/sveltekit-cloudflare-pages/src/routes/prerender-test/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { PageServerLoad } from './$types'; | ||
|
||
export const prerender = true; | ||
|
||
export const load: PageServerLoad = async function load() { | ||
return { | ||
message: 'From server load function.', | ||
}; | ||
}; |
6 changes: 6 additions & 0 deletions
6
...tests/test-applications/sveltekit-cloudflare-pages/src/routes/prerender-test/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script lang="ts"> | ||
let { data } = $props(); | ||
</script> | ||
|
||
<h1>{data.message}</h1> | ||
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p> |
Binary file added
BIN
+1.53 KB
...kages/e2e-tests/test-applications/sveltekit-cloudflare-pages/static/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions
18
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import adapter from "@sveltejs/adapter-cloudflare"; | ||
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; | ||
|
||
/** @type {import('@sveltejs/kit').Config} */ | ||
const config = { | ||
// Consult https://svelte.dev/docs/kit/integrations | ||
// for more information about preprocessors | ||
preprocess: vitePreprocess(), | ||
|
||
kit: { | ||
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list. | ||
// If your environment is not supported, or you settled on a specific environment, switch out the adapter. | ||
// See https://svelte.dev/docs/kit/adapters for more information about adapters. | ||
adapter: adapter() | ||
} | ||
}; | ||
|
||
export default config; |
11 changes: 11 additions & 0 deletions
11
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/tests/demo.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { expect, test } from '@playwright/test'; | ||
|
||
test('home page has expected h1', async ({ page }) => { | ||
await page.goto('/'); | ||
await expect(page.locator('h1')).toBeVisible(); | ||
}); | ||
|
||
test('prerendered page has expected h1', async ({ page }) => { | ||
await page.goto('/prerender-test'); | ||
await expect(page.locator('h1')).toHaveText('From server load function.'); | ||
}); |
19 changes: 19 additions & 0 deletions
19
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "./.svelte-kit/tsconfig.json", | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"checkJs": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"strict": true, | ||
"moduleResolution": "bundler" | ||
} | ||
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias | ||
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files | ||
// | ||
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes | ||
// from the referenced tsconfig.json - TypeScript does not merge them in | ||
} |
7 changes: 7 additions & 0 deletions
7
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { sveltekit } from '@sveltejs/kit/vite'; | ||
import { defineConfig } from 'vite'; | ||
import { sentrySvelteKit } from '@sentry/sveltekit'; | ||
|
||
export default defineConfig({ | ||
plugins: [sentrySvelteKit({ autoUploadSourceMaps: false }), sveltekit()], | ||
}); |
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/sveltekit-cloudflare-pages/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
compatibility_date = "2024-12-17" | ||
compatibility_flags = ["nodejs_compat"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './worker'; | ||
// export * from './vite'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not directly applicable to this line but: I had to make this change to the
node
export to start the build:However, even after this change, I get another error a bit later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @Lms24 if this isn't the right place to discuss this, but is there any reason why we don't make this change in a separate PR?
I'm having an issue where trying to import
import * as Sentry from '@sentry/sveltekit';
using ESM in Node.JS has all the@sentry/node
imports underSentry.default
, and the above change seems to fix it.From reading #9872 (comment), it looks like ESM used to be broken, but I think 7f2e804 might have fixed it? But a similar change for remix seems to have not worked: #12742
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aloisklink sorry for only seeing your comment now: I agree, we should make this change separately. Thing is, as far as I know, SvelteKit transpiles to CJS (at least for Node-based environments). So I'm a bit hesitant to add an ESM entry point, especially because this works even less well than it currently works in CJS, with OpenTelemetry.
Feel free to submit a PR and let's see what our tests have to see