Farm.js

Sentry

@farm.js/sentry connects Farm.js's request, render, build, and shutdown lifecycle to Sentry. It reports server errors with Farm.js event and route context, names request spans by route pattern, and safely flushes pending events without letting monitoring failures stop the application.

Install

Install the Farm.js plugin and the Sentry Node SDK:

pnpm add @farm.js/sentry @sentry/node

@farm.js/sentry provides the Farm.js lifecycle integration. @sentry/node collects and sends the events. The SDK is an optional peer dependency so applications can control its version or supply a compatible SDK explicitly.

Configure

Add the Sentry project DSN to the application environment:

SENTRY_DSN=https://examplePublicKey@o0.ingest.sentry.io/0

Register the plugin in farm.config.ts:

import { defineConfig } from "@farm.js/core";
import { sentryPlugin } from "@farm.js/sentry";

export default defineConfig({
  plugins: [
    sentryPlugin({
      dsn: process.env.SENTRY_DSN,
      tracesSampleRate: 0.1,
    }),
  ],
});

The DSN is the only credential needed to report runtime events. A Sentry auth token is only needed for a separate source-map upload workflow.

Initialize before application modules

The Node SDK instruments modules as they load. Initialize it from src/instrumentation.ts so HTTP and database instrumentation attaches before the rest of the application starts:

import { registerSentry } from "@farm.js/sentry";

export const register = registerSentry({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: 0.1,
});

The plugin still captures Farm.js errors without this file, but automatic HTTP and database instrumentation will not attach.

What it captures

Farm.js lifecycleSentry behavior
Runtime startResolves and initializes the SDK once.
Request contextNames the request span by route pattern, such as GET /users/[id].
ResponseRecords the response status and finishes plugin-owned spans.
Runtime and render errorsCaptures the exception with the Farm.js event, request kind, and route context.
Browser errorsCaptures errors Farm.js catches on the client with the Farm.js phase and page context, when browserDsn is set.
Runtime shutdownFlushes pending events without allowing a flush failure to stop shutdown.
Production buildGenerates source maps when sourceMaps is enabled.

When the Sentry SDK already created an HTTP span, Farm.js renames that span instead of creating a competing request span. Automatic HTTP and database spans therefore remain correctly nested.

Serverless deployments

Some serverless hosts terminate an invocation without running the normal shutdown lifecycle. Enable response flushing on those deployments:

sentryPlugin({
  dsn: process.env.SENTRY_DSN,
  flushOnResponse: true,
});

Farm.js hands the flush promise to waitUntil, allowing supported hosts to keep the invocation alive long enough to deliver pending events. Rejected flushes are logged and do not replace the application response or original error.

Pass an existing SDK

Applications can pass the Sentry SDK module instead of letting the plugin import it:

import * as Sentry from "@sentry/node";
import { sentryPlugin } from "@farm.js/sentry";

sentryPlugin({
  sdk: Sentry,
  dsn: process.env.SENTRY_DSN,
});

The sdk option accepts the module's structural API, not a separately constructed client. If the application already initialized Sentry, the plugin detects the active client and does not initialize it again.

Options

OptionDefaultDescription
dsnSentry project DSN.
browserDsnBrowser project DSN, provided explicitly. Never reuse the server DSN.
environmentInstrumentation modeEnvironment name.
releaseRelease identifier for the deployment.
tracesSampleRateFraction of requests traced. Errors are always reported.
sendDefaultPiifalseInclude request headers and user data.
enabledtrueSet to false to keep the hooks registered without reporting.
sdk@sentry/nodeCompatible SDK module to use instead of importing @sentry/node.
flushOnResponsefalseFlush after successful and failed requests for serverless deployments.
flushTimeoutMs2000Maximum time allowed for each flush.
sourceMapsfalseGenerate production source maps. This does not upload them.
sentryOptions{}Extra Sentry.init options such as beforeSend, ignoreErrors, or debug.

sendDefaultPii remains disabled by default because error events can contain request headers and user information.

Source maps

sourceMaps: true generates production source maps but does not upload them to Sentry. Upload the generated maps separately with Sentry's tooling. Enabling source maps moves minification to Nitro's terser, so install its build dependency too:

pnpm add -D @rollup/plugin-terser

Runtime support

The default SDK integration currently supports Node presets. Cloudflare Workers require @sentry/cloudflare, so early registerSentry initialization is a no-op for edge and bun runtimes.

If a DSN is configured but no SDK resolves, Farm.js logs a clear error and continues without reporting. Monitoring failures must not prevent the application from starting, responding, or shutting down.

Browser errors

Errors Farm.js catches on the client reach the client error hook, which has no consumer until a browser DSN is configured:

sentryPlugin({
  dsn: process.env.SENTRY_DSN,
  browserDsn: process.env.PUBLIC_SENTRY_DSN,
});

browserDsn must be a separate browser DSN, provided explicitly — never reuse the server DSN. A browser DSN is public by design (it ships in the browser bundle); a server DSN is a secret. Only the browser DSN and scalar options cross into the browser bundle. Without browserDsn no browser code is registered and @sentry/browser stays out of the browser bundle.

Browser errors are tagged with the Farm.js error phase (farm.phase) and the page they happened on, with the route pattern when the error happened during a navigation. Window errors and unhandled rejections are left to Sentry's own handlers, so they are reported once, with Sentry's mechanism — Farm.js reports only the errors it catches. Install the browser SDK for this:

pnpm add @sentry/browser

The browser SDK is loaded in the background: loading starts in the client setup hook and hydration does not wait for it — the error hook awaits the load instead. If a browser DSN is configured but @sentry/browser is not installed, Farm.js logs a clear error telling you to install it, and continues without reporting.

To pass an SDK the application already initialized instead, register the reporter from the app client entry:

// src/client.ts
import { defineClient } from "@farm.js/core/client/lifecycle";
import { sentryClient } from "@farm.js/sentry/client";
import * as Sentry from "@sentry/browser";

export default defineClient(sentryClient({ sdk: Sentry }));

An already-initialized SDK reports without browserDsn; pass browserDsn as well only if the SDK still needs initializing.

If a browser DSN is configured but no SDK resolves, Farm.js logs a clear error and continues without reporting. Monitoring failures must not prevent the application from starting, responding, or shutting down.