Remix

Features Overview

Under the hood, Remix SDK relies on our React SDK on the frontend and Node SDK on the backend, which makes all features available in those SDKs also available in this SDK.

On this page, we get you up and running with Sentry's SDK, so that it will automatically report errors and exceptions in your application.

Don't already have an account and Sentry project established? Head over to sentry.io, then return to this page.

Install

Sentry captures data by using an SDK within your application’s runtime.

Copied
npm install --save @sentry/remix

Configure

Configuration should happen as early as possible in your application's lifecycle.

To use this SDK, initialize Sentry in your Remix entry points for both the client and server.

Create two files in the root directory of your project, entry.client.tsx and entry.server.tsx (if they don't exist yet). In these files, add your initialization code for the client-side SDK and server-side SDK, respectively.

The two configuration types are mostly the same, except that some configuration features, like Session Replay, only work in entry.client.tsx.

entry.client.tsx
Copied
import { useLocation, useMatches } from "@remix-run/react";
import * as Sentry from "@sentry/remix";
import { useEffect } from "react";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [
    new Sentry.BrowserTracing({
      routingInstrumentation: Sentry.remixRouterInstrumentation(
        useEffect,
        useLocation,
        useMatches
      ),
    }),
    // Replay is only available in the client
    new Sentry.Replay(),
  ],

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // Capture Replay for 10% of all sessions,
  // plus for 100% of sessions with an error
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
});

Initialize Sentry in your entry point for the server to capture exceptions and get performance metrics for your action and loader functions. You can also initialize Sentry's database integrations, such as Prisma, to get spans for your database calls.

entry.server.tsx
Copied
import { prisma } from "~/db.server";

import * as Sentry from "@sentry/remix";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  tracesSampleRate: 1,
  integrations: [new Sentry.Integrations.Prisma({ client: prisma })],
  // ...
});

If you use a custom Express server in your Remix application, you should wrap your createRequestHandler function manually with wrapExpressCreateRequestHandler. This is not required if you use the built-in Remix App Server.

server/index.ts
Copied
import { wrapExpressCreateRequestHandler } from "@sentry/remix";
import { createRequestHandler } from "@remix-run/express";

// ...

const createSentryRequestHandler =
  wrapExpressCreateRequestHandler(createRequestHandler);

// Use createSentryRequestHandler like you would with createRequestHandler
app.all("*", createSentryRequestHandler(/* ... */));

Also, wrap your Remix root with withSentry to catch React component errors and to get parameterized router transactions.

root.tsx
Copied
import {
  Links,
  LiveReload,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
} from "@remix-run/react";

import { withSentry } from "@sentry/remix";

function App() {
  return (
    <html>
      <head>
        <Meta />
        <Links />
      </head>
      <body>
        <Outlet />
        <ScrollRestoration />
        <Scripts />
        <LiveReload />
      </body>
    </html>
  );
}

export default withSentry(App);

You can disable or configure ErrorBoundary using a second parameter to withSentry.

Copied
withSentry(App, {
  wrapWithErrorBoundary: false,
});

// or

withSentry(App, {
  errorBoundaryOptions: {
    fallback: <p>An error has occurred</p>,
  },
});

Once you've done this set up, the SDK will automatically capture unhandled errors and promise rejections, and monitor performance in the client. You can also manually capture errors.

Verify

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

routes/error.tsx
Copied
<button
  type="button"
  onClick={() => {
    throw new Error("Sentry Frontend Error");
  }}
>
  Throw error
</button>

This snippet adds a button that throws an error in a component or page.

Then, throw an error in a loader or action.

routes/error.tsx
Copied
export const action: ActionFunction = async ({ request }) => {
  throw new Error("Sentry Error");
};

To view and resolve the recorded error, log into sentry.io and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

Add Readable Stack Traces to Errors

Depending on how you've set up your JavaScript project, the stack traces in your Sentry errors probably don't look like your actual code.

To fix this, head over to our source maps documentation where you'll learn how to upload source maps, so you can make sense of your stack traces.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").