Skip to content

Next.js Plugin Reference — @rep-protocol/next

Terminal window
npm install -D @rep-protocol/next
app/layout.tsx
import type { ReactNode } from 'react';
import { RepScript } from '@rep-protocol/next';
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<head><RepScript /></head>
<body>{children}</body>
</html>
);
}
interface RepScriptProps {
env?: string;
strict?: boolean;
}
function RepScript(props?: RepScriptProps): React.ReactElement | null;

A React Server Component. Renders <script id="__rep__"> during next dev; renders null in production.

PropTypeDefaultDescription
envstring'.env.local'Path to the env file, resolved relative to process.cwd(). Same REP_PUBLIC_* / REP_SENSITIVE_* / REP_SERVER_* classification as the gateway.
strictbooleanfalseTurn guardrail warnings into thrown errors instead of console.warn.
<RepScript env=".env.development" strict />
app/api/rep/session-key/route.ts
export { GET } from '@rep-protocol/next/session-key';

Only needed if you read REP_SENSITIVE_* variables with rep.getSecure() / useRepSecure() during development. Returns the ephemeral AES key as JSON, matching the gateway’s /rep/session-key response shape.

  • Renders nothing in production. NODE_ENV === 'production' (which next build sets automatically) makes RepScript return null. This also prevents build-time env vars from being baked into the static output — the whole point of REP is that config is resolved after the build, not during it.
  • Server Component only. RepScript reads the filesystem (env) and generates keys server-side; it cannot be used in a Client Component. Consume the injected payload from Client Components via rep.get() / useRep() as usual — the SDK reads the DOM, not React context.
  • Script content is Go-escaped (<, >, & → <, >, &) before being written via dangerouslySetInnerHTML, preventing </script> breakout from untrusted env values. The tag still carries type="application/json", so the browser never executes it.
  • Ephemeral keys are process-scoped, held in a module-level singleton (getOrCreateKeys()) so the same key survives across route handler invocations within one next dev process.
  • Guardrails scan every REP_PUBLIC_* value whole, including csv values the gateway scans by element (details).
  • Byte-identical payload to the Go gateway and the Vite plugin (sorted keys, same HMAC + SRI format) — rep.verify() and rep.meta() behave the same regardless of which one produced the payload.

RepScript is a Server Component and requires the App Router (app/). The Pages Router (pages/) doesn’t support React Server Components — use the CLI dev server or mock payload approach instead.