SvelteKit

Sentry's SvelteKit SDK enables automatic reporting of errors and performance data.

The minimum supported SvelteKit version is 1.0.0 but we recommend using SvelteKit version 1.24.0 or newer for best performance. This SDK works best with Vite 4.2 and newer. Older Vite versions might not generate source maps correctly.

The SvelteKit SDK is designed to work out of the box with the following SvelteKit adapters:

  • Adapter-auto - for Vercel; other platforms might work but we don't guarantee compatibility at this time.
  • Adapter-vercel - only for Node (Lambda) runtimes, not yet Vercel's edge runtime.
  • Adapter-node.

Other adapters may work but aren't currently supported. We're looking into extending first-class support to more adapters in the future.

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

Sentry 通过在应用程序运行时使用 SDK 来捕获数据。

We recommend installing the SDK by running our installation wizard in the root directory of your project:

Copied
npx @sentry/wizard@latest -i sveltekit

The wizard will prompt you to log in to Sentry. It will then automatically do the following steps for you:

  • create or update SvelteKit files with the default Sentry configuration:
    • hooks.(client|server).js to initialize the SDK and instrument SvelteKit's hooks
    • vite.config.js to add source maps upload and auto-instrumentation via Vite plugins.
  • create a .sentryclirc file with an auth token to upload source maps (this file is automatically added to .gitignore)
  • add an example page to your app to verify your Sentry setup

After the wizard setup is completed, the SDK will automatically capture unhandled errors, and monitor performance. You can also manually capture errors.

If the setup through the wizard doesn't work for you, you can also set up the SDK manually.

配置应在应用程序生命周期的尽可能早阶段进行。

To complete your configuration, add options to your Sentry.init() calls. Here you can also set context data - data about the user, for example, or tags, or even arbitrary data - which will be added to every event sent to Sentry.

此代码片段包含一个有意的错误,因此您可以立即测试设置是否正常工作。

Add a button to a frontend component that throws an error:

src/routes/sentry/+page.svelte
Copied
<button
  on:click={() => {
    throw new Error("Sentry Frontend Error");
  }}
>
  Throw error
</button>;

Or throw an error in one of your load functions:

src/routes/sentry/+page.js
Copied
export const load = () => {
  throw new Error("Sentry Load Error");
};

Or throw an error in an API route:

src/routes/sentry/+server.js
Copied
export const GET = () => {
  throw new Error("Sentry API Error");
};

The possibilities are endless!

Errors triggered from within Browser DevTools are sandboxed and will not trigger error monitoring. Keep this in mind when verifying your Sentry SDK installation.

了解更多关于手动捕获错误或消息的信息,请参阅我们的使用文档

要查看和解决记录的错误,请登录 sentry.io 并选择您的项目。单击错误标题将打开一个页面,您可以在其中查看详细信息并将其标记为已解决。