SolidStart

Learn how to set up Sentry in your SolidStart application and capture your first errors.

You need:

Choose the features you want to configure, and this guide will show you how:

Want to learn more about these features?
  • Issues (always enabled): Sentry's core error monitoring product that automatically reports errors, uncaught exceptions, and unhandled rejections. If you have something that looks like an exception, Sentry can capture it.
  • Tracing: Track software performance while seeing the impact of errors across multiple systems. For example, distributed tracing allows you to follow a request from the frontend to the backend and back.
  • Session Replay: Get to the root cause of an issue faster by viewing a video-like reproduction of what was happening in the user's browser before, during, and after the problem.
  • Logs: Centralize and analyze your application logs to correlate them with errors and performance issues. Search, filter, and visualize log data to understand what's happening in your applications.

Run the command for your preferred package manager to add the Sentry SDK to your application:

Copied
npm install @sentry/solidstart --save

You need to initialize and configure the Sentry SDK in two places: the client side and the server side.

Import and initialize the Sentry SDK in your /src/entry-client.tsx file.

If you're using Solid Router, make sure to import and add the solidRouterBrowserTracingIntegration to enable tracing in your app:

src/entry-client.tsx
Copied
import * as Sentry from "@sentry/solidstart";
// ___PRODUCT_OPTION_START___ performance
// import solidRouterBrowserTracingIntegration if you're using Solid Router
import { solidRouterBrowserTracingIntegration } from "@sentry/solidstart/solidrouter";
// ___PRODUCT_OPTION_END___ performance
import { mount, StartClient } from "@solidjs/start/client";

Sentry.init({
  dsn: "___PUBLIC_DSN___",
  // Adds request headers and IP for users, for more info visit:
  // https://docs.sentry.io/platforms/javascript/guides/solidstart/configuration/options/#sendDefaultPii
  sendDefaultPii: true,
    // ___PRODUCT_OPTION_START___ performance
    // add solidRouterBrowserTracingIntegration if you're using Solid Router
    solidRouterBrowserTracingIntegration(),
    // ___PRODUCT_OPTION_END___ performance
    // ___PRODUCT_OPTION_START___ session-replay
    // Replay may only be enabled for the client-side
    Sentry.replayIntegration(),
    // ___PRODUCT_OPTION_END___ session-replay
    // ___PRODUCT_OPTION_START___ user-feedback
    Sentry.feedbackIntegration({
      // Additional SDK configuration goes in here, for example:
      colorScheme: "system",
    }),
    // ___PRODUCT_OPTION_END___ user-feedback
  // ___PRODUCT_OPTION_START___ performance

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  tracesSampleRate: 1.0,
  // ___PRODUCT_OPTION_END___ performance
  // ___PRODUCT_OPTION_START___ session-replay

  // Capture Replay for 10% of all sessions,
  // plus for 100% of sessions with an error
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
  // ___PRODUCT_OPTION_END___ session-replay
  // ___PRODUCT_OPTION_START___ logs

  // Enable logs to be sent to Sentry
  enableLogs: true,
  // ___PRODUCT_OPTION_END___ logs
});

mount(() => <StartClient />, document.getElementById("app"));

Create a file named instrument.server.ts in your src folder. In this file, initialize and import Sentry for your server:

src/instrument.server.ts
Copied
import * as Sentry from "@sentry/solidstart";

Sentry.init({
  dsn: "___PUBLIC_DSN___",
  // Adds request headers and IP for users, for more info visit:
  // https://docs.sentry.io/platforms/javascript/guides/solidstart/configuration/options/#sendDefaultPii
  sendDefaultPii: true,
  // ___PRODUCT_OPTION_START___ performance

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  tracesSampleRate: 1.0,
  // ___PRODUCT_OPTION_END___ performance
  // ___PRODUCT_OPTION_START___ logs

  // Enable logs to be sent to Sentry
  enableLogs: true,
  // ___PRODUCT_OPTION_END___ logs
});

The Sentry SDK provides middleware lifecycle handlers that enhance the data collected by Sentry on the server side, enabling distributed tracing between the client and server.

Create or update your src/middleware.ts file and add the sentryBeforeResponseMiddleware handler:

src/middleware.ts
Copied
import { sentryBeforeResponseMiddleware } from "@sentry/solidstart";
import { createMiddleware } from "@solidjs/start/middleware";

