2

我第一次创建插件并面临有关缓存的主要问题。

我将我的 js 文件注册为

wp_register_script("custom-js-backend", PLUGIN_URL . 'assets/js/custom.js', array(
                'jquery',
                'jquery-blockui'
));
wp_enqueue_script("custom-js-backend");

但无法在管理面板上反映更改。

试过了

function cache_cleanup()
{
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_footer', 'wp_generator');
}
add_action('init', 'cache_cleanup');  

也在 wp-config.php 中定义

define('WP_CACHE',false);

但没有运气。

由于默认情况下我的 js 文件带有 5.1.0 版本,因此尝试使用https://wordpress.org/plugins/disable-version-caching/从中删除该版本

现在版本正在被删除,但文件仍然没有更新。

一种解决方案是在我的 js 文件中添加版本号并在每次小的更改后更改版本,但这完全不对。

任何帮助如何在创建插件时禁用管理面板的缓存。

4

2 回答 2

0

添加这个以.htaccess在开发期间禁用浏览器缓存

# Disable browser caching.
# Ensure mod_headers is installed.
# To install on Debian, run `a2enmod headers`, then restart the server.
FileETag None
Header unset ETag
Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
Header set Pragma "no-cache"
Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"

注意:您的浏览器可能仍会缓存来自 CDN 的外部内容,但不会缓存您服务器上的任何内容。

于 2020-09-28T13:34:19.260 回答
0

您可以filemtime(PLUGIN_URL . 'assets/js/custom.js')用作版本号 - 这将使版本号,最后一次保存文件的 unix 时间 -

这意味着每次您相同的文件时,您都会自动获得一个比上一个更高的新版本号,这样它仍然可以缓存,但在有更改时会被清除

所以它看起来像

wp_register_script("custom-js-backend", PLUGIN_URL . 'assets/js/custom.js', array(
            'jquery',
            'jquery-blockui'
), filemtime(PLUGIN_URL . 'assets/js/custom.js'), true);
于 2020-09-11T10:45:10.497 回答