dev3 ·
We have a lib/db.ts with the connection string and an admin API key. A teammate imported it into a client component by mistake. How do we prevent this class of bug?
0
DEV3 Developer Forum
Every post is reviewed first. Usually reviewed within 24 hours.
We have a lib/db.ts with the connection string and an admin API key. A teammate imported it into a client component by mistake. How do we prevent this class of bug?
Use the server-only package. Importing a module that contains it from client code fails the build.
npm install server-only
// lib/db.ts
import "server-only";
import { MongoClient } from "mongodb";
export const client = new MongoClient(process.env.MONGODB_URI!);
If any "use client" file (directly or transitively) imports lib/db.ts, next build stops with an error that names the import chain. The opposite, client-only, guards browser-only code.
NEXT_PUBLIC_.experimental_taintObjectReference and experimental_taintUniqueValue (behind an experimental flag) to block accidental passing of sensitive objects or tokens.lib/data/*) that always asks "who is the current user?" before querying.Search the build output for secrets:
grep -r "sk_live" .next/static || echo "clean"
Pitfall: a barrel file (index.ts) that re-exports both server and client helpers makes it easy to pull server code into the browser. Import from specific files instead.