Search code examples
node.jsexpressviewhandlebars.js

How do I clear the view cache for a single file in express.js?


when i render my homepage like this:

router.get('/', function(req, res, next) {

    res.render('../_cache/homepage-1.hbs', {
        title: 'Home',
        style: 'home-new',
        projectSlug: 'homepage',
    });
});

it seems to cache the way homepage-1.hbs was when the first started. if i then edit the file, it will still show the old one until i reboot the server. but only in production, it does not happen in development.

How can i clear this cache?

edit: apparently it is caused by the view engine, which for this: https://www.npmjs.com/package/hbs

there is a cache built into this view engine, but not documention on how to access it.


Solution

  • this will

     const hbs = require('hbs');
     let fullPath = path.resolve('_cache/homepage-1.hbs');
     delete hbs.cache[fullPath];
    

    this is how you delete stuff from the view cache in hbs.