Next.js Environment Variables — How NEXT_PUBLIC_ and .env Work on Skyversal

Why does NEXT_PUBLIC_ get exposed to the browser? Why is a re-deploy required? Why must DATABASE_URL stay secret? All the answers about Next.js env variables in one guide.

Read Time: 4 Min
Level: Intermediate

Skyversal fully supports Next.js' environment variable model. For security reasons, Next.js separates the code running on the server from the code sent to the browser. In this guide, you will learn what the NEXT_PUBLIC_ prefix does, which variables are sent to the client, and how to securely manage environment variables on Skyversal.

💡
VPS vs. Skyversal Advantage

Manually transferring environment variables or keeping .env files secure requires additional configuration and security responsibility on a traditional VPS. Skyversal securely stores your environment variables and safely passes them to your application during the build and runtime phases. Nixpacks uses these variables at necessary points during the build process.

1. Server-side vs Client-side Variables

🔒

Server-side Only

These variables can only be read by server-side code and are never sent to the browser.

💡 Server-side Examples
  • Server Components
  • Server Actions
  • Route Handlers
  • API Routes
  • Middleware
DATABASE_URL="postgres://..."
STRIPE_SECRET_KEY="sk_live_xxxxx"
JWT_SECRET="super-secret"
🌍

Exposed to the Browser

You must prefix variables with NEXT_PUBLIC_ if you want them to be accessible by the browser. Variables starting with NEXT_PUBLIC_ are not secret. They are embedded into the application's client bundle so that the browser can read them.

NEXT_PUBLIC_API_URL="https://api.example.com"
NEXT_PUBLIC_SUPABASE_URL="https://xyz.supabase.co"
NEXT_PUBLIC_ANALYTICS_ID="G-XXXXXXXX"

2. Where are Environment Variables Used on Skyversal?

🏗️ Build Time

  • SSG (Static Site Generation)
  • Static Export
  • next/image
  • Bundle creation

🚀 Runtime

  • Server Components
  • API Routes
  • Server Actions
  • Cron Jobs
  • Background Workers

3. Adding Environment Variables on Skyversal

To add a new environment variable to your project:

  1. Open your project dashboard.
  2. Go to the Settings → Environment Variables section.
  3. Enter the Key and Value.
  4. Save changes.
  5. Trigger a Re-deploy if necessary.

4. Why is a Re-deploy Required?

While server-side variables might not require a new build depending on the framework, variables starting with NEXT_PUBLIC_ are embedded into JavaScript packages (client bundle) by Next.js during the build process, therefore a new deploy is recommended.

1
Environment Variable Changed A new value was added or deleted.
2
New Build Started (Re-deploy) Next.js compiles the code and embeds values into the JS bundle.
3
Deployed with Zero Downtime The old version spins down, the new version goes live.
4
Browser Sees New Value When users visit your site, they use the new API URL.

5. Security Tips

⚠️ Warning

  • ❌ DATABASE_URL
  • ❌ JWT_SECRET
  • ❌ STRIPE_SECRET_KEY
  • ❌ OPENAI_API_KEY
  • ❌ RESEND_API_KEY

Never use NEXT_PUBLIC_ for sensitive information like these. Doing so includes these variables in the client bundle, meaning they can be viewed through Browser Developer Tools or client-side JavaScript code.

Frequently Asked Questions

What happens if I don't use NEXT_PUBLIC_?

The client-side (browser) code will not be able to access the variable, and its value will be undefined.

I updated a value in production, but the app uses the old one.

Variables starting with NEXT_PUBLIC_ are embedded into the client bundle during the build. You must trigger a new Re-deploy to apply the changes.

Should I upload my .env file to GitHub?

No. Your local .env file should remain in .gitignore, while production variables must be securely managed from your Skyversal dashboard.

🎉

Everything is Ready!

You can now securely manage both server-side secrets and client-side environment variables in your Next.js application.

Next Steps

In the next guide, we will explore how Next.js Image Optimization, Middleware, ISR, and other advanced features work on Skyversal.

Last updated on July 27, 2026
Edit this page on GitHub