export default createMiddleware({
  onBeforeResponse: [
sentryBeforeResponseMiddleware(),
// Add your other middleware handlers after `sentryBeforeResponseMiddleware` ], });

Wrap your SolidStart config in app.config.ts with withSentry so that the instrumentation file gets included in your build output. Then, specify the middleware that you've just created:

app.config.ts
Copied
import { withSentry } from "@sentry/solidstart";
import { defineConfig } from "@solidjs/start/config";

export default defineConfig(
withSentry( { // other SolidStart config options... middleware: "./src/middleware.ts", }, { // Your Sentry build-time config (such as source map upload options) // optional: if your `instrument.server.ts` file is not located inside `src` instrumentation: "./mypath/instrument.server.ts", }, ),
);

If you're using Solid Router and the Sentry solidRouterBrowserTracingIntegration integration, wrap your Solid Router with withSentryRouterRouting to enable Sentry to collect navigation spans:

app.tsx
Copied
import { Router } from "@solidjs/router";
import { FileRoutes } from "@solidjs/start/router";
import { withSentryRouterRouting } from "@sentry/solidstart/solidrouter";

const SentryRouter = withSentryRouterRouting(Router);
export default function App() { return (
<SentryRouter>
<FileRoutes />
</SentryRouter>
); }

Instrumentation needs to happen as early as possible to make sure Sentry works as intended. To do this, add an --import flag to the NODE_OPTIONS environment variable when you run your application and set it to import the instrumentation file created by the build output: .output/server/instrument.server.mjs.

For example, update your scripts in package.json.

package.json
Copied
{
  "scripts": {
    "start:vinxi": "NODE_OPTIONS='--import ./.output/server/instrument.server.mjs ' vinxi start",
    "start:node": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs"
  }
}

If you're not able to use the --import flag, check the alternative installation methods.

To automatically report exceptions from inside a component tree to Sentry, wrap Solid's ErrorBoundary with Sentry's helper function:

Copied
import * as Sentry from "@sentry/solidstart";
import { ErrorBoundary } from "solid-js";

// Wrap Solid"s ErrorBoundary to automatically capture exceptions
const SentryErrorBoundary = Sentry.withSentryErrorBoundary(ErrorBoundary);

export default function SomeComponent() {
  return (
    <SentryErrorBoundary
      fallback={(err) => <div>Error: {err.message}</div>}
    >
      <div>Some Component</div>
    </SentryErrorBoundary>
  );
}

To upload source maps for clear error stack traces, add your Sentry auth token, organization, and project slug in your SolidStart configuration:

app.config.ts
Copied
import { withSentry } from '@sentry/solidstart';
import { defineConfig } from '@solidjs/start/config';

export default defineConfig(
  withSentry(
    {
      /* Your SolidStart config */
    },
    {
      org: "___ORG_SLUG___",
      project: "___PROJECT_SLUG___",
      // store your auth token in an environment variable
      authToken: process.env.SENTRY_AUTH_TOKEN,
    }
  ),
);

To keep your auth token secure, always store it in an environment variable instead of directly in your files:

.env
Copied
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___

Alternatively, you can create a .env.sentry-build-plugin file:

.env.sentry-build-plugin
Copied
SENTRY_ORG="___ORG_SLUG___"
SENTRY_PROJECT="___PROJECT_SLUG___"
SENTRY_AUTH_TOKEN="___ORG_AUTH_TOKEN___"

You can prevent ad blockers from blocking Sentry events using tunneling. Use the tunnel option to add an API endpoint in your application that forwards Sentry events to Sentry servers.

To enable tunneling, update Sentry.init with the following option:

Copied
Sentry.init({
  dsn: "___PUBLIC_DSN___",
tunnel: "/tunnel",
});

This will send all events to the tunnel endpoint. However, the events need to be parsed and redirected to Sentry, so you'll need to do additional configuration on the server. You can find a detailed explanation on how to do this on our Troubleshooting page.

Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.

To verify that Sentry captures errors and creates issues in your Sentry project, add a test button to an existing page or create a new one:

Copied
<button
  type="button"
  onClick={() => {
    throw new Error("Sentry Test Error");
  }}
>
  Break the world
</button>;

To test tracing, create a test API route like src/routes/sentry-test.tsx:

sentry-test.tsx
Copied
export async function GET() {
  throw new Error("Sentry Example API Route Error");
}

Next, update your test button to call this route and throw an error if the response isn't ok:

Copied
<button
  type="button"
  onClick={() => {
    Sentry.startSpan(
      {
        op: "test",
        name: "My First Test Transaction",
      },
      async () => {
        const res = await fetch("/sentry-test");
        if (!res.ok) {
          throw new Error("Sentry Test Error");
        }
      },
    );
  }}
>
  Break the world
</button>;

Open the page in the browser and click the button to trigger a frontend error, an error in the API route, and a trace to measure the time it takes for the API request to complete.

Now, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).

Need help locating the captured errors in your Sentry project?
  1. Open the Issues page and select an error from the issues list to view the full details and context of this error. For more details, see this interactive walkthrough.
  2. Open the Traces page and select a trace to reveal more information about each span, its duration, and any errors. For an interactive UI walkthrough, click here.
  3. Open the Replays page and select an entry from the list to get a detailed view where you can replay the interaction and get more information to help you troubleshoot.
  4. Open the Logs page and filter by service, environment, or search keywords to view log entries from your application. For an interactive UI walkthrough, click here.

At this point, you should have integrated Sentry into your SolidStart application and should already be sending data to your Sentry project.

Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:

Are you having problems setting up the SDK?
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").