Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache with manifest Node.js site

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

like image 265
enedebe Avatar asked Feb 01 '12 07:02

enedebe


People also ask

What is manifest cache?

The cache manifest file is a simple text file that lists the resources the browser should cache for offline access.

How do you caching in node JS?

const express = require('express'); const fetch = require('node-fetch'); const NodeCache = require('node-cache'); // stdTTL is the default time-to-live for each cache entry const myCache = new NodeCache({ stdTTL: 600 }); // retrieve some data from an API async function getPosts() { const response = await fetch(`https ...

Does Nodejs cache require?

Per the node documentation, modules are cached after the first time they are loaded (loaded is synonymous with 'required'). They are placed in the require. cache . This means that every future require for a previously loaded module throughout a program will load the same object that was loaded by the first require.


1 Answers

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

like image 119
Jan Wikholm Avatar answered Oct 19 '22 06:10

Jan Wikholm