Skip to content
Twilight React Playground
ثيم رائدaren

withThemeSentry

functionAdvancedserver

Wraps the server's request handler so errors during server rendering are reported to Sentry, tagged with the theme name and the store host.

import { withThemeSentry } from '@salla.sa/twilight-theme-engine/sentry/server';

In plain words

Your theme also runs on a server: every page is first built there, before the browser receives it. An error during that step never reaches the browser. withThemeSentry(handler, 'my-theme') wraps the server's request handler so those errors are reported to Sentry, tagged with your theme's name.

Like its browser twin, it sends nothing until a DSN is configured.

Signature

function withThemeSentry<T extends { fetch: (...args: any[]) => any }>(
  handler: T,
  theme: string
): T
// DSN: env.SENTRY_DSN (server variable) || import.meta.env.VITE_SENTRY_DSN (build time)

Example

app/server.ts
import handler from '@tanstack/react-start/server-entry';
import { withThemeSentry } from '@salla.sa/twilight-theme-engine/sentry/server';

// TanStack Start's own request handler, wrapped. Same name as initThemeSentry.
export default withThemeSentry(handler, 'my-theme');

How it behaves

  • app/server.ts is TanStack Start's server entry: when the file exists Start uses it, and its default export must be an object with fetch. Wrap the handler Start exports from @tanstack/react-start/server-entry.

  • It calls withSentry from the server Sentry SDK with the DSN, serverName: 'twilight-theme-<theme>', the tags source: 'twilight-engine' and theme, and a beforeSend that adds a store tag (the host of the failing request's URL). beforeSend never drops an event.

  • The DSN comes from the server's SENTRY_DSN variable, then from VITE_SENTRY_DSN baked in at build time. With neither, the handler is still wrapped and Sentry sends nothing.

  • A theme published through Salla runs without server variables, so there only a VITE_SENTRY_DSN present when the theme is built turns reporting on.

Gotchas

  • Never import @salla.sa/twilight-theme-engine/sentry/server from code that reaches the browser: it imports the server-only Sentry SDK at the top level. Browser code uses initThemeSentry from /sentry/client.

Related

Source and docs