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

redhead

v0.4.0

Published

Dynamically setup headers and redirects for you static deployments

Downloads

17

Readme

RedHead

Motivation

When deploying our website we realized we wanted to have a very subtle difference in the redirects if the environment was staging or production, we could have gone for the ENV variable option, but the netlify.toml file does not allow environment variable interpolation, so you end up having to use a sed command (or multiple) to do the replacement, something like:

sed -i s/REDIRECT_1_PLACEHOLDER/${REDIRECT_1_VALUE}/g netlify.toml
sed -i s/REDIRECT_2_PLACEHOLDER/${REDIRECT_2_VALUE}/g netlify.toml
yarn build

After that, we noticed that many static deployment sites have similar limitations, that lead us to creating RedHead, and now you can simply do:

redhead build && yarn build

Table of content

Installation

yarn global add redhead

Or you can add it to your package.json

yarn add redhead --dev
$ npm install -g redhead
$ redhead COMMAND
running command...
$ redhead (-v|--version|version)
redhead/0.4.0 darwin-x64 node-v11.9.0
$ redhead --help [COMMAND]
USAGE
  $ redhead COMMAND
...

Supported Platforms

We currently two static deployments, but we plan on adding more (contributions are welcome):

Currently supported

Plan on supporting

Commands

redhead build

Generate the platform specific files based on the configuration

USAGE
  $ redhead build

OPTIONS
  -o, --output=output              [default: .] Folder where the generated files should be saved.
  -p, --platform=netlify|firebase  [default: netlify] The target platform for the generated files

See code: src/commands/build.js

redhead help [COMMAND]

display help for redhead

USAGE
  $ redhead help [COMMAND]

ARGUMENTS
  COMMAND  command to show help for

OPTIONS
  --all  see all commands in CLI

See code: @oclif/plugin-help

redhead init

Initialize the required files

USAGE
  $ redhead init

OPTIONS
  -h, --no-headers    Whether or not to handle headers with redhead
  -r, --no-redirects  Whether or not to handle redirects with redhead

DESCRIPTION
  Generates files for handling your headers and/or redirects configuration.

See code: src/commands/init.js

Examples

Different config based on environment

For example, if you want to have different headers based on the environment you just need to customize the headers.js file for your needs and make sure you ENV variables are set for each case, for Netlify this could be done via the netlify.toml file.

// .redhead/headers.js

const headers = [];

if (process.env.NODE_ENV === 'production') {
  headers.push({
    path: '/cool',
    headers: [
      'X-Cool: 123'
    ],
  });
}

module.exports = headers;

Redirecting one path to the latest post

Let's say you have a blog and want to have a /latest path that always takes users to the latest post that has been published, this could be easily achieved with RedHead.

// .redhead/redirects.js

// use your DB library here;
const db = require('db').config(process.env.CONNECTION_URL);
const lastPost = db.posts.last();

module.exports = [{
  from: '/latest',
  to: `${lastPost.permalink}`,
  status: '302',
  options: '',
}];

Contributing

All contributions or issue reporting are welcomed. If you are filing a bug please include information to help debug it!

If you plan to contribute, please make sure you test the code.

Credits