How to Prevent Re-encoding Unchanged AVIF Images

AVIF offers superior compression efficiency for modern web applications, but its encoding process requires significant CPU resources and time. In static site generators and automated CI/CD pipelines, re-encoding every image on every build causes massive build-time bottlenecks and inflates server costs. This article explains how developers can prevent the redundant processing of unchanged AVIF images by using content hashing, persistent caching, timestamp validation, and offloaded image pipelines.

Implement Content-Based Hashing

The most reliable method to detect file changes is generating a cryptographic hash (such as MD5 or SHA-256) of the source image buffer.

  1. Generate a hash from the raw image file before passing it to encoders like Sharp or Libavif.
  2. Maintain a JSON manifest or metadata file that pairs the original file's hash with its generated AVIF output path.
  3. During a build, check if the source file's current hash matches the recorded hash in the manifest. If it matches and the output AVIF file exists on disk, skip encoding.

This method guarantees that even if a file's metadata or timestamp changes, it will not be re-encoded unless the image content itself is modified.

Persist Build Caches in CI/CD

Local build caching fails in ephemeral CI/CD environments like GitHub Actions, GitLab CI, or Netlify, where every run begins with a fresh virtual environment.

To maintain efficiency:

Use File Modification Timestamps for Local Builds

For simple setups or command-line scripts using tools like Make, rsync, or custom Node.js scripts, comparing modification timestamps (mtime) is an effective, low-overhead approach.

Decouple Image Processing from Site Builds

Embedding heavy AVIF compression directly into the main website build step causes unnecessary slowdowns. Decoupling the workflows prevents routine code changes from triggering image re-encoding.

Leverage Framework-Native Caching

Modern static site generators and frameworks provide built-in caching for asset transformations:

Ensure your configuration preserves the respective framework cache directories between successive deployments.