Set up Faro with Next.js ΒΆ

Set up Grafana Faro in a Next.js application using the App Router (the default since Next.js 13).

Faro is a browser-only SDK and cannot run in React Server Components. You need a 'use client' component that initializes Faro on the client side.

Prerequisites ΒΆ

  • A Next.js application using the App Router, deployed on Nais
  • Node.js and npm

Install ΒΆ

sh

For browser tracing (optional β€” connects frontend spans with backend traces):

sh

Create the Faro component ΒΆ

Create a client component that initializes Faro. Use useEffect to avoid running side effects during server-side rendering or React Strict Mode double-invocations.

tsx

Add it to your root layout ΒΆ

Include the component in your root app/layout.tsx so Faro initializes on every page. Pass the collector URL from the server environment:

tsx

The NAIS_FRONTEND_TELEMETRY_COLLECTOR_URL environment variable is set automatically when you enable spec.frontend.generatedConfig in your nais.yaml. If the env var isn't set, the Faro component falls back to https://telemetry.external.prod.test-nais.cloud.nais.io/collect.

`NEXT_PUBLIC_` env vars are build-time only

Don't use NEXT_PUBLIC_ for the collector URL. Next.js inlines NEXT_PUBLIC_* variables at next build time, so they won't change per cluster at deploy time. Pass runtime values through Server Components as props instead.

Configure your nais.yaml ΒΆ

Enable auto-configuration to get the collector URL set per cluster:

yaml

This sets NAIS_FRONTEND_TELEMETRY_COLLECTOR_URL in your pod, which the root layout passes to the Faro component. The mountPath can be any writable path β€” for SSR apps the file itself isn't served to browsers, only the env var matters.

See the auto-configuration reference for all generated values.

Error boundaries ΒΆ

Next.js App Router has built-in error boundaries via error.tsx and global-error.tsx. Connect these to Faro to capture React rendering errors:

tsx

Create a similar app/global-error.tsx to catch errors in the root layout itself.

React integration package ΒΆ

The @grafana/faro-react package provides:

  • <FaroErrorBoundary> β€” wraps components and reports rendering errors to Faro
  • <FaroRoutes> β€” tracks route changes as navigation events (for React Router)
  • React Router v7 helpers β€” createReactRouterV7Options and createReactRouterV7DataOptions (since v2.2.3)
  • React 19 support (since v2.1.0)

Install it:

sh

Use the error boundary to wrap parts of your component tree:

tsx

Mount/unmount tracking

The profiler integration tracks component mounts and unmounts, which generates a lot of data. Only enable it for specific components you want to monitor.

Next.js webpack externals ΒΆ

If you use the nais.js auto-configuration, add this to your next.config.js:

js

Real-world example ΒΆ

For React Router examples with Faro, see the main setup guide.

Further reading ΒΆ