integrated next-build-id library

This commit is contained in:
corgifist 2025-07-28 20:41:57 +03:00
parent ad39e0d557
commit 10ad436c92
5 changed files with 26 additions and 18 deletions

View File

@ -1,26 +1,17 @@
import type { NextConfig } from "next";
import build from "next/dist/build";
import { describe } from "node:test";
const nextBuildId = require('next-build-id');
const generateGitCommitHash = () => {
if (process.env.NODE_ENV === "development") return "development server";
if (!process.env.GIT_COMMIT) return "git commit hash is unavailable";
const hash = process.env.GIT_COMMIT;
if (!hash || hash.trim().length == 0) return "empty git commit hash";
return hash;
}
const gitCommitHash = generateGitCommitHash();
const buildId = nextBuildId({ dir: __dirname, describe: true })
const nextConfig: NextConfig = {
output: "standalone",
generateBuildId: () => gitCommitHash,
webpack: (config, { webpack, buildId, isServer }) => {
config.plugins.push(
new webpack.DefinePlugin({
'process.env.BUILD_ID_ENV': JSON.stringify(buildId)
})
);
return config;
generateBuildId: () => buildId,
env: {
BUILD_ID: buildId
}
};
export default nextConfig;

10
package-lock.json generated
View File

@ -29,6 +29,7 @@
"dexie-react-hooks": "^1.1.7",
"lucide-react": "^0.525.0",
"next": "^15.4.3",
"next-build-id": "^3.0.0",
"next-themes": "^0.4.6",
"react": "19.1.0",
"react-dom": "19.1.0",
@ -5743,6 +5744,15 @@
}
}
},
"node_modules/next-build-id": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/next-build-id/-/next-build-id-3.0.0.tgz",
"integrity": "sha512-B3JCsL/9Z/wkmo3EySukQHCgx89Aw0i4LPi2MEhCboQBJ6wpkYTIu1z6hOYKuw/S1Wy8ZRqCEq0dVY/ST6jGqg==",
"license": "ISC",
"engines": {
"node": ">=8"
}
},
"node_modules/next-themes": {
"version": "0.4.6",
"resolved": "https://registry.npmjs.org/next-themes/-/next-themes-0.4.6.tgz",

View File

@ -30,6 +30,7 @@
"dexie-react-hooks": "^1.1.7",
"lucide-react": "^0.525.0",
"next": "^15.4.3",
"next-build-id": "^3.0.0",
"next-themes": "^0.4.6",
"react": "19.1.0",
"react-dom": "19.1.0",

View File

@ -9,6 +9,7 @@ import { useIsMobile } from "@/hooks/use-mobile";
import AscendingCard from "@/components/ascending-card";
import Link from "next/link";
import WideContainer from "@/components/wide-container";
import getBuildID from "@/lib/build";
export default function Settings(): ReactNode {
@ -46,7 +47,7 @@ export default function Settings(): ReactNode {
<ChevronRightIcon />
</AscendingCard>
</Link>
<p className="text-sm text-muted-foreground">Build ID: {process.env.BUILD_ID_ENV}</p>
<p className="text-sm text-muted-foreground">Build ID: {getBuildID()}</p>
</div>
</WideContainer>
</div>

5
src/lib/build.ts Normal file
View File

@ -0,0 +1,5 @@
const getBuildID = () => (
process.env.NODE_ENV == "development" ? "development build" : process.env.BUILD_ID
);
export default getBuildID;