Skip to content

Commit f2255d9

Browse files
committed
Fix static export by excluding API routes during build
1 parent aff1450 commit f2255d9

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

build-static.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { execSync } = require("child_process");
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
console.log("🚀 Building static export...");
6+
7+
// Temporarily rename API routes to exclude them from build
8+
const apiDir = path.join(__dirname, "src", "app", "api");
9+
const tempApiDir = path.join(__dirname, "src", "app", "_api_temp");
10+
11+
try {
12+
// Move API directory temporarily
13+
if (fs.existsSync(apiDir)) {
14+
fs.renameSync(apiDir, tempApiDir);
15+
console.log("✅ Temporarily moved API routes");
16+
}
17+
18+
// Run the build
19+
console.log("🔨 Running Next.js build...");
20+
execSync("npm run build", { stdio: "inherit" });
21+
22+
// Move API directory back
23+
if (fs.existsSync(tempApiDir)) {
24+
fs.renameSync(tempApiDir, apiDir);
25+
console.log("✅ Restored API routes");
26+
}
27+
28+
console.log("🎉 Static build completed successfully!");
29+
} catch (error) {
30+
// Restore API directory if build failed
31+
if (fs.existsSync(tempApiDir)) {
32+
fs.renameSync(tempApiDir, apiDir);
33+
console.log("✅ Restored API routes after build failure");
34+
}
35+
console.error("❌ Build failed:", error.message);
36+
process.exit(1);
37+
}

next.config.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ const nextConfig = {
88
basePath: "/acm",
99
assetPrefix: "/acm/",
1010

11-
// Memory optimization for cPanel build
12-
experimental: {
13-
// Reduce memory usage during build
14-
memoryBasedWorkers: false,
15-
},
16-
11+
// Exclude API routes from static export
12+
trailingSlash: true,
13+
1714
// Webpack optimization for memory
1815
webpack: (config, { isServer }) => {
1916
if (!isServer) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
7-
"build": "next build",
7+
"build": "node build-static.js",
8+
"build:next": "next build",
89
"start": "node server.js",
910
"lint": "next lint"
1011
},

0 commit comments

Comments
 (0)