|
/ Documentation /General/ Caching in Spectra Blocks

Caching in Spectra Blocks

Introduction

Spectra Blocks generates the CSS your blocks need and caches it so returning visitors get fast page loads. Most of the time this happens quietly in the background and you never have to think about it. This document is for developers and site builders who want to understand how that caching works, hook into it, or turn it off while building a site.

How Spectra Generates and Caches CSS

Spectra Blocks builds CSS per block, so each block outputs only the styles it actually uses. A few things are worth knowing about how that output is handled:

  • Inline output: The generated CSS is printed directly into the page rather than written to a .css file on disk. There is no asset folder to manage or clean up.
  • Asset version key: The cached CSS is stored against an asset version key. As long as nothing relevant changes, the same cached CSS is reused. When a block’s styles or content change, the key updates and the CSS is regenerated.
  • No disk cache: Because the cache lives in memory and transients rather than on disk, “clearing the cache” means flushing the WordPress object or transient cache, the same flush a host caching plugin would trigger. There’s no directory to delete.

Hooks for Caching and CSS Handling

Spectra Blocks exposes two points you can hook into.

Filter: spectra_enable_css_cache

This filter gates whether generated CSS is cached. It defaults to true.

php

$enabled = apply_filters( 'spectra_enable_css_cache', true );

Action: spectra_css_generator_register_handlers

This action fires with the CSS generator instance, so third-party plugins can register their own CSS emit handlers on it.

php

do_action( 'spectra_css_generator_register_handlers', $generator );

Example: Integrating a Cache or Optimization Plugin

A caching or optimization plugin can hook the register handlers action to participate in how Spectra’s CSS is emitted, for example to capture the generated CSS into its own cache layer.

php

add_action( 'spectra_css_generator_register_handlers', function ( $generator ) {
    // $generator is the Spectra CSS generator instance.
    // Register your own CSS emit handler on it here, for example to
    // capture the generated CSS into your plugin's own cache layer.
} );

For purging the page cache itself, use the host or caching plugin’s own purge API. There’s no Spectra specific purge hook because there’s no disk cache to purge.

Turning Off CSS Caching During Development

If you’re hot editing CSS while building a site, you may want Spectra to rebuild its CSS on every load instead of serving a cached copy. Return false from the filter to opt out:

php

add_filter( 'spectra_enable_css_cache', '__return_false' );

With caching off, the generated CSS is rebuilt on each request, so your changes show up immediately. This adds overhead, so remove the filter (or let it return true) once you’re done developing and back in production.

Hosts and Third-Party Caches

Since Spectra Blocks keeps no file-based cache directory, there’s nothing to delete from disk. To serve fresh output, purge the host’s own page cache layer, which is the same flush a caching plugin performs.

  • Already supported: Spectra Blocks Pro wires in support for LiteSpeed, Breeze, and Hummingbird, so on those the page cache is purged for you.
  • Other hosts and caches: Trigger a purge through that host’s own caching plugin or API.

Frequently Asked Questions

Q: Does Spectra Blocks write CSS files to disk?
A: No. CSS is generated inline and stored against an asset version key in memory and transients, not as files in an asset directory.

Q: How do I stop Spectra from caching CSS while I work?
A: Return false from the spectra_enable_css_cache filter. Remember to remove it before going to production.

Q: How do I purge the cache so visitors see my latest changes?
A: On LiteSpeed, Breeze, or Hummingbird with Spectra Blocks Pro, this is handled for you. On other setups, purge through your host’s caching plugin or API, the same way you would for any other change.

Q: Where do I get the purge function for my environment?
A: There’s no Spectra disk cache to purge, so you don’t need one for Spectra itself. Use your host or caching plugin’s purge API. If you need a specific function, ask your caching plugin’s support which method clears the page cache in your environment.

Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

Need help? Contact Support

Get Notified When Available

Note - You can purchase the Essential Toolkit now and easily upgrade to the Business Toolkit once it becomes available.

Get your hands on Spectra Pro

Enter your name and email address to get access to Spectra Pro.