How to read NextJS's build ID?
P粉904450959
P粉904450959 2024-02-03 16:07:29
0
1
558

Using NextJS, you can create a build ID according to the documentation: https://nextjs.org/docs/app/api-reference/next-config-js/generateBuildId

It can also be obtained according to the latest git version: https://www.npmjs.com/package/next-build-id

But the question is: how do I read this build ID? I want to display it inside the window console.

P粉904450959
P粉904450959

reply all(1)
P粉186897465

I haven't tested it, but I think you could try the old way of passing the env variable via the env key in the configuration (I guess it's deprecated now, but it should still work) like this:

const nextBuildId = require('next-build-id')

const buildId = nextBuildId({ dir: __dirname })

const nextConfig = {
  generateBuildId: () => buildId 
  env: {
    BUILD_ID_ENV: buildId
  },
};

Will be provided later as process.env.BUILD_ID_ENV

To use it in a client component you may have to name it NEXT_PUBLIC_BUILD_ID_ENV, not sure.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template