Skip to main content
This guide covers everything you need to deploy your Mizu views in production: embedding templates, caching, performance optimization, and best practices.

Embedding Templates

In production, embed templates into your binary. This eliminates file system dependencies and ensures templates can’t be accidentally modified.

Using embed.FS

Go’s embed package lets you include files in your binary:

Directory Structure for Embedding

The //go:embed directive embeds a directory relative to the source file:
After building, the binary contains all templates.

Conditional Embedding

Use environment variables to switch between development and production:

Preloading Templates

In production, preload all templates at startup to:
  1. Fail fast - Catch template errors before serving requests
  2. Warm cache - Avoid first-request latency
  3. Validate - Ensure all templates parse correctly

Preload Errors

Load() catches errors like:
  • Missing template files
  • Template syntax errors
  • Invalid template references

Performance Optimization

Template Caching

In production mode (Development: false), templates are cached:
  • Parsed once - Templates are parsed at first use or Load()
  • Reused - Same parsed template serves all requests
  • No disk I/O - Templates read from memory

Pre-compute in Handlers

Move complex logic to Go:

Minimize Template Complexity

Complex templates are slower. Keep them focused:

Error Handling

Custom Error Pages

Create error page templates:

Error Handler

Set up a global error handler:

Template Error Handling

Handle template rendering errors gracefully:

Security

Content Security Policy

Set appropriate headers:

HTML Escaping

Go templates escape HTML by default. Only bypass escaping for trusted content:

CSRF Tokens

Include CSRF tokens in forms:

Monitoring

Template Metrics

Track template rendering performance:

Health Checks

Verify templates work in health checks:

Deployment Checklist

Before Deploying

  • Set Development: false
  • Use embed.FS for templates
  • Call Load() at startup
  • Create error page templates
  • Set up error handler
  • Test all pages render correctly

Environment Variables

Docker Example

Complete Production Setup

What’s Next?

Now that your views are ready for production, explore: