From 0844027460f3aa867965b58ca90367c5c4bf5b60 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Sun, 26 Apr 2026 19:16:46 +0700 Subject: [PATCH] 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:///absproxy//. --- .env.example | 2 ++ .gitignore | 3 +++ next-env.d.ts | 2 +- next.config.ts | 24 +++++++++++++++++++++++- package.json | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..04480ff --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +CODESERVER_HOST=your-codeserver.example.com +CODESERVER_PORT=3000 diff --git a/.gitignore b/.gitignore index 4f076b5..17e8407 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ node_modules/ .next/ out/ +.env.local +.env*.local +tsconfig.tsbuildinfo diff --git a/next-env.d.ts b/next-env.d.ts index 9edff1c..c4b7818 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -1,6 +1,6 @@ /// /// -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. diff --git a/next.config.ts b/next.config.ts index 1040bca..2462dd4 100644 --- a/next.config.ts +++ b/next.config.ts @@ -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; diff --git a/package.json b/package.json index 92b9a4b..65e8ec7 100644 --- a/package.json +++ b/package.json @@ -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"