location.reload 重新加载大部分内容

location.reload reloads most things

我正在创建一个多语言网站,它通过我设置的名为 nav_lang 的 cookie 了解用户的 "navigation language"。目前,我设置它的方式是,如果用户选择通过一种新语言导航,它会将 cookie 设置为该语言(例如印地语)并运行 location.reload()。

我在查看 Chrome 的 "inspect element" 函数时注意到,它从服务器而不是缓存重新加载 Javascript 和字体文件。我希望它重新加载页面而不是它的所有元素。有趣的是,从 SDN(例如 jQuery)中提取的文件不会重新加载,因为 304 Not Modified 消息是 returned 的。另外,我自己服务器上的PNG文件return 304 Not Modified,所以不要重新下载。

如何配置服务器以便 location.reload() 只重新加载页面本身而不是辅助文件(例如,字体、脚本、css 等)?我想这是我的 Apache 配置中的一些设置。我是 运行 Apache 2.4.9,它是 Bitnami 的 Lampstack 版本 5.4.30-0 的一部分。

reload() 方法从浏览器缓存中重新加载。 您可以通过将 forceGet 参数设置为 true 来强制重新加载以从服务器获取页面:location.reload(true);

在您的情况下,您需要在 web 目录的 .htaccess 文件或 apache 的虚拟主机文件中设置 css、js、图像和字体的过期时间


<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 0 seconds"



# Your document html
  ExpiresByType text/html                 "access plus 0 seconds"

# Data
  ExpiresByType text/xml                  "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType application/json          "access plus 0 seconds"

# RSS feed
  ExpiresByType application/rss+xml       "access plus 1 hour"

# Favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week"

# Media: images, video, audio
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpg                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"
  ExpiresByType video/webm                "access plus 1 month"

# HTC files  (css3pie)
  ExpiresByType text/x-component          "access plus 1 month"

# Webfonts
  ExpiresByType font/truetype             "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# CSS and JavaScript
  ExpiresByType text/css                  "access plus 1 year"
  ExpiresByType application/javascript    "access plus 1 year"
  ExpiresByType text/javascript           "access plus 1 year"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>

您可以在 http://www.paulund.co.uk/set-expire-headers-in-htaccess

获得更多信息