refactor: enhance build ID generation and API error handling in configuration

This commit is contained in:
Marco Pedone 2025-11-25 12:04:53 +01:00
parent 94edc34e10
commit e9c26d8b42
2 changed files with 38 additions and 4 deletions

View file

@ -11,12 +11,26 @@ const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true", enabled: process.env.ANALYZE === "true",
}); });
function getBuildId(): string {
try {
const { execSync } = require("node:child_process");
// Get short hash (8 characters) instead of full hash
return execSync("git rev-parse --short=8 HEAD").toString().trim();
} catch {
// Fallback to timestamp-based ID
return `${Date.now().toString(36)}`;
}
}
async function createNextConfig(): Promise<NextConfig> { async function createNextConfig(): Promise<NextConfig> {
const { createJiti } = await import("jiti"); const { createJiti } = await import("jiti");
const jiti = createJiti(fileURLToPath(import.meta.url)); const jiti = createJiti(fileURLToPath(import.meta.url));
await jiti.import("./src/env.ts"); await jiti.import("./src/env.ts");
const buildId = getBuildId();
const apiVersion = `v.${buildId}`;
return withBundleAnalyzer({ return withBundleAnalyzer({
compiler: { compiler: {
removeConsole: false, removeConsole: false,
@ -24,18 +38,35 @@ async function createNextConfig(): Promise<NextConfig> {
experimental: { experimental: {
// esmExternals: "loose", // This if react-pdf gives compilation issues // esmExternals: "loose", // This if react-pdf gives compilation issues
}, },
generateBuildId: async () => buildId,
devIndicators: false, devIndicators: false,
headers: async () => { headers: async () => {
return [ return [
{ {
source: "/:path*",
headers: [ headers: [
{ {
key: "X-Frame-Options", key: "X-Frame-Options",
value: "", value: "",
}, },
], ],
source: "/:path*", },
{
source: "/api/trpc/:path*",
headers: [
{
key: "Cache-Control",
value: "no-cache, no-store, must-revalidate",
},
{
key: "X-API-Version",
value: apiVersion,
},
{
key: "X-Build-ID",
value: buildId,
},
],
}, },
]; ];
}, },

View file

@ -28,8 +28,11 @@ const errorLink: TRPCLink<AppRouter> = () => {
complete: observer.complete, complete: observer.complete,
error(err) { error(err) {
if ( if (
err instanceof TRPCClientError && (err instanceof TRPCClientError &&
err.data?.code === "UNAUTHORIZED" err.data?.code === "UNAUTHORIZED") ||
err.data?.code === "FORBIDDEN" ||
err.data?.code === "CONFLICT" ||
err.data?.code === "NOT_FOUND"
) { ) {
window.location.reload(); window.location.reload();
} }