refactor: enhance build ID generation and API error handling in configuration
This commit is contained in:
parent
94edc34e10
commit
e9c26d8b42
2 changed files with 38 additions and 4 deletions
|
|
@ -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<NextConfig> {
|
||||
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<NextConfig> {
|
|||
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,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
},
|
||||
|
|
|
|||
|
|
@ -28,8 +28,11 @@ const errorLink: TRPCLink<AppRouter> = () => {
|
|||
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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue