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

@stevent-team/epoxy

v2.2.0

Published

Lightweight server-side per-route html injection

Downloads

7

Readme

🪣 Epoxy

npm version minzip size

Simple server-side per-route html injection

Purpose

Epoxy allows for running middleware on select HTTP routes that inject HTML into the served content. Simply point it at a static directory, define some routes in a script and you're off to the races! :horse:

Epoxy comes with esbuild, which is used for transpiling a route handler file.

Usage

Add as a dependency

yarn add @stevent-team/epoxy

Options

You can also run npx epoxy --help to see this information.

epoxy

Usage: epoxy <target> [routeFile] [options]

Serve the provided static folder

Arguments:
  target                 Path to static directory
  routeFile              Path to cjs router script (can use ES6 with --build option)

Options:
  -V, --version          output the version number
  -p, --port <port>      port to use for http server (default: 8080)
  -h, --host <url>       host to use for http server (default: "0.0.0.0")
  -i, --index <path>     path to index html inside of target (default: "index.html")
  -b, --build            build routes file in memory (default: false)
  --no-cache             disable all route handler result caching
  --help                 display help for command

epoxy build

Usage: epoxy build <routeFile> [options]

Build a routes file

Arguments:
  routeFile                Path to ES6 router script

Options:
  -o, --outputDir <path>   folder to output built routes file (default: "dist")
  -h, --help               display help for command

Example

Create a script to define your dynamic routes.

// Handlers are async functions returning HTML to be injected
const routeHandler = async req => {
  const { params } = req // Get route params from request

  return {
    head: `<meta property="coolness" value="${params.coolness}">`, // Injected into the end of the <head>
    body: 'Howdy!' // Injected into the end of the <body>
  }
}

export default {
  '/some/route/with/:coolness': routeHandler
}

createMeta Helper

Epoxy comes with a helper that allows you to easily create meta tags from an object.

import { createMeta } from '@stevent-team/epoxy/helpers'

const routeHandler = async ({ params }) => {
  // Create a string of meta tags from the object passed in
  const metaTags = createMeta({
    coolness: params.coolness,
    description: 'A pretty cool page',
  })

  return { head: metaTags }
}

export default {
  '/route/:coolness': routeHandler
}

For more information about the helpers available in Epoxy, see the readme.

Then serve your static directory and dynamic routes!

epoxy ./dist ./routes.js --build

or setup an npm script

// package.json
{
  "scripts": {
    "serve": "epoxy ./dist ./routes.js --build"
  }
}

If you have a deployment that will need to start and stop your Epoxy server often, such as an automatically scaled deployment like Google App Engine, then you can prebuild the routes file so it doesn't have to build before every start:

// package.json
{
  "scripts": {
    "build": "epoxy build ./routes.js",
    "serve": "epoxy ./dist ./dist/routes.js"
  }
}

Alternatively, you could also write your routes file in CommonJS so it doesn't require building; the epoxy command only builds if the flag --build is specified.

API

Your route handler will be built by Epoxy using Parcel when Epoxy is started. It also has to export a default object with the structure:

export default {
  'express/js/route/:withParams': yourRouteHandlerFunction
}

// or

export default {
  'express/js/route/:withParams': {
    handler: yourRouteHandlerFunction,
    key: request => ['A key to cache with', request.params.withParams], // optional, any type, will cache result based on key
    ttl: 3600000, // time to live, optional, in milliseconds
  }
}

If you include a key that is not undefined, then the result of the handler function will be cached based on that key. You can also include a time to live ttl parameter which will expire the cache after a certain amount of milliseconds. There is also a command line argument --no-cache that will disable all caching irrespective of any keys provided.

Each route must have a function to handle it, which will receive a request object from ExpressJS, from which you can learn about the request. See the express docs for the request object for more information.

Contributing

PRs and Issues are more than welcome :)

This library uses changesets. If the changes you've made would constitute a version bump, run yarn changeset and follow the prompts to document the changes you've made. Changesets are consumed on releases, and used to generate a changelog and bump version number.

License

Created by Stevent (2022) and licensed under MIT