39

How do I define

<meta http-equiv="cache-control" content="no-cache" />

in HTML5? It is no longer valid according to the W3C Validator and the documentation.

5
  • Why not send it with the HTTP headers? Jul 12, 2011 at 13:02
  • 1
    @Linus Kleen: I'm looking for a pure HTML solution.
    – powtac
    Jul 12, 2011 at 13:03
  • 13
    I got here precisely because I’m also looking for a pure HTML5 solution. I got my Website hosted on GitHub Pages, which doesn't offer me a way to modify HTTP headers. Jul 14, 2011 at 22:12
  • 2
    @AndrésBotero - Can I ask why you want no-cache on GitHub Pages, given that it's designed for static content?
    – Stu Cox
    Dec 13, 2012 at 10:27
  • "A way to modify headers" in general, not necessarily just no-cache, hehe. I don’t remember now which header I wanted to modify. Dec 13, 2012 at 18:23

4 Answers 4

12

Putting caching instructions into meta tags is not a good idea, because although browsers may read them, proxies won't. For that reason, they are invalid and you should send caching instructions as real HTTP headers.

8

In the beginning of code you need to use this:

<!DOCTYPE html>
<html manifest="cache.manifest">

...

Then create cache.manifest file with content of what you want to cache i.e

CACHE MANIFEST
# 2010-06-18:v2

# Explicitly cached 'master entries'.
CACHE:
/favicon.ico
index.html
stylesheet.css
images/logo.png
scripts/main.js

# Resources that require the user to be online.
NETWORK:
*

# static.html will be served if main.py is inaccessible
# offline.jpg will be served in place of all images in images/large/
# offline.html will be served in place of all other .html files
FALLBACK:
/main.py /static.html
images/large/ images/offline.jpg

A manifest can have three distinct sections: CACHE, NETWORK, and FALLBACK.

CACHE: This is the default section for entries. Files listed under this header (or immediately after the CACHE MANIFEST) will be explicitly cached after they're downloaded for the first time.

NETWORK: Files listed in this section may come from the network if they aren't in the cache, otherwise the network isn't used, even if the user is online. You can white-list specific URLs here, or simply "", which allows all URLs. Most sites need "".

FALLBACK: An optional section specifying fallback pages if a resource is inaccessible. The first URI is the resource, the second is the fallback used if the network request fails or errors. Both URIs must from the same origin as the manifest file. You can capture specific URLs but also URL prefixes. "images/large/" will capture failures from URLs such as "images/large/whatever/img.jpg".

1
7

There is no HTML solution. Mozilla's application cache (cache.manifest) is deprecated. The application cache site says:

This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible. ... Use Service Workers instead.

Apart from that, I suggest you use HTTP Cache-Control to solve cache issues.

1

There isn't an HTML solution, because it's not a markup problem. Caching is an action on the resource, not part of the resource definition itself.

As others have said, HTTP headers are the best way to control caches, because these are observed by all caches - <meta> tags are only observed by browser caches. These should be set by your server / web framework.

That said, I wouldn't be surprised if browsers still observe <meta http-equiv="cache-control" content="no-cache"> for pages with the HTML5 doctype.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.