Keep Supabase Service Role Keys Out of Next.js Client BundlesKeep Supabase Service Role Keys Out of Next.js Client Bundles
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
If a key ships to the browser, anyone can copy it.
In Next.js, every variable that starts with NEXT_PUBLIC_ ends up in the JavaScript your visitors download. That is fine for the Supabase anon key. It is not fine for the service role key, which skips every RLS policy.
A quick check after a build
grep -r "service_role" .next/static
If that finds anything, rotate the key in Supabase and move every call that needs it to the server.
A common shortcut when a query fails under RLS is to switch to the service role key. The error goes away and the hole stays.
Have you ever had to rotate a key in a hurry?
Post image
Jems's avatar
Good habit. I'd put that grep in CI too, so it runs on every build and not only when someone remembers. Same goes for AI keys: an OpenAI key in the browser can burn through credits fast, so I keep those calls in a server route and send only the result to the client.
Yaroslav's avatar
Agreed, that's where it belongs. A failing build is the only reminder nobody ignores. Same check works for any AI provider key.
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started