RealComposer
Client PlatformsOfficial Next.js docs

Next.js Safe & Unsafe Practices

Direct Answer

In Next.js, any variable prefixed NEXT_PUBLIC_ is inlined into the client bundle. Secrets are safe only in Server Components, Route Handlers, and Server Actions, and every Server Action is a public endpoint that must authenticate and authorize its caller.

Credentials & Where They Belong

Client safe
NEXT_PUBLIC_ variables

Inlined at build time into browser JavaScript.

Server only
Unprefixed environment variables

Available only in server code paths.

Safe Practices

  • Import the server-only package in secret modules

    The build fails if a client component accidentally imports code that reads secrets.

  • Authorize inside every Server Action

    Check the session and ownership in the action, not just in the UI that calls it.

  • Pass minimal props to client components

    Props are serialized into the page, so strip private fields first.

Unsafe Practices

  • Prefixing secrets with NEXT_PUBLIC_

    The value ships to every visitor.

  • Passing full database rows to client components

    Hidden fields still appear in the serialized payload.

The RealComposer Pattern

Use RealComposer pipelines from client components or ISR pages for third-party aggregation, so preview tokens and upstream keys stay in the vault rather than in Next.js env files.

START BUILDING FREE

Frequently Asked Questions

Q:Are Next.js Server Actions secure by default?

No. They are reachable by direct POST requests, so each one must validate input and check authorization.

Related Concepts