Code Monkey home page Code Monkey logo

next-build-id's Introduction

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

next-build-id's People

Contributors

greenkeeper[bot] avatar nexdrew avatar shibukawa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

next-build-id's Issues

Problem with dynamic imports

When using dynamic imports i.e.

import dynamic from 'next/dynamic';

const MyModule = dynamic(import('./my-module'), {
  ssr: false,
  loading: () => <span />,
});

Getting bunch of errors:

app.js:1 Uncaught (in promise) Error: Loading chunk 3 failed.

So all chunks are still referencing previous build id

Add synchronous version of plugin

Would it be possible to get synchronous version of this plugin? It would be useful e.g. for passing buildId directly to Sentry configuration in custom server implementation.

Heroku build failures

Tried the next build && next-build-id but get the following error when trying to deploy on Heroku:

fatal: Not a git repository: '/tmp/build_b1a2400958a54ea0fb28e8b3ecb17d71/.git'
    at ChildProcess.exithandler (child_process.js:275:12)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Socket.stream.socket.on (internal/child_process.js:346:11)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at Pipe._handle.close [as _onclose] (net.js:557:12)
  killed: false,
  code: 128,
  signal: null,
  cmd: 'git --git-dir=/tmp/build_b1a2400958a54ea0fb28e8b3ecb17d71/.git --work-tree=/tmp/build_b1a2400958a54ea0fb28e8b3ecb17d71 rev-parse HEAD' }
error Command failed with exit code 1.
       info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: "build:next" exited with 1.

It works fine locally, however. My guess is that because we don't push our .git folder to production, Heroku is failing to recognize our git history. Any suggestion on how to bypass this issue?

How expose buildId to server?

I'm trying to expose the buildId to the express application so I can set cache keys based on the buildId.

What would be the best way of getting the buildId?

An in-range update of tap is breaking the build 🚨

The devDependency tap was updated from 14.8.1 to 14.8.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

tap is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 3 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Next.js build fails due to git rev-parse HEAD error

Using this in next.config.js is triggering the following error:

10:11:16.084 | > Build error occurred
-- | --
10:11:16.087 | Error: Command failed: git --git-dir=/vercel/path0/.git --work-tree=/vercel/path0 rev-parse HEAD
10:11:16.087 | fatal: not a git repository: '/vercel/path0/.git'
10:11:16.088 | at ChildProcess.exithandler (child_process.js:308:12)
10:11:16.088 | at ChildProcess.emit (events.js:315:20)
10:11:16.088 | at maybeClose (internal/child_process.js:1048:16)
10:11:16.088 | at Socket.&lt;anonymous&gt; (internal/child_process.js:439:11)
10:11:16.088 | at Socket.emit (events.js:315:20)
10:11:16.088 | at Pipe.&lt;anonymous&gt; (net.js:673:12) {
10:11:16.088 | killed: false,
10:11:16.088 | code: 128,
10:11:16.088 | signal: null,
10:11:16.089 | cmd: 'git --git-dir=/vercel/path0/.git --work-tree=/vercel/path0 rev-parse HEAD',
10:11:16.089 | stdout: '',
10:11:16.089 | stderr: "fatal: not a git repository: '/vercel/path0/.git'\n"
10:11:16.089 | }
10:11:16.103 | error Command failed with exit code 1.
10:11:16.103 | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
10:11:16.123 | Error: Command "yarn run build" exited with 1

I suppose this is arising due to the SHA that is to be generated for the build-id by this module using the command git --git-dir=/vercel/path0/.git --work-tree=/vercel/path0 rev-parse HEAD which is triggering the build failure.

Solution: Remove this module from your next.config.js and vercel will build your next.js application just fine!

`nextBuildId.sync()` should have an option to return string version of git commit hash

When using with Next.JS and webpack, the function does not return a string version of the git commit hash, causing this error:

Screenshot at Sep 26 13-06-52

next.config.js:

module.exports = {
  webpack(config, options) {
    /**
    * Define build ID value `__next_build_id__` to be the current git commit hash,
    * which can be referenced anywhere in the bundle.
    */
    config.plugins.push(
      new webpack.DefinePlugin({
         __next_build_id__: nextBuildId.sync({ dir: __dirname }),
      }),
    );
    return config;
   },
};

