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

next-build-id

v3.0.0

Published

Use a consistent, git-based build id for your Next.js app

Downloads

148,664

Readme

next-build-id

Use a consistent, git-based build id for your Next.js app

Build Status Coverage Status JavaScript Style Guide Conventional Commits Greenkeeper badge

Small package to generate a consistent, git-based build id for your Next.js app when running next build on each server in a multi-server deployment.

This module exports a function that you can use as your generateBuildId config option in next.config.js.

By default, it will use the latest git commit hash from the local git repository (equivalent of git rev-parse HEAD):

// next.config.js
const nextBuildId = require('next-build-id')
module.exports = {
  generateBuildId: () => nextBuildId({ dir: __dirname })
}
// => 'f9fc968afa249d162c924a8d5b4ce6562c164c2e'

If you'd rather use a build id relative to the most recent tag in your git repo, pass describe: true as an option and the output of git describe --tags will be used instead:

// next.config.js
const nextBuildId = require('next-build-id')
module.exports = {
  generateBuildId: () => nextBuildId({ dir: __dirname, describe: true })
}
// => 'v1.0.0' (no changes since v1.0.0 tag)
// => 'v1.0.0-19-ga8f7eee' (19 changes since v1.0.0 tag)

This module also exposes a synchronous version for custom needs, e.g. passing the build id directly to a Sentry configuration. Just call nextBuildId.sync({ dir: __dirname }) instead.

Why?

If you're running multiple instances of your app sitting behind a load balancer without session affinity (and you're building your app directly on each production server instead of pre-packaging it), a tool like this is necessary to avoid Next.js errors like "invalid build file hash", which happens when the same client (browser code) talks to multiple server backends (Node server) that have different build ids.

The build id used by your app is stored on the file system in a BUILD_ID text file in your build directory, which is .next by default.

Install

$ npm i next-build-id

API

This module exports two functions, one that is asynchronous (nextBuildId() primary export) and one that is synchronous (nextBuildId.sync()). Both functions accept a single options object, supporting the same options listed below. Both functions return (or resolve to) a string, representing the git-based build id.

The options supported are:

  • dir (string, default process.cwd()): a directory within the local git repository

    Using __dirname from your next.config.js module is generally safe. The default value is assumed to be the directory from which you are running the next build command, but this may not be correct based on how you build your Next.js app.

  • describe (boolean, default false): use git tag description instead of latest commit sha

    Specify this as true to use git describe --tags instead of git rev-parse HEAD for generating the build id. If there are no tags in your local git repository, the latest commit sha will be used instead, unless you also specify fallbackToSha: false.

  • fallbackToSha (boolean, default true): fallback to latest commit sha when describe: true and no tags exist

    Only applies when using describe: true. If you want to be strict about requiring the use (and presence) of tags, then disable this with fallbackToSha: false, in which case an error will be thrown if no tags exist.

Note that this module really provides a generic way to get an id or status string for any local git repository, meaning it is not directly tied to Next.js in any way - it just depends on how you use it.

Reference

License

ISC © Andrew Goode