GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(sveltekit): Document new `wrapServerRouteWithSentry` wrapper by Lms24 · Pull Request #10990 · getsentry/sentry-docs

feat(sveltekit): Document new wrapServerRouteWithSentry wrapper #10990

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.

Sign up for GitHub

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

Merged
Lms24 merged 3 commits into master from lms/feat-sveltekit-wrapServerRouteWithSentry-docs
Aug 9, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat(sveltekit): Document new wrapServerRouteWithSentry wrapper #10990

Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
8f12c70
feat(sveltekit): Document new `wrapServerRouteWithSentry` wrapper
Lms24 Aug 6, 2024
8f82a74
Apply suggestions from code review
Lms24 Aug 9, 2024
e76bb5f
Update docs/platforms/javascript/guides/sveltekit/manual-setup.mdx
Lms24 Aug 9, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

104 changes: 69 additions & 35 deletions 104 docs/platforms/javascript/guides/sveltekit/manual-setup.mdx
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export default {

If you disable automatic source maps upload, you must explicitly set a `release` value in your `Sentry.init()` configs. You can also skip the `sentry-cli` configuration step below.

### Configure Auto-Instrumentation
### Auto-Instrumentation

The SDK primarily uses [SvelteKit's hooks](https://kit.svelte.dev/docs/hooks) to collect error and performance data. However, SvelteKit doesn't yet offer a hook for universal or server-only `load` function calls. Therefore, the SDK uses a Vite plugin to auto-instrument `load` functions so that you don't have to add a Sentry wrapper to each function manually.

Expand Down Expand Up @@ -415,9 +415,59 @@ export default {
};
```

#### Instrument `load` Functions Manually
#### Configure Client-side `fetch` Instrumentation

<Note>

Available since version `7.91.0`

</Note>

The `sentryHandle` function you added to your `handle` hook in `hooks.server.ts` during [server-side setup](#server-side-setup) injects an inline `<script>` tag into the HTML response of the server.
This script attempts to proxy all client-side `fetch` calls, so that `fetch` calls inside your `load` functions are captured by the SDK.
However, if you configured CSP rules to block inline fetch scripts by default, this script will be [blocked by the browser](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_inline_script).
To enable the script, you need to add an exception for the `sentryHandle` script:

First, specify your nonce in the `fetchProxyScriptNonce` option in your `sentryHandle` call:

```javascript {filename:hooks.server.ts}
// Add the nonce to the <script> tag:
export const handle = sentryHandle({ fetchProxyScriptNonce: "<your-nonce>" });
```

Then, adjust your [SvelteKit CSP configuration](https://kit.svelte.dev/docs/configuration#csp):

If you don't want to use auto-instrumentation, you can also manually instrument specific `load` functions with the SDK's `load` function wrappers.
```javascript {svelte.config.js} {5}
const config = {
kit: {
csp: {
directives: {
"script-src": ["nonce-<your-nonce>"],
},
},
},
};
```

##### Disable Client-side `fetch` Proxy Script

If you do not want to inject the script responsible for instrumenting client-side `load` calls, you can disable injection by passing `injectFetchProxyScript: false` to `sentryHandle`:

```javascript {filename:hooks.server.ts}
export const handle = sentryHandle({ injectFetchProxyScript: false });
```

Note that if you disable the `fetch` proxy script, the SDK will not be able to capture spans for `fetch` calls made in your `load` functions on the client.
This also means that potential spans created on the server for these `fetch` calls will not be connected to the client-side trace.

### Manual Instrumentation

Instead or in addition to [Auto Instrumentation](#auto-instrumentation), you can manually instrument certain SvelteKit-specific features with the SDK:

#### Instrument `load` Functions

SvelteKit's universal and server `load` functions are instrumented automatically by default.
If you don't want to use `load` auto-instrumentation, you can [disable it](#disable-auto-instrumentation), and manually instrument specific `load` functions with the SDK's `load` function wrappers.

##### Universal `load` Functions

Expand Down Expand Up @@ -467,47 +517,31 @@ export const load = wrapServerLoadWithSentry(({ event }) => {
});
```

#### Configure Client-side `fetch` Instrumentation
#### Instrument Server Routes

<Note>

Available since version `7.91.0`
Available since `8.25.0`

</Note>

The `sentryHandle` function you added to your `handle` hook in `hooks.server.ts` during [server-side setup](#server-side-setup) injects a small inline `<script>` tag into the HTML response of the server.
This script attempts to proxy all client-side `fetch` calls, so that `fetch` calls inside your `load` functions are captured by the SDK.
However, if you configured CSP rules to block inline fetch scripts by default, this script will be [blocked by the browser](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_inline_script).
To enable the script, you need to add an exception for the `sentryHandle` script:

First, specify your nonce in the `fetchProxyScriptNonce` option in your `sentryHandle` call:

```javascript {filename:hooks.server.ts}
// Add the nonce to the <script> tag:
export const handle = sentryHandle({ fetchProxyScriptNonce: "<your-nonce>" });
```
You can also manually instrument [server (API) routes ](https://kit.svelte.dev/docs/routing#server) with the SDK.
This is useful if you have custom server routes that you want to trace or if you want to capture `error()` calls within your server routes:

Then, adjust your [SvelteKit CSP configuration](https://kit.svelte.dev/docs/configuration#csp):
```javascript {filename:+server.js} {1,3}
import { wrapServerRouteWithSentry } from "@sentry/sveltekit";

```javascript {svelte.config.js} {5}
const config = {
kit: {
csp: {
directives: {
"script-src": ["nonce-<your-nonce>"],
},
},
},
};
export const GET = wrapServerRouteWithSentry(async () => {
// your endpoint logic
return new Response("Hello World");
});
```

##### Disable Client-side `fetch` Proxy Script

If you do not want to inject the script responsible for instrumenting client-side `load` calls, you can disable injection by passing `injectFetchProxyScript: false` to `sentryHandle`:
```typescript {filename:+server.ts} {1,3}
Comment thread

@nicohrubec nicohrubec Aug 9, 2024

Copy link
Copy Markdown
Member

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.

Why is this duplicated for ts and js?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Lms24 Lms24 Aug 9, 2024

Copy link
Copy Markdown
Member Author

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.

because you can use JS and TS simultaneously in SvelteKit. But most of these snippets have no TS-specific syntax, so it might be better to switch to a shared filename.(js|ts) snippet

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Lms24 Lms24 Aug 9, 2024

Copy link
Copy Markdown
Member Author

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.

I'm realizing, we're a bit inconsistent with the languages and even diverge a bit. I'll dedupe the snippets.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Lms24 Lms24 Aug 9, 2024

Copy link
Copy Markdown
Member Author

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.

Opened an additional PR because I streamlined some other things while removing the TS stuff: #11038

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

nicohrubec reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
import { wrapServerRouteWithSentry } from "@sentry/sveltekit";

```javascript {filename:hooks.server.ts}
export const handle = sentryHandle({ injectFetchProxyScript: false });
export const GET = wrapServerRouteWithSentry(async () => {
// your endpoint logic
return new Response("Hello World");
});
```

Note that if you disable the `fetch` proxy script, the SDK will not be able to capture spans for `fetch` calls made in your `load` functions on the client.
This also means that potential spans created on the server for these `fetch` calls will not be connected to the client-side trace.
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.