問題描述

我在谷歌速度測試中遇到關於我的腳本中的查詢字符串的遲到。所以,我試圖通過傳遞 false 作為參數的參數來刪除它們。但是,它似乎沒有效果:

wp_register_script('myscript', get_bloginfo('template_directory').'/scripts.myversionnumber.js',false,false,true);
wp_enqueue_script('myscript');

PS:js 文件名的”myversionnumber” 部分是我控制緩存/版本控制的手段,而不是顯示出一些代理服務器 (根據 Google pagepeed 測試) 的?ver =方法:

Remove query strings from static resources Enabling public caching in the HTTP headers for static resources allows the browser to download resources from a nearby proxy server rather than from a remote origin server. Learn more

Suggestions for this page

Resources with a “?” in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources:

最佳解決方案

我認為你必須傳遞 NULL 作為第 4 個參數。

wp_register_script(
    'myscript',
    get_bloginfo('template_directory').'/scripts.myversionnumber.js',
    false,
    NULL,
    true);
wp_enqueue_script('myscript');

次佳解決方案

您可以將 null 作為版本值傳遞給 wp_register_scriptwp_enqueue_script,它應該丟棄查詢字符串。

<?php
wp_enqueue_script(
   'myscript',
   '/path/to/script.js',
   array(),
   null
);

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。