ladjs/cache-pug-templates

Name: cache-pug-templates

Owner: Lad

Description: Cache Pug templates for Lad/Koa/Express/Connect with Redis

Created: 2017-09-12 07:10:48.0

Updated: 2018-05-20 00:45:26.0

Pushed: 2018-03-06 20:35:50.0

Homepage: https://lad.js.org

Size: 95

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

cache-pug-templates

build status code coverage code style styled with prettier made with lass license

Cache Pug templates for Lad/Koa/Express/Connect with Redis

Table of Contents
Install

npm:

install cache-pug-templates

yarn:

 add cache-pug-templates
Usage
Basic
t path = require('path');
t redis = require('redis');
t cachePugTemplates = require('cache-pug-templates');

t views = path.join(__dirname, 'views');
ePugTemplates(redisClient, views);
Koa
t path = require('path');
t redis = require('redis');
t Koa = require('koa');
t cachePugTemplates = require('cache-pug-templates');

t app = new Koa();
t redisClient = redis.createClient();

ptional (e.g. if you want to cache in non-production)
pp.cache = true;

ote that koa requires us to specify a
ath name for the views directory
t views = path.join(__dirname, 'views');

listen(3000, () => {
chePugTemplates(app, redisClient, views);

Express
t path = require('path');
t redis = require('redis');
t express = require('express');
t cachePugTemplates = require('cache-pug-templates');

t app = express();
t redisClient = redis.createClient();

ptional (by default express defaults to `./views`)
pp.set('views', path.join(__dirname, 'views'));

set('view engine', 'pug');

listen(3000, () => {
chePugTemplates(app, redisClient, views);

Custom Callback

You can also pass an optional callback function with arguments err and cached.

The argument cached is an Array of filenames that have been cached (from Object.keys(pug.cache)).

..

listen(3000, () => {
chePugTemplates(app, redisClient, (err, cached) => {
if (err) throw err;
console.log(`successfully cached (${cached.length}) files`);
;

Note that with Koa you'll need to pass the views argument as well:

..

t views = path.join(__dirname, 'views');

listen(3000, () => {
chePugTemplates(app, redisClient, views, (err, cached) => {
if (err) throw err;
console.log(`successfully cached (${cached.length}) files`);
;

By default this callback simply console.error the err (if any).

Debugging

If you want to check what the cache state is at anytime:

t pug = require('pug');

..

et everything:
ole.log('pug.cache', pug.cache);

ust get the file names:
ole.log('pug cached files', Object.keys(pug.cache));
Contributors

| Name | Website | | ————– | ————————– | | Nick Baugh | http://niftylettuce.com/ |

License

MIT © Nick Baugh


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.