Consistent Git-based Build ID for Next.js

3.0.0 · active · verified Tue Apr 21

next-build-id is a utility package for Next.js applications, currently at version 3.0.0. It addresses a common issue in multi-server deployments where different instances of a Next.js app might have varying build IDs, leading to "invalid build file hash" errors for clients. This package provides a mechanism to generate a consistent build ID, derived from the local Git repository's state, specifically the latest commit hash (`git rev-parse HEAD`) or a description based on the most recent Git tag (`git describe --tags`). It exports an asynchronous function (the primary export) and a synchronous variant, both intended to be used with Next.js's `generateBuildId` configuration option in `next.config.js`. The package helps ensure that all deployed instances of a Next.js application serve assets built with the same identifier, crucial for load-balanced environments without sticky sessions where apps are built directly on each server.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate next-build-id into your Next.js configuration to generate a consistent, git-based build ID, preventing deployment issues.

// next.config.js
const nextBuildId = require('next-build-id');

/** @type {import('next').NextConfig} */
const nextConfig = {
  // Required to ensure consistent build IDs across multiple servers
  // and prevent 'invalid build file hash' errors.
  generateBuildId: async () => {
    // Use the latest git commit hash as the build ID by default.
    // Pass { describe: true } to use git tags instead (e.g., 'v1.0.0-19-ga8f7eee').
    // The `dir` option should point to your project's root containing the .git folder.
    return nextBuildId({ dir: __dirname });
  },
  // Other Next.js configurations here...
};

module.exports = nextConfig;

view raw JSON →