distDir in next.config.js not being respected

next build src/ && next-build-id src/ --id gls

Gives the following error:

Output directory does not exist: ~/my-project/apps/lobby/src/.next

In next.config.js I have distDir: "../build",.

In README it says "If you have distDir defined in a next.config.js file, it will be respected. Otherwise, this module assumes the Next.js build output is in a relative .next directory."

It doesn't appear to be behaving as expected for me. What am I missing?

I'm on next.js 5 and cannot upgrade to version 6 due to all the babel versioning issues, as described here vercel/next.js#4691

Error: spawnSync git ENOENT

The following error occurs in Docker container when building production image:

#16 2.458 > Build error occurred
#16 2.461 <ref *1> Error: spawnSync git ENOENT
#16 2.461     at Object.spawnSync (internal/child_process.js:1067:20)
#16 2.461     at spawnSync (child_process.js:602:24)
#16 2.461     at Object.execFileSync (child_process.js:629:15)
#16 2.461     at gitSync (/build/node_modules/next-build-id/index.js:43:23)
#16 2.461     at AsyncFunction.nextBuildId.sync (/build/node_modules/next-build-id/index.js:170:8)
#16 2.461     at Object.<anonymous> (/build/next.config.js:9:32)
#16 2.461     at Module._compile (internal/modules/cjs/loader.js:1063:30)
#16 2.461     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
#16 2.461     at Module.load (internal/modules/cjs/loader.js:928:32)
#16 2.461     at Function.Module._load (internal/modules/cjs/loader.js:769:14)
#16 2.461     at Module.require (internal/modules/cjs/loader.js:952:19)
#16 2.461     at require (internal/modules/cjs/helpers.js:88:18)
#16 2.461     at loadConfig (/build/node_modules/next/dist/next-server/server/config.js:8:94)
#16 2.461     at /build/node_modules/next/dist/build/index.js:2:227
#16 2.461     at /build/node_modules/next/dist/build/tracer.js:3:360
#16 2.461     at NoopTracer.withSpan (/build/node_modules/@opentelemetry/api/build/src/trace/NoopTracer.js:47:16) {
#16 2.461   errno: -2,
#16 2.461   code: 'ENOENT',
#16 2.461   syscall: 'spawnSync git',
#16 2.461   path: 'git',
#16 2.461   spawnargs: [
#16 2.461     '--git-dir=/build/.git',
#16 2.461     '--work-tree=/build',
#16 2.461     'rev-parse',
#16 2.461     'HEAD'
#16 2.461   ],
#16 2.461   error: [Circular *1],
#16 2.461   status: null,
#16 2.461   signal: null,
#16 2.461   output: null,
#16 2.461   pid: 39,
#16 2.461   stdout: null,
#16 2.461   stderr: null
#16 2.461 }
#16 2.486 error Command failed with exit code 1.
#16 2.486 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Build Error Occurred

Firstly, I want to thank you immensely for creating this package. I am hoping to use it for a legit Sentry-NextJS integration by using this to set the release as the commit hash. However, I am having some problems getting this work in my latest build:

> Build error occurred
19:23:37.663  
Error: Command failed: git --git-dir=/vercel/32bafab9/.git --work-tree=/vercel/32bafab9 rev-parse HEAD
19:23:37.663  
fatal: not a git repository: '/vercel/32bafab9/.git'
19:23:37.663  
    at ChildProcess.exithandler (child_process.js:295:12)
19:23:37.663  
    at ChildProcess.emit (events.js:223:5)
19:23:37.663  
    at maybeClose (internal/child_process.js:1021:16)
19:23:37.663  
    at Socket.<anonymous> (internal/child_process.js:430:11)
19:23:37.663  
    at Socket.emit (events.js:223:5)
19:23:37.663  
    at Pipe.<anonymous> (net.js:664:12) 

Here is my next.config.js file: https://github.com/scefali/vercel-integration/blob/bac517b6701d382467e7a0be1e8c619d6cb285bf/next.config.js

Let me know if you need any more information.

EDIT: Found a workaround to this using the environment variables described here: https://vercel.com/docs/v2/build-step#system-environment-variables

Just used VERCEL_GITHUB_COMMIT_SHA

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.