SolidStart
Learn how to set up Sentry in your SolidStart application and capture your first errors.
Important
This SDK is currently in beta. Beta features are still in progress and may have bugs. Please reach out on GitHub if you have any feedback or concerns.
This SDK guide is specifically for SolidStart. For instrumenting your Solid app, see our Solid SDK.
You need:
Choose the features you want to configure, and this guide will show you how:
Run the command for your preferred package manager to add the Sentry SDK to your application:
npm install @sentry/solidstart --save
You need to initialize and configure the Sentry SDK in two places: the client side and the server side.
The examples in this guide will use TypeScript with a src folder structure. Make sure to adjust the file paths and extensions (.js, .jsx, .ts, .tsx) to match your project setup.
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.tsximport * 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.tsimport * 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.tsimport { 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.tsimport { 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.tsximport { 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.
Run your build command to generate the instrument.server.mjs file before running your app. Depending on your build preset, the location of the file can differ. To find out where the file is located, monitor the build log output for:
[Sentry SolidStart withSentry] Successfully created /my/project/path/.output/server/instrument.server.mjs.
For example, update your scripts in package.json.
package.json{
"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:
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.tsimport { 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:
.envSENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
Alternatively, you can create a .env.sentry-build-plugin file:
.env.sentry-build-pluginSENTRY_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:
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:
<button
type="button"
onClick={() => {
throw new Error("Sentry Test Error");
}}
>
Break the world
</button>;
Open the page in a browser and click the button to trigger a frontend error.
Important
Errors triggered from within your browser's developer tools (like the browser console) are sandboxed, so they will not trigger Sentry's error monitoring.
To test tracing, create a test API route like src/routes/sentry-test.tsx:
sentry-test.tsxexport 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:
<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).
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:
- Explore practical guides on what to monitor, log, track, and investigate after setup
- Learn how to manually capture errors
- Continue to customize your configuration
- Learn how to make use of SolidStart-specific features
- Get familiar with Sentry's product features like tracing, insights, and alerts
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").