Skip to main content
Environment injection allows you to pass server-side environment variables to your frontend at runtime, perfect for configuration that changes between environments.

Basic Usage

Backend Configuration

Frontend Access

Variables are available as window.__ENV__:

How It Works

The middleware injects a script tag into your HTML:
The script is injected before </head>, making variables available before your app loads.

Use Cases

API URLs

Analytics

Feature Flags

Multi-Environment Setup

Security Considerations

Never Inject Secrets

Why: All injected variables are visible in the HTML source and browser console.

Only Inject Public Configuration

Prefixing Convention

Follow the convention of prefixing public variables:

Build-Time vs Runtime

Build-Time (Vite/Webpack)

Pros:
  • Type-safe (TypeScript)
  • Tree-shaking works
  • Optimizations apply
Cons:
  • Must rebuild for each environment
  • Can’t change without rebuild

Runtime (Mizu Injection)

Pros:
  • Single build for all environments
  • Change without rebuild
  • Dynamic configuration
Cons:
  • Not type-safe by default
  • Available after page load
  • Requires fallbacks

Hybrid Approach

Best of both worlds:

TypeScript Support

Create type definitions:
Use safely:

Testing

Mock in Tests

Environment-Specific Tests

Alternative: Meta Tags

You can also inject config via meta tags:
Read in frontend:
When to use:
  • SEO-relevant config
  • Prefer meta tags over script tags
  • Framework requires meta tags

Next Steps

Configuration

All configuration options

Security

Security best practices

Deployment

Build and deployment guide