Optimize Next.js memory and handle 404s

Configure startup and package-import optimizations, exclude generated Next.js files from TypeScript checks, and route fetch responses with status 404 to the not-found page.
This commit is contained in:
Ichsan Kurniawan
2026-11-18 18:20:27 +07:00
parent d4168b45f2
commit d10603acc8
4 changed files with 11 additions and 4 deletions
+3 -1
View File
@@ -1,4 +1,4 @@
import { redirect } from "next/navigation" import { notFound, redirect } from "next/navigation"
import { ResponseData } from "@/types/types" import { ResponseData } from "@/types/types"
import { logger } from "./logger" import { logger } from "./logger"
import { getCacheServerSession } from "./get-access-data" import { getCacheServerSession } from "./get-access-data"
@@ -24,6 +24,7 @@ export default async function fetchData(url: string = '/', cache: "default" | "f
console.log("FETCH ERROR DATA :", res.status, data) console.log("FETCH ERROR DATA :", res.status, data)
if (res.status === 401 || res.status === 403) return redirect(`/auth?error=Session error or has expired !`) if (res.status === 401 || res.status === 403) return redirect(`/auth?error=Session error or has expired !`)
if (res.status === 404) notFound()
// This will activate the closest `error.js` Error Boundary // This will activate the closest `error.js` Error Boundary
throw new Error(`(${data.code || res.status}) ${data.message || res.statusText}`) throw new Error(`(${data.code || res.status}) ${data.message || res.statusText}`)
} }
@@ -62,6 +63,7 @@ export async function fetchDataWithPost(url: string = '/', payload: any = {}, ca
console.log("FETCH ERROR DATA :", res.status, data) console.log("FETCH ERROR DATA :", res.status, data)
if (res.status === 401 || res.status === 403) redirect(`/auth?error=Session error or has expired !`); if (res.status === 401 || res.status === 403) redirect(`/auth?error=Session error or has expired !`);
if (res.status === 404) notFound()
throw new Error(`(${data?.code || res.status} ${res.statusText}) ${data?.message}`); throw new Error(`(${data?.code || res.status} ${res.statusText}) ${data?.message}`);
} }
+5
View File
@@ -27,6 +27,11 @@ const nextConfig: NextConfig = {
reactStrictMode: false, reactStrictMode: false,
experimental: { experimental: {
authInterrupts: true, // for forbidden page authInterrupts: true, // for forbidden page
// MEMORY OPTIMIZING
// 1. Prevent Next.js from loading all route modules into RAM at startup
preloadEntriesOnStart: false,
// 2. Optimize barrel imports (e.g., lucide-react, @mui/icons-material, lodash)
optimizePackageImports: ['lucide-react', 'date-fns', 'lodash'],
}, },
}; };
+2 -2
View File
@@ -12,7 +12,7 @@
"url": "https://github.com/ichsankurnia/hats-app/issues" "url": "https://github.com/ichsankurnia/hats-app/issues"
}, },
"scripts": { "scripts": {
"dev": "next dev -p 1209", "dev": "set NODE_OPTIONS=\"--max-old-space-size=2048\" && next dev -p 1209",
"dev:save": "set NODE_TLS_REJECT_UNAUTHORIZED='0' && next dev -p 1209", "dev:save": "set NODE_TLS_REJECT_UNAUTHORIZED='0' && next dev -p 1209",
"build": "next build", "build": "next build",
"start": "next start -p 7002", "start": "next start -p 7002",
@@ -95,4 +95,4 @@
"tw-animate-css": "^1.4.0", "tw-animate-css": "^1.4.0",
"typescript": "^5" "typescript": "^5"
} }
} }
+1 -1
View File
@@ -30,5 +30,5 @@
".next/dev/types/**/*.ts", ".next/dev/types/**/*.ts",
"**/*.mts" "**/*.mts"
], ],
"exclude": ["node_modules", "**/*.old.tsx", "**/*.old.ts"] "exclude": ["node_modules", ".next", "**/*.old.tsx", "**/*.old.ts"]
} }