idearia/wordpress-cache-busting

在WordPress中选择性清除浏览器缓存

1.2 2023-05-18 14:41 UTC

This package is auto-updated.

Last update: 2024-09-18 17:47:59 UTC


README

清除通过wp_enqueue_script和wp_enqueue_style加载的CSS和JS文件的浏览器缓存。

缓存可以通过以下两种方式之一进行清除:

  • 静态方式,指定固定值作为ver查询参数的值,
  • 动态方式,使用文件最后更改的时间戳设置'ver'(使用filemtime())。

您需要在配置主类中的$this->assets数组时选择所需的方式在资产级别配置。

灵感来源于https://www.recolize.com/en/blog/wordpress-cache-busting-design-changes/

# 安装

使用Composer v2或更高版本

composer require idearia/wordpress-cache-busting

如果您不想使用Composer,只需包含src/CacheBuster.php文件。

示例

class CacheBuster extends \Idearia\WordPressCacheBusting\CacheBuster
{
	protected $assets = [
		/**
		 * Example of dynamic cache invalidation
		 */
		[
			'handle' => 'some-script-or-css',
			'path'   => 'wp-content/plugins/some-plugin/script.js',
		],
		/**
		 * Example of static cache invalidation
		 */
		[
			'handle' => 'some-other-script-or-css',
			'ver'    => '1.2.3',
		],
	];
}

new CacheBuster;