fix: make static export work under php base path

This commit is contained in:
overtrue
2026-02-09 16:03:06 +08:00
parent 4fc4583969
commit 18f9b48890
3 changed files with 45 additions and 1 deletions
+1
View File
@@ -3,6 +3,7 @@ import type { NextConfig } from "next"
const nextConfig: NextConfig = {
output: "export",
basePath: process.env.NEXT_PUBLIC_BASE_PATH ?? "/rustfs/console",
trailingSlash: true,
images: {
unoptimized: true,
},
+1 -1
View File
@@ -5,7 +5,7 @@
"packageManager": "pnpm@10.19.0",
"scripts": {
"dev": "next dev",
"build": "next build --webpack",
"build": "next build --webpack && node scripts/prepare-static-basepath.mjs",
"start": "next start",
"lint": "eslint",
"lint:fix": "eslint --fix",
+43
View File
@@ -0,0 +1,43 @@
import fs from "node:fs"
import path from "node:path"
const OUT_DIR = path.resolve(process.cwd(), "out")
const basePathRaw = process.env.NEXT_PUBLIC_BASE_PATH ?? "/rustfs/console"
function normalizeBasePath(input) {
const value = String(input || "").trim()
if (!value || value === "/") return ""
return value.replace(/^\/+/, "").replace(/\/+$/, "")
}
function main() {
const basePath = normalizeBasePath(basePathRaw)
if (!basePath) {
console.log("[prepare-static-basepath] skipped: empty basePath")
return
}
if (!fs.existsSync(OUT_DIR)) {
console.log("[prepare-static-basepath] skipped: out directory not found")
return
}
const targetDir = path.join(OUT_DIR, basePath)
const firstSegment = basePath.split("/")[0]
fs.rmSync(targetDir, { recursive: true, force: true })
fs.mkdirSync(targetDir, { recursive: true })
for (const entry of fs.readdirSync(OUT_DIR)) {
if (entry === firstSegment) continue
fs.cpSync(path.join(OUT_DIR, entry), path.join(targetDir, entry), {
recursive: true,
force: true,
errorOnExist: false,
})
}
console.log(`[prepare-static-basepath] prepared ${targetDir}`)
}
main()