0

server-only & keeping secrets server-side

Securityused in ~65% of apps

One careless import can ship your Stripe secret key, your database URL, and your admin tokens straight into the browser bundle — viewable by anyone with devtools. The boundary you learned for 'use client' is also a security boundary, and this concept is the seatbelt that fails the build instead of failing in production.

Core idea. Server Components keep code and secrets on the server by default — but nothing physically stops a teammate from importing a secret-bearing module into a Client Component, where it gets bundled and shipped. The `server-only` package turns that mistake into a build error, and only env vars prefixed `NEXT_PUBLIC_` are ever sent to the browser.
Playground, this isn't a screenshot
Tracing one production secret:STRIPE_SECRET_KEY=sk_live_9f3kZ8aQ2
.envcommitted (mistake)
STRIPE_SECRET_KEY=sk_live_9f3kZ8aQ2

// git add .env && git commit

git log (history)PERMANENT · PUBLIC IF PUSHED
a1b2c3d add config
+ STRIPE_SECRET_KEY=sk_live_9f3kZ8aQ2

// the secret is now in a commit, copyable by anyone with the repo

Leaked forever. The key sits in commit a1b2c3d. Anyone who cloned, forked, or scraped the repo already has it. Deleting the file does not rewrite history. The only real fix: rotate the key in Stripe now (then scrub history as cleanup).
↑ play a little first, then dig in