From e9c26d8b42b685a1c8e8e91ab83c32880b9908fb Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Tue, 25 Nov 2025 12:04:53 +0100 Subject: [PATCH] refactor: enhance build ID generation and API error handling in configuration --- apps/infoalloggi/next.config.ts | 35 +++++++++++++++++++++++++++++-- apps/infoalloggi/src/utils/api.ts | 7 +++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/apps/infoalloggi/next.config.ts b/apps/infoalloggi/next.config.ts index 9b70475..b3e3d94 100644 --- a/apps/infoalloggi/next.config.ts +++ b/apps/infoalloggi/next.config.ts @@ -11,12 +11,26 @@ const withBundleAnalyzer = require("@next/bundle-analyzer")({ 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 { const { createJiti } = await import("jiti"); const jiti = createJiti(fileURLToPath(import.meta.url)); await jiti.import("./src/env.ts"); + const buildId = getBuildId(); + const apiVersion = `v.${buildId}`; + return withBundleAnalyzer({ compiler: { removeConsole: false, @@ -24,18 +38,35 @@ async function createNextConfig(): Promise { experimental: { // esmExternals: "loose", // This if react-pdf gives compilation issues }, - + generateBuildId: async () => buildId, devIndicators: false, headers: async () => { return [ { + source: "/:path*", headers: [ { key: "X-Frame-Options", 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, + }, + ], }, ]; }, diff --git a/apps/infoalloggi/src/utils/api.ts b/apps/infoalloggi/src/utils/api.ts index ce0fcdf..ed8ca93 100644 --- a/apps/infoalloggi/src/utils/api.ts +++ b/apps/infoalloggi/src/utils/api.ts @@ -28,8 +28,11 @@ const errorLink: TRPCLink = () => { complete: observer.complete, error(err) { if ( - err instanceof TRPCClientError && - err.data?.code === "UNAUTHORIZED" + (err instanceof TRPCClientError && + err.data?.code === "UNAUTHORIZED") || + err.data?.code === "FORBIDDEN" || + err.data?.code === "CONFLICT" || + err.data?.code === "NOT_FOUND" ) { window.location.reload(); }