Search by

lameco / craft-blitz

Laméco Development B.V.

Laméco defaults for Blitz: forced cache settings and per-site query string exclusions

Package info

github.com/Lameco-Development/craft-blitz

Type:craft-plugin

pkg:composer/lameco/craft-blitz

Statistics

Installs: 27 157

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1


README

Laméco defaults for Blitz: it forces one set of Blitz settings on every site and lets each site name the query string parameters that must bypass the cache.

Requirements

  • PHP 8.2 or higher
  • Craft CMS 5
  • Blitz 5, installed and enabled

Installation

composer require lameco/craft-blitz:^6.0
php craft plugin/install _craft-blitz

The constraint matters: Packagist also carries the tags 4.0.0, 5.0.0 and 5.0.1 from an older implementation (2024).

What it forces

The plugin overrides these Blitz settings at runtime, whatever the Blitz CP settings or config/blitz.php say:

Blitz setting Value
Caching enabled BLITZ_ENABLED (default off)
Debug BLITZ_DEBUG (default off)
Included URI patterns every URI, every site
Generator LocalGenerator when BLITZ_GENERATOR=LOCAL, otherwise HttpGenerator, with concurrency 1
Storage compressed cached values
Refresh mode expire the cache
Query string caching cache URLs with query strings as the same page
Included / excluded query string params none

BLITZ_GENERATOR=LOCAL is for local development only, where Blitz's HTTP generator can't reach Valet's .test domains. Servers use HTTP: the local generator can't rebuild craft.blitz.includeCached() fragments, so an expired header or footer stays up.

Only pages that resolve to an entry are cached, plus the fragments of craft.blitz.includeCached().

Translations

With Enupal Translate installed, a changed site translation expires only the cached pages and craft.blitz.includeCached() fragments that show it, as Blitz recommends for content it can't track itself (craft-blitz#649):

  • While Blitz caches a page or fragment, the plugin tags it with every site translation (|t, Craft::t('site', …)) it renders.
  • Enupal writes each save, import or provider translation to the translation file and queues its "Syncing translations with DB" job. Just before that job runs, the plugin compares Enupal's database copy with the files, and afterwards expires the pages tagged with a changed message. A change in translations/nl/ also covers the regions that fall back to it, such as nl-NL.
  • If Enupal's database copy is behind the files, for example when translations were changed in git, the next sync treats those messages as changed too.

This needs a queue runner, and only happens while Blitz caching is enabled (BLITZ_ENABLED). As with any expired page, the first visit still gets the old text and queues the regeneration; the visits after that get the new text. A site that configures its own message source for the site category isn't tagged; there, a sync expires the whole cache. Edits to plugin translations through Enupal are not tracked.

Pages cached before 6.1.0 carry no translation tags, so clear the Blitz cache once after updating.

Navigation menus

