Last active
February 11, 2023 22:03
-
-
Save ocean90/1966227 to your computer and use it in GitHub Desktop.
WordPress Plugin: Filename-based cache busting for scripts/styles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Filename-based cache busting | |
* Version: 0.3 | |
* Description: Filename-based cache busting for WordPress scripts/styles. | |
* Author: Dominik Schilling | |
* Author URI: https://dominikschilling.de/ | |
* Plugin URI: https://gist.github.com/ocean90/1966227/ | |
* | |
* License: GPLv2 or later | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* | |
* | |
* Extend your .htaccess file with these lines: | |
* | |
* <IfModule mod_rewrite.c> | |
* RewriteEngine On | |
* RewriteBase / | |
* | |
* RewriteCond %{REQUEST_FILENAME} !-f | |
* RewriteCond %{REQUEST_FILENAME} !-d | |
* RewriteRule ^(.+)\.(.+)\.(js|css)$ $1.$3 [L] | |
* </IfModule> | |
*/ | |
/** | |
* Moves the `ver` query string of the source into | |
* the filename. Doesn't change admin scripts/styles and sources | |
* with more than the `ver` arg. | |
* | |
* @param string $src The original source. | |
* @return string | |
*/ | |
function ds_filename_based_cache_busting( $src ) { | |
// Don't touch admin scripts. | |
if ( is_admin() ) { | |
return $src; | |
} | |
$_src = $src; | |
if ( '//' === substr( $_src, 0, 2 ) ) { | |
$_src = 'http:' . $_src; | |
} | |
$_src = parse_url( $_src ); | |
// Give up if malformed URL. | |
if ( false === $_src ) { | |
return $src; | |
} | |
// Check if it's a local URL. | |
$wp = parse_url( home_url() ); | |
if ( isset( $_src['host'] ) && $_src['host'] !== $wp['host'] ) { | |
return $src; | |
} | |
return preg_replace( | |
'/\.(js|css)\?ver=(.+)$/', | |
'.$2.$1', | |
$src | |
); | |
} | |
add_filter( 'script_loader_src', 'ds_filename_based_cache_busting' ); | |
add_filter( 'style_loader_src', 'ds_filename_based_cache_busting' ); |
Is this plugin similar in functionality/purpose to the Busted plugin?
Is it possible to applies the feature to images?
This gist was the start point for this plugin: https://wordpress.org/plugins/filename-based-asset-cache-busting/
It's been improved/has a few new features + conveniences compared to the gist; eg: it modifies .htaccess for you, sets caching headers properly and includes a querystring mode if you can't get .htaccess to work on your hosting.
(Disclaimer: I adapted the gist to create the plugin)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got it working on nginx, but had to change it to filename modified date instead of the version string.
The advantage is that with this rule the server first checks whether the requested filename exists and only if it doesn't tries to serve the file without the version number.
The solution is heavily based on these two:
https://wordpress.org/plugins/filename-based-asset-cache-busting/
https://github.com/h5bp/server-configs-nginx/blob/master/h5bp/location/cache-busting.conf