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.
npm install next-build-idVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to integrate next-build-id into your Next.js configuration to generate a consistent, git-based build ID, preventing deployment issues.
Ensure `git` is installed and in the system's PATH during the build process. For Docker, add `RUN apt-get update && apt-get install -y git` or equivalent for your base image.
Verify that the `dir` option resolves to a path containing the `.git` directory relevant to your project's version control. For monorepos, this might require adjusting the path relative to the `next.config.js` location.
Ensure your repository has relevant Git tags if you rely on the `describe` option. If strict tag usage is required, set `fallbackToSha: false` to force an error when no tags are found.
Monitor Next.js release notes for changes related to build ID generation. For critical integrations, consider direct Git SHA retrieval rather than relying solely on the Next.js `generateBuildId` hook for custom release tracking.
Ensure you run `npm run build` (or `yarn build`) before starting your Next.js application in production. Verify that the `.next` directory, including the `BUILD_ID` file, is correctly deployed to your server. This error can also occur if `next-build-id` fails to generate an ID due to missing `git`.
Implement `next-build-id` in your `next.config.js`'s `generateBuildId` function to ensure all deployed instances of your Next.js application use a consistent, git-based build ID. This is critical for load-balanced environments without session affinity.