Next.js

Altix ErrorTrack unterstützt das Sentry-Protokoll, sodass Sie das offizielle @sentry/nextjs-SDK verwenden. Die folgenden Code-Snippets nutzen einen Platzhalter-DSN; auf der Einrichtungsseite Ihres Projekts finden Sie die echten Werte.

  1. SDK installieren

    Fügen Sie @sentry/nextjs hinzu, das offizielle Sentry-SDK. Es funktioniert unverändert mit Altix ErrorTrack.

    bash

    npm install @sentry/nextjs --save
  2. Konfigurieren

    Initialisieren Sie das SDK so früh wie möglich in Ihrer App mit Ihrer DSN.

    instrumentation-client.ts

    import * as Sentry from "@sentry/nextjs";
    
    Sentry.init({
      dsn: "https://<public-key>@errortrack.altixcode.com/<project-id>",
      integrations: [Sentry.replayIntegration()],
      tracesSampleRate: process.env.NODE_ENV === "development" ? 1.0 : 0.1,
      replaysSessionSampleRate: 0.1,
      replaysOnErrorSampleRate: 1.0,
      // Optional: send events through your own server to get past ad blockers (see /docs/tunnel)
      // tunnel: "/monitoring",
    });
    
    export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;

    sentry.server.config.ts (and sentry.edge.config.ts)

    import * as Sentry from "@sentry/nextjs";
    
    Sentry.init({
      dsn: "https://<public-key>@errortrack.altixcode.com/<project-id>",
      tracesSampleRate: process.env.NODE_ENV === "development" ? 1.0 : 0.1,
    });

    instrumentation.ts

    import * as Sentry from "@sentry/nextjs";
    
    export async function register() {
      if (process.env.NEXT_RUNTIME === "nodejs") {
        await import("./sentry.server.config");
      }
      if (process.env.NEXT_RUNTIME === "edge") {
        await import("./sentry.edge.config");
      }
    }
    
    // Captures errors from Server Components, middleware and proxies.
    export const onRequestError = Sentry.captureRequestError;

    next.config.ts

    import type { NextConfig } from "next";
    import { withSentryConfig } from "@sentry/nextjs/config";
    
    const nextConfig: NextConfig = {};
    
    export default withSentryConfig(nextConfig, {
      // Only print upload logs in CI.
      silent: !process.env.CI,
    });
  3. Source Maps hochladen

    Damit Stacktraces Ihren Originalcode statt des minifizierten Codes anzeigen. Erstellen Sie unter Einstellungen → Auth-Tokens ein Auth-Token mit dem Scope project:releases und bewahren Sie es in Ihren CI-Secrets auf – niemals im Repository.

    bash

    # Set in the build environment; the SDK's build plugin uploads source maps with them.
    export SENTRY_URL=https://errortrack.altixcode.com
    export SENTRY_ORG=<org-slug>
    export SENTRY_PROJECT=<project-slug>
    export SENTRY_AUTH_TOKEN=<an auth token from Settings → Auth Tokens>

    Source-Maps-Anleitung

  4. Überprüfen

    Lösen Sie absichtlich einen Fehler aus. Er erscheint innerhalb weniger Sekunden in Ihrem Issue-Stream.

    tsx

    "use client";
    
    export default function Page() {
      return (
        <button onClick={() => { throw new Error("My first ErrorTrack error"); }}>
          Break the world
        </button>
      );
    }

Erstellen Sie ein Projekt, und auf der Einrichtungsseite wird automatisch Ihre echte DSN eingetragen.