npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-static-file-cache

v0.2.3

Published

Express middleware that creates a static html cache from jade templating.

Downloads

18

Readme

express-static-file-cache

npm install --save express-static-file-cache

Middleware to cache your Pug, Jade, EJS, or other Express compliant templates as static html. The api gives you the ability to wholesale clear the cache as needed. As a side effect, also gives you the ability to pod organize your jade templates with the middlewares that show them.

Streaming to return content to the user is faster than compiling the entire response first. Static files are streamed by default in express, but many templates do not support streaming. So, even if you cache all database interactions, a static html page will be faster than templating that doesn't support streaming.

Api

configure and clearCache are available directly from the npm.

require('express-static-file-cache').configure({ ... })

Configure return a middleware function to use in your app.

options

  • app : required - the express app being used
  • cacheDir : required - path to root directory of cache
  • express : required - express
  • verbose : optional - if truthy will console log some extra info
  • viewEngine : optional - defaults to pug – options: 'pug', 'jade', or 'ejs' - fallsback to jade if pug is not present and vice versa (in case you are not passing in viewEngine and relying on the default)
  • dev : options - defaults to false - true will send the response but not cache it to a file
var express = require('express'),
    app = express();
    
app.use(require('express-static-file-cache').configure({
    app         : app,
    express     : express,
    cacheDir    : cacheDir
}));

Now in your downstream middleware:

var path = require('path');

function(req, res, next) {
    res.cache(require.resolve('./view.jade', data);
}

require('express-static-file-cache').clearCache()

Calling this method will delete all static cache files. Returns a promise that is resolved once all files are cleared.

clearCache is available both directly from the npm and on the response object:

var staticCache = require('express-static-file-cache')

db.on('content-changed', function() {
    staticCache.clearCache();
});

or

function(req, res, next) {
    
    if (timeToClearCache()) {
        res
            .clearCache()
            .then(function() {
                next();
            });
    } else {
        next();
    }
}

middleware

res.cache and res.clearCache are available as methods on the response object downstream from the addition of the middleware.

To add the caching middleware, use the method returned after configuration:

res.cache(templatePath, templatingData)

res.cache takes the path to a jade template and the data to be used. It will send the response and write a static html file with that repsonse to `path.join(req.url, 'index.html');

function(req, res, next) {
    res.cache(require.resolve('./view.jade'), {
        name : 'jane'
    });

res.clearCache()

Deletes all static cache fiels and returns a promise that is resolved once done.

Directory Structure

Since res.cache takes a path to the Jade file to be used, you can modularize your jade templates.

For example if you always call res.cache(require.resolve('./view.jade'), data), you can organize your "pages" as follows:

pages
    home
        index.js
        view.jade
    notFound
        index.js
        view.jade