AleannLab

Your Production App Has No Error Monitoring: A 30-Minute Sentry Setup for Next.js

AleannLab team2 min read

Install @sentry/nextjs, add instrumentation-client.ts and instrumentation.ts, wrap next.config.js with withSentryConfig, and report errors from global-error.tsx and your error.tsx boundaries. On the free plan, sample traces at a few percent and send every error. The whole setup takes about 30 minutes.

On this page
  1. 1. Install
  2. 2. Client side
  3. 3. Server and edge
  4. 4. Error boundaries
  5. 5. Build config
  6. 6. Test it
  7. 7. Route alerts to a human
  8. What it caught on day three
  9. Free plan budget

We run a lot of client projects with Sentry. Our own marketing site had none. We found out when a colleague's Safari crashed on our homepage and we had zero record of it (the story is here). This is the setup we added the same day.

1. Install

bash
npm install @sentry/nextjs

Create a project in Sentry (platform: Next.js) and copy the DSN from Settings → Client Keys. The DSN is public by design: it only lets a client send events, so it is safe in the browser bundle.

2. Client side

Next.js 15 loads src/instrumentation-client.ts in the browser before your app code.

TypeScript
import * as Sentry from '@sentry/nextjs';

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  enabled: process.env.NODE_ENV === 'production',
  environment: process.env.NEXT_PUBLIC_VERCEL_ENV,
  tracesSampleRate: 0.05,
  sendDefaultPii: false,
  ignoreErrors: [
    'ResizeObserver loop limit exceeded',
    'ResizeObserver loop completed with undelivered notifications',
  ],
});

export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;

enabled keeps local development out of your quota. tracesSampleRate: 0.05 keeps performance data at 5% of page loads while errors still go through at 100%.

3. Server and edge

src/instrumentation.ts runs once per server instance:

TypeScript
import * as Sentry from '@sentry/nextjs';

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs' || process.env.NEXT_RUNTIME === 'edge') {
    Sentry.init({
      dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
      enabled: process.env.NODE_ENV === 'production',
      tracesSampleRate: 0.05,
    });
  }
}

export const onRequestError = Sentry.captureRequestError;

onRequestError is the hook that reports errors thrown in Server Components, route handlers and middleware.

4. Error boundaries

This step gets skipped most often. Next.js catches render errors in error.tsx, shows your fallback, and the error never reaches Sentry unless you send it:

TSX
'use client';
import * as Sentry from '@sentry/nextjs';
import { useEffect } from 'react';

export default function ErrorBoundary({ error }: { error: Error }) {
  useEffect(() => { Sentry.captureException(error); }, [error]);
  return <YourFallback />;
}

Do the same in app/global-error.tsx, which catches errors in the root layout. Our old error.tsx rendered a 404 page and swallowed the error. Every crash looked like a missing page.

5. Build config

JavaScript
const { withSentryConfig } = require('@sentry/nextjs/config');

module.exports = withSentryConfig(nextConfig, {
  org: 'your-org',
  project: 'your-project',
  authToken: process.env.SENTRY_AUTH_TOKEN,
  sourcemaps: { disable: !process.env.SENTRY_AUTH_TOKEN },
  telemetry: false,
});

Without SENTRY_AUTH_TOKEN the build skips source map upload and your stack traces stay minified. Add the token in Vercel once you start reading errors seriously.

6. Test it

Deploy a preview and run this in the browser console:

JavaScript
setTimeout(() => { throw new Error('Sentry setup test'); }, 0);

The setTimeout matters. An error thrown directly in the console does not reach the global handler. Within a few seconds the issue should appear in Sentry.

7. Route alerts to a human

The default alert emails the account owner. We created the Sentry account on an email address that forwards into a Slack channel, so new issues land where the team already reads. Any channel works, as long as someone looks at it every day.

What it caught on day three

Three days after setup Sentry reported an unhandled error on /en/blog: Please check if your server is running. The breadcrumbs showed the real story. Our CMS had answered 502 with an HTML error page, the fetch helper called response.json() on it, and a cold server instance with no cached copy rendered a crash for the visitor.

Nobody had reported it. The fix took 20 minutes: retry 5xx responses twice with a short backoff, fail fast on 4xx, and put the status code in the error message so Sentry groups by cause.

Free plan budget

The free tier is enough for most marketing sites and early products if you:

  • send all errors, sample traces at 1-5%,
  • ignore known noise (ResizeObserver, browser extensions),
  • turn off session replay until you need it.

If your app already has thousands of errors a day and nobody knows where to start, that is a different job. We cover it in our app rescue and security audit.

All articles →

Gotowy, aby omówić swój projekt z nami?

idea
*
*
*
Interesuje mnie...
Tworzenie stron internetowych
Tworzenie aplikacji
Projektowanie UX/UI
DevOps
Sztuczna inteligencja i uczenie maszynowe
Testowanie QA
Tworzenie aplikacji mobilnych
Konsultacja
Outsourcing
Budżet projektu (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Prześlij plik