uniquejava / blog

My notes regarding the vibrating frontend :boom and the plain old java :rofl.
Creative Commons Zero v1.0 Universal
11 stars 5 forks source link

Disable Browser Caching #273

Open uniquejava opened 5 years ago

uniquejava commented 5 years ago

https://bedigit.com/blog/disable-browser-caching/

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

Where:

Cache-Control is for HTTP 1.1
Pragma is for HTTP 1.0
Expires is for proxies

For the Web Pages (HTML) add the following tags to the page(s) you want to keep browsers from caching (the code must be in the section of your page, for example right after tag):</p> <pre><code><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" /></code></pre> <p>.htaccess (Apache)</p> <pre><code><IfModule mod_headers.c> Header set Cache-Control "no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires 0 </IfModule></code></pre> <p>PHP</p> <pre><code>header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0');</code></pre> <p>Java Servlet</p> <pre><code>response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0);</code></pre> <p>ASP</p> <pre><code>Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" Response.addHeader "Pragma", "no-cache" Response.addHeader "Expires", "0"</code></pre> <p>ASP.NET</p> <pre><code>Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); Response.AppendHeader("Pragma", "no-cache"); Response.AppendHeader("Expires", "0");</code></pre> <p>Ruby on Rails</p> <pre><code>response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' response.headers['Pragma'] = 'no-cache' response.headers['Expires'] = '0'</code></pre> <p>Python on Flask</p> <pre><code>resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" resp.headers["Pragma"] = "no-cache" resp.headers["Expires"] = "0"</code></pre> <p>Google Go</p> <pre><code>responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") responseWriter.Header().Set("Pragma", "no-cache") responseWriter.Header().Set("Expires", "0")</code></pre> <p>Resources</p> <pre><code>http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 http://stackoverflow.com/a/2068407</code></pre> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/uniquejava"><img src="https://avatars.githubusercontent.com/u/1049271?v=4" />uniquejava</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <h2>no-cache 和 no-store的区别</h2> <p><a href="https://stackoverflow.com/questions/7573354/what-is-the-difference-between-no-cache-and-no-store-in-cache-control">https://stackoverflow.com/questions/7573354/what-is-the-difference-between-no-cache-and-no-store-in-cache-control</a></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/uniquejava"><img src="https://avatars.githubusercontent.com/u/1049271?v=4" />uniquejava</a> commented <strong> 5 years ago</strong> </div> <div class="markdown-body"> <h2>HTTP Caching</h2> <p><a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching">https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching</a></p> <h2>Spring 作用于Controller</h2> <pre><code class="language-java">public class ReqInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception { res.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); res.setHeader(HttpHeaders.PRAGMA, "no-cache"); res.setDateHeader(HttpHeaders.EXPIRES, 0); return true; } @Override public void postHandle @Override public void afterCompletion } @Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new ReqInterceptor()); } }</code></pre> <h2>Spring作用于所有请求</h2> <pre><code class="language-java">@Component @Order(2) public class NoCacheFilter implements Filter { private final static Logger logger = LoggerFactory.getLogger(NoCacheFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse res = (HttpServletResponse) response; WebUtils.setNoCache(res); chain.doFilter(request, res); } } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new NoCacheFilter()); // List<String> urlPatterns = new ArrayList<String>(); // urlPatterns.add("*.js"); // urlPatterns.add("*.css"); // registrationBean.setUrlPatterns(urlPatterns); // registrationBean.setOrder(1); return registrationBean; }</code></pre> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>