With Navigation 4.1 or later installed, a menu change expires the cached pages and craft.blitz.includeCached() fragments that show that menu, at any level. When Navigation 4.1 serves a menu from its own cache, it doesn't query the menu's nodes, so Blitz can't link that menu to the pages that render it and keeps showing the old one.

  • While Blitz caches a page or fragment, the plugin tags it with every menu it reads from Navigation's cache, next to its translation tags.
  • When Navigation clears a menu from its cache, the plugin expires the pages tagged with that menu. For a menu whose nodes don't propagate across sites, that's only the site it changed on. For any other menu (Navigation's default propagates to all sites), it's every site, since deleting or moving a node or publishing the menu builder changes the menu on all of them while Navigation only names one. A change to all menus expires every page that shows one.
  • Pages that work out the active node (craft.navigation.getActiveNode()) are tagged as well when the menu query behind it is one Navigation caches (a menu given by handle or id, without options such as withMenu that bypass its cache): a menu change then also expires those pages.
  • Dynamic nodes are left to Blitz: Navigation queries their entries, categories or assets directly, which Blitz tracks itself.

The expiry only happens while Blitz caching is enabled (BLITZ_ENABLED) and Blitz's refreshCacheEnabled setting is on (its default; it lives in config/blitz.php). As with translations, the first visit to an expired page still gets the old menu. No template or module code is needed.

When the plugin can't tag menus, it says so in Craft's log under lameco\blitz\Plugin::_tagNavigationMenus and leaves Navigation alone:

  • If Navigation is missing, disabled or older than 4.1, there's nothing to tag and it logs at info level: "Navigation is not installed or disabled, so menus are not tagged in the Blitz cache." or "Navigation menus are not tagged in the Blitz cache: Navigation 4.1 or later is not installed." Craft's default log target drops info messages outside dev mode.
  • If Navigation is 4.1 or later but a Navigation update removed or changed the cache service the plugin hooks into, or the service has already been replaced or configured elsewhere (by the project or another plugin), it logs at warning level: "Navigation menus are not tagged in the Blitz cache:" followed by the reason.

While the integration is off, Blitz's own tracking and clearing the cache still work as before. Only menu changes Blitz can't see itself, those served from Navigation's cache, can then stay stale until the plugin is updated.

Pages cached before this release carry no menu tags, so clear the Blitz cache once after updating.

Query string exclusions

Because every URL with a query string is cached as the same page, a parameter that changes the output has to be listed, or its render ends up in the cache under the plain URL for every visitor. A listed parameter makes the request bypass the cache whenever it is present, whatever its value (?q=0, ?q= and ?q included). Blitz neither serves such a request from the cache nor stores it.

  • Global query string params: a comma-separated list that applies to every page, for example q,page.
  • Per section and entry type: rows of a section/entry type and its comma-separated parameters.

Write array parameters with or without brackets: filters[] matches ?filters[]=1 and ?filters[0]=1. Both settings can also be set in config/_craft-blitz.php, which then overrides the CP.

Which parameters to list:

  • Everything a template reads from the URL, including a Sprig component's first render: Sprig turns query string parameters into component variables, so a search or filter component that reads query, category or page needs them listed.

  • Query string pagination (pageTrigger('?page') in config/general.php): list page, since every page of a listing shares one URI: ?page=2 would otherwise get the cached first page, or store page two under the plain URL.

  • Path pagination (/news/p2, Craft's default) gives each page its own URI and needs nothing listed. Craft does clamp a page number past the end to the last page and answers 200, so /news/p99999 gets cached as its own page, and so does every other number a crawler tries. Return a 404 instead, right after the {% paginate … as pageInfo, entries %} tag (use the template's own name for pageInfo):

    {% if craft.app.request.pageNum > pageInfo.totalPages %}{% exit 404 %}{% endif %}

    Blitz doesn't cache a 404, and drops a cached page whose URL now returns one when it regenerates it.

Clearing the cache

Where this README says to clear the Blitz cache, run both commands on every environment, in this order:

php craft blitz/cache/flush && php craft blitz/cache/clear
  • clear deletes the cached pages but not their records. A deploy that runs php craft clear-caches/all, as Deployer's stock Craft recipe does, does the same to Blitz. The records stay, and Blitz's refresh-expired and generate jobs keep requesting the URIs in them, including pages the current rules no longer cache.
  • flush deletes the records but not the cached pages. A page left without a record is still served, and an edit never expires it.
  • Flush first: a page cached between the two commands then ends up as a record without a file, which the next visit to that page repairs.

Blitz's own blitz/cache/refresh doesn't do this: under the forced refresh mode it only marks pages expired.

Upgrading

  • From an earlier 6.x release: on sites with Navigation 4.1 or later, clear the Blitz cache once after updating, so that pages and fragments cached before this release get the menu tags. A project that tags its menus itself (an EVENT_INVALIDATE handler plus tags in the templates) can drop that code.

  • From 1.x: require ^6.0, run php craft up, then clear the Blitz cache, since pages cached under the old rules stay in the cache otherwise. Then follow From an earlier 6.x release above.

    The forced generator and storage settings now take effect; before 6.0.0 Blitz had already built those components when the plugin set them. Cached pages also get a gzipped copy, which Craft serves to browsers that accept it, and BLITZ_GENERATOR now picks the generator. Check that no server's .env says BLITZ_GENERATOR=LOCAL before deploying.

  • From 5.0.x: all of the above. The migration converts the 5.0.x rows, which stored a section ID, into one row per entry type of that section, and logs a warning for rows whose section no longer exists. Run the migration in the environment where project config is edited and commit the YAML; other environments pick it up when project config is applied.

    A listed parameter now means something else. In 5.0.x it cached each value as its own page, so every ?query=… and every filter combination got its own cache entry. Now a request carrying it skips the cache: search results and filtered views are always rendered by Craft, and only the plain URL is cached.

    The migration can't be reverted. Going back to 5.0.x means restoring the database backup php craft up takes before migrating (backupOnUpdate, on by default; check that it exists before upgrading) and reverting the commit with the converted project config, together with the previous release. Craft won't apply project config whose schema version doesn't match the installed plugin.