Skip to content

[6.x] Hybrid Glide image caching#14570

Open
duncanmcclean wants to merge 6 commits into6.xfrom
glide-half-measure-caching
Open

[6.x] Hybrid Glide image caching#14570
duncanmcclean wants to merge 6 commits into6.xfrom
glide-half-measure-caching

Conversation

@duncanmcclean
Copy link
Copy Markdown
Member

@duncanmcclean duncanmcclean commented Apr 29, 2026

This pull request implements a new "hybrid" caching strategy for Glide image manipulation.

The Problem

Statamic currently offers two Glide caching modes:

  • Dynamic (cache: false) — Fast template rendering, but every image request hits PHP, even after the image has already been generated.
  • Static (cache: true) — Images are served directly by the web server, but they're all eagerly generated during template rendering. A page with 10 images × 11 variants = 110 images blocking the page from even loading.

Hybrid Caching

Setting cache to 'hybrid' enables a new mode that combines the best of both:

// config/statamic/assets.php
'image_manipulation' => [
    'cache' => 'hybrid',
    'cache_path' => public_path('img'),
],

The Glide tag outputs a URL pointing to where the cached image will live, but doesn't generate the image during template rendering. When the browser requests the image:

  1. If the file already exists on disk, the web server (Nginx/Apache) serves it directly — no PHP needed.
  2. If the file doesn't exist yet, the request falls through to PHP, which generates the image, saves it to the public cache_path, and serves it.

After the first request, the web server serves the static file directly on all subsequent requests. The default Nginx/Apache/IIS configurations handle this automatically — no additional rewrite rules are needed.

How It Works

  • A new GlideCachePathResolver predicts the deterministic cache path for an image without generating it.
  • A new HybridUrlBuilder builds clean URLs using the cache path, and stores a mapping in the Glide cache store so the controller knows how to generate the image later.
  • The GlideController handles the PHP fallback: it checks if the file already exists (logging a debug message if server rewrite rules aren't configured), looks up the mapping, and generates the image on demand.
  • Cache invalidation and php please glide:clear work automatically — the cache disk is the public path, so existing cleanup logic handles it.

Closes statamic/ideas#1420

Docs PR: statamic/docs#1899

@duncanmcclean duncanmcclean changed the title [6.x] Half-measure Glide image caching [6.x] Hybrid Glide image caching Apr 29, 2026
@daun
Copy link
Copy Markdown
Contributor

daun commented Apr 29, 2026

🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hybrid approach to Asset caching for massive performance benefits

2 participants