ObserverKit

React Router

Track production errors in a React Router v7 app: initialize in app/entry.client.tsx, and upload client and server source maps at build time with the @observerkit/vite plugin.

1. Install the SDK

Install the package

terminal
pnpm add @observerkit/js

Initialize

Call init as early as possible in your application entry point. Uncaught errors and unhandled promise rejections are captured automatically.

app/entry.client.tsx
import { init } from "@observerkit/js"

init({
  projectKey: "ok_proj_...",
  environment: "production",
})

Options

projectKeystringRequired. Your project key.
autoCapturebooleanCapture uncaught errors automatically. Defaults to true.
environmentstringEnvironment name, e.g. "production" or "staging".
endpointURLCustom ingest API URL.
servicestringOptional. Distinguishes services within one project, e.g. multiple apps in a monorepo. Auto-created on first event, up to 20 per project.

Capture errors manually

Errors are captured automatically by default. You can also report them manually with additional metadata.

file.ts
import { captureException } from "@observerkit/js"

try {
  await fetchUserData()
} catch (error) {
  captureException(error, {
    metadata: { userId: "usr_123" },
  })
}

Verify

Trigger a test error in your application. If everything is set up correctly, it will appear in your dashboard within a few seconds.

<button
  onClick={() => {
    throw new Error("test error")
  }}
>
  Throw test error
</button>

2. Upload source maps

Install the bundler plugin

terminal
pnpm add -D @observerkit/vite

The plugin uploads source maps on each build and injects debug IDs for accurate stack trace mapping.

Configure

Add the plugin to your Vite config. Source maps are enabled automatically.

vite.config.ts
import { reactRouter } from "@react-router/dev/vite"
import { defineConfig } from "vite"
import observerkit from "@observerkit/vite"

export default defineConfig({
  plugins: [
    reactRouter(),
    observerkit({
      projectKey: "ok_proj_...",
    }),
  ],
})

Framework mode builds your app in several environments, each writing to its own directory (build/client and build/server). The plugin uploads each one automatically. No extra configuration is needed, and you should not set build.outDir to make it work. This requires @observerkit/vite 0.6.1 or later.

Options

projectKeystringRequired. Your project key.
endpointstringIngest API URL. Defaults to ingest.observerkit.com.
deleteAfterUploadbooleanRemove .map files from output after upload and strip sourceMappingURL references so nothing dangles. Defaults to true.

Verify the build uploaded

A production build prints one upload summary per output directory. A framework that builds several environments prints one per environment.

[ObserverKit] Enabling build.sourcemap for source map upload
[ObserverKit] Uploaded 12 source map(s) from /app/build/client
[ObserverKit] Deleted 12 .map files from output
[ObserverKit] Uploaded 3 source map(s) from /app/build/server
[ObserverKit] Deleted 3 .map files from output

If you see Output directory not found or No .map files found in, the plugin scanned somewhere your build does not write to. The message names the path it tried. Transient upload failures are retried twice and log each attempt, so a slow build is explicable rather than mysterious.

No .map file should remain in your output afterwards. With deleteAfterUpload left on, running find build -name "*.map" should print nothing.

You do not have to catch this in a build log. If your app sends errors referencing a build whose source maps never arrived, your project settings page says so, along with the date of your last successful upload. A project that has never uploaded and one that stopped uploading are called out differently, because they point at different causes.

Verify a stack trace resolves

Drop a button into your app that throws, trigger it after a production build, then check your dashboard. The stack trace should show original file names and line numbers.

<button
  onClick={() => {
    throw new Error("test error")
  }}
>
  Throw test error
</button>