如何将版本号添加到HTML文件(不仅是CSS和JS文件) [英] How to add version number to HTML file (not only to css and js files)

查看:1115
本文介绍了如何将版本号添加到HTML文件(不仅是CSS和JS文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人使用CSS和js文件上的版本号来强制非缓存版本在发布更新时加载到网页上:

Many people use version numbers on css and js files to force non-cached versions to load up on webpages when updates are published:

CSS示例:

<link rel="stylesheet" type="text/css" href="style.css?v=2017-03-17">

JS示例:

<script type="text/javascript" src="js/myscript.js?v=2017-03-17"></script>

我注意到,通过将版本号添加到css/js文件中,网络浏览器还将从头开始加载 HTML文件(从中引用css/js文件)不使用缓存的版本.

I've noticed that by adding version numbers to css/js files, the web browsers will also load up the HTML file (from which the css/js files are referenced from) from scratch and not use the cached version.

这是确保HTML文件在所有网络浏览器中从头开始显示的一种充分方法,还是一种可以为 HTML文件设置版本号的方法,以确保新是不是从浏览器的缓存中加载了更新的HTML文档?

Is this a sufficient way of ensuring that HTML-files are displayed from scratch in all web browsers, or is there a way to set a version number to the HTML file as well, to ensure newly updated HTML-documents are not loaded from a browser's cache?

推荐答案

通常,浏览器始终采用HTML的较新版本.

Usually the browser takes always the newer version of your HTML.

如果您希望防止任何缓存,可以使用以下技巧:

If you wish to prevent any cache you can use those tricks:

最正确的最小标头集,适用于大多数 重要的浏览器:

The correct minimum set of headers that works across the most important browsers:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

HTML

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

.htaccess(Apache)

.htaccess (Apache)

<IfModule mod_headers.c>
  Header set Cache-Control "no-cache, no-store, must-revalidate"
  Header set Pragma "no-cache"
  Header set Expires 0
</IfModule>

Java Servlet

Java Servlet

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);

PHP

header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

ASP

Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate"
Response.addHeader "Pragma", "no-cache"
Response.addHeader "Expires", "0"

ASP.NET

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "0");

Ruby on Rails

Ruby on Rails

response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'

Flask上的Python

Python on Flask

resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"

Google Go

Google Go

responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
responseWriter.Header().Set("Pragma", "no-cache")
responseWriter.Header().Set("Expires", "0")

来源: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