How to cache with manifest Node.js site

enedebe picture enedebe · Feb 1, 2012 · Viewed 7k times · Source

In relation with my early question of how to add manifest cache in node.js, my question now is related with how to cache the HTML generated by node.js. As we didn't have a physical file like in php (index.php) we cannot cache such kind of files.

How we can cache a "non existing" page? Just adding in cache:

    CACHE MANIFEST

    CACHE:
    # plain files to cache
    /javascripts/client.js
    /stylesheets/style.css
    /stylesheets/style.styl
    # generated files like /
    /
    /content

Any idea of how to solve this problem?

Thanks!

Solution:

Add router to return the cache.manifest file with the correct mime-type:

app.get("/offline.manifest", function(req, res){
  res.header("Content-Type", "text/cache-manifest");
  res.end("CACHE MANIFEST");
});

Found at stackoverflow

Answer

Jan Wikholm picture Jan Wikholm · Feb 1, 2012

The cache manifest list URLs that should be cached. The client accessing those urls has no knowledge whether these are static html files on top of Apache or dynamic content generated by node.js or anything else.

You are basically instructing the client:

  1. Read my list of urls
  2. Go through each url
  3. Download the response and store it someplace safe
  4. Check back on my cache.manifest if it has changed and then proceed to step 1

So as long as your data generated by node.js is reachable via a URL there is no problem in defining it as a line in the cache manifest.

And if you are worried "how will I know which urls there are" you can always generate the cache.manifest file programmatically from node.js itself -- but remember to serve the correct content-type text/cache-manifest