feat: add codeserver dev profile

NEXT_DEV_PROFILE=codeserver wires basePath, assetPrefix, and
allowedDevOrigins from CODESERVER_HOST/PORT in .env.local. Use
/absproxy/{port} so code-server preserves the path prefix and Next's
basePath matches the incoming request.

Run with `npm run dev:codeserver` and access via
https://<host>/absproxy/<port>/.
This commit is contained in:
2026-04-26 19:16:46 +07:00
parent bca0e6866d
commit 0844027460
5 changed files with 30 additions and 2 deletions
+2
View File
@@ -0,0 +1,2 @@
CODESERVER_HOST=your-codeserver.example.com
CODESERVER_PORT=3000
+3
View File
@@ -2,3 +2,6 @@
node_modules/
.next/
out/
.env.local
.env*.local
tsconfig.tsbuildinfo
+1 -1
View File
@@ -1,6 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
import "./.next/types/routes.d.ts";
import "./.next/dev/types/routes.d.ts";
// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
+23 -1
View File
@@ -1,10 +1,32 @@
import type { NextConfig } from "next";
const isProd = process.env.NODE_ENV === "production";
const isCodeserver = process.env.NEXT_DEV_PROFILE === "codeserver";
// In dev under code-server's reverse proxy, basePath/assetPrefix must match
// the proxy URL so links, assets, and the HMR socket all resolve.
// Use /absproxy/{port} (NOT /proxy/{port}) — /proxy strips the prefix before
// forwarding; /absproxy preserves it so basePath matches the incoming request.
// CODESERVER_HOST and CODESERVER_PORT are loaded from .env.local by Next.
function codeserverConfig() {
const host = process.env.CODESERVER_HOST;
if (!host) {
throw new Error(
"CODESERVER_HOST is required when NEXT_DEV_PROFILE=codeserver (set it in .env.local)"
);
}
const port = process.env.CODESERVER_PORT ?? "3000";
return { basePath: `/absproxy/${port}`, allowedDevOrigins: [host] };
}
const cs = isCodeserver ? codeserverConfig() : null;
const basePath = cs?.basePath ?? (isProd ? "/loto" : "");
const nextConfig: NextConfig = {
output: "export",
basePath: isProd ? "/loto" : "",
basePath,
assetPrefix: basePath || undefined,
...(cs?.allowedDevOrigins ? { allowedDevOrigins: cs.allowedDevOrigins } : {}),
};
export default nextConfig;
+1
View File
@@ -4,6 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"dev:codeserver": "NEXT_DEV_PROFILE=codeserver next dev -H 0.0.0.0",
"build": "next build",
"start": "next start",
"lint": "eslint"