Asset Auto Cache Busting
Automatically appends the file's last-modified time (mtime) as the version query string for enqueued scripts and styles, ensuring effective cache-busting.
by Joan Piedra (@neojp) · github.com/neojp/asset-auto-cache-busting · website
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/neojp/asset-auto-cache-busting/archive/refs/heads/main.zipAutomatically appends the file's last-modified time (mtime) as the version query string for enqueued scripts and styles, ensuring cache-busting.
Description
Stop worrying about manually bumping version numbers every time you make a change to your CSS or JS files. Asset Auto Cache Busting ensures your users always see the latest version of your assets by dynamically appending the file's modification time (mtime) as the ver query parameter.
WordPress typically versions assets using a static theme or plugin version number. If you forget to update that number, browsers and caching layers may continue serving stale files. This plugin automates the process, ensuring that as soon as you save a change to a file on disk, the browser sees a new version string and fetches the fresh file.
Key Features
- Automatic Cache Busting: Leverages
filemtime()to update asset versions instantly upon modification. - Safe and Selective: Automatically ignores external URLs, CDNs, and files not found on the local server to prevent broken requests.
- Lightweight: Written with modern PHP namespaces and zero dependencies.
- Zero Configuration: Activate it, and it works for all properly enqueued WordPress assets.
How it Works
The plugin intercepts asset enqueues, maps the URL to a local filesystem path, and appends the file's last-modified timestamp to the query string.
graph TD
A[wp_enqueue_script/style called] --> B{Is it a local file?}
B -- No/External CDN --> C[Keep original URL]
B -- Yes --> D[Get local server path]
D --> E{Does file exist?}
E -- No --> C
E -- Yes --> F[Get filemtime / last-modified time]
F --> G[Append ?ver=TIMESTAMP]
G --> H[Output to HTML]
H --> I{Caching Layer present?}
I -- Page Cache/Varnish --> J[Treat as unique URL & Refresh Cache]
I -- Edge Cache/CDN --> K[Cache Miss & Re-fetch]
I -- Browser Cache --> L[Cache Busting: Force reload]
Installation
- Upload the
asset-auto-cache-bustingfolder to the/wp-content/plugins/directory. - Activate the plugin through the 'Plugins' menu in WordPress.
- That's it! All enqueued scripts and styles will now use their actual file modification time for versioning.
Frequently Asked Questions
Does this work with minified files?
Yes. As long as your minification process updates the file on the server, this plugin will detect the new timestamp and update the version string accordingly.
Does this work on managed hosting platforms?
Yes. This plugin has been tested and verified to work seamlessly on enterprise-managed WordPress environments, including:
- Pantheon: Integrates perfectly with their global CDN and Varnish-based edge caching.
- WP Engine: Fully compatible with their proprietary caching layers, ensuring that your asset updates are purged and refreshed as expected across their architecture.
Does this affect files loaded from a CDN?
No. The plugin verifies the host of the asset URL. If the asset is served from a different domain (like a CDN), it leaves the URL untouched.
Will this slow down my site?
The plugin performs a simple filesystem stat check to get the file modification time. In almost all environments, this is extremely fast and adds negligible overhead.
However, if you are using advanced caching layers, keep in mind:
- Page Caching (e.g., Varnish, Nginx FastCGI Cache): Since this plugin modifies the asset URL by changing the query string (e.g.,
?ver=123456789), the page cache will treat this as a unique URL. This ensures that when you update a file, the cache is effectively "busted" and users receive the new version immediately. - Edge Caching (e.g., Cloudflare, CloudFront): If you use a CDN, these services will also respect the new query string. When the version string updates, the CDN will see it as a "cache miss" and fetch the fresh file from your server.
In short, this plugin helps your caching layers work more accurately by ensuring they don't serve stale files after you have deployed updates.
Support & Contributing
Issues
If you encounter any bugs or have feature requests, please submit them via the GitHub Issues tracker.
Contributing
Contributions are always welcome! Whether it’s a bug fix, documentation improvement, or a new feature, feel free to submit a pull request on GitHub.
Donations
If this plugin has helped you save time or improved your workflow, consider supporting its development: https://ko-fi.com/neojp
Changelog
1.0.0
- Initial release.
Author
This plugin is developed and maintained by Joan Piedra (@neojp). You can find more of my work at joanpiedra.com
Sponsored by
This plugin is proudly maintained and sponsored by 256bits.dev
License
GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.html)