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

@adultswim/express-includes-middleware

v0.2.1

Published

Express middleware for loading static include files

Downloads

74

Readme

@adultswim/express-includes-middleware

Express middleware for loading static include files

Installation

npm install @adultswim/express-includes-middleware

Dependencies

  • axios ^0.18.0
  • html-minifier ^3.5.12
  • p-memoize ^1.0.0

API

CommonJS:

const { includesMiddleware } = require('@adultswim/express-includes-middleware')

TypeScript:

import includesMiddleware from '@adultswim/express-includes-middleware'

includesMiddleware(options)

Returns a middleware function for fetching and attaching site includes to res.locals on each request.

Options

baseURL

Required

Type: number

The base URL to be prepended to all include paths unless the include path is absolute.

Example: https://www.adultswim.com

maxAge

Type: number

Milliseconds until the cache expires. Caching is disabled unless this is provided.

minify

Type: boolean or an html-minifier options object

Default: false

Controls the minification of includes. If true is passed the default minification options will be:

{
    collapseWhitespace: true,
    minifyCSS: true,
    minifyJS: true,
    removeComments: true,
}
paths

Required

Type: object | string[]

Either a list of key/values where the key is the name on res.locals.includes and the value is a string of the path to the include, or an array of path strings where the path string will also be the resulting key on res.locals.includes.

All paths are prefixed with the baseURL option.

Example with Object:

{
  legal: '/tools/includes/legal.html'
}

will result in res.locals.includes.legal being "/tools/includes/legal.html"

Example with Array:

[
    '/tools/includes/global-nav.html'
]

will result in res.locals.includes["/tools/includes/global-nav.html"] being "/tools/includes/global-nav.html"

Example Usage

Setup the middleware on the app route:

import express from 'express';
import includesMiddleware from '@adultswim/express-includes-middleware';

const app = express();

// load includes on all GET requests
app.get('*', includesMiddleware({
  baseURL: 'https://www.adultswim.com',
  maxAge: 300000, // cache includes for 5 minutes
  minify: true,
  paths: {
    // content will be available as raw text on res.locals.includes.nav:
    'nav': '/tools/includes/global-nav.html',
    'footer': '/tools/includes/legal.html',
  }
}), (req, res) => {
  res.render('client');
});

Then output the result in the view:

// client.pug
// res.locals is automatically provided to the view by Express

doctype html
html
  head
    title My App
   body
     != includes.nav
     #app
     != includes.footer