Pre-Compression vs Dynamic Compression
What you'll learn: How to decide whether to compress your assets during the build process or let your server compress them on-demand for every request.
The Core Difference
Pre-compression (also called static compression) means compressing your files once at build time—before they ever reach your server. You store both the original and compressed versions on disk. When a request arrives, the server simply sends the pre-compressed file without doing any CPU work.
Dynamic compression means your server compresses files on-the-fly for each request. When someone asks for style.css, the server reads it, compresses it in real-time, then sends it. The server does this work for every single request (though smart servers cache the result).
When to Use Each Approach
Pre-compression is optimal when:
- You have static assets that don't change between deploys (JavaScript bundles, CSS files, fonts)
- You want to use maximum compression levels without worrying about response time
- You want to minimize server CPU usage and save hosting costs
- You're using a CDN or static file host
Dynamic compression is optimal when:
- You're serving dynamically generated content (HTML rendered from templates, API responses)
- Files change frequently or are personalized per user
- You don't control the build process
- Storage space is more limited than CPU resources
The Hybrid Approach
Many production setups use both: pre-compress static assets during build, but dynamically compress HTML and API responses. Think of it like meal prep—you can cook rice in bulk ahead of time (pre-compression), but you'll still need to prepare the fresh vegetables right before serving (dynamic compression).
Key Takeaway: Pre-compress static assets at build time for maximum efficiency and compression quality; use dynamic compression for content that changes frequently or is generated per-request.