diff --git a/src/build/virtual/app.ts b/src/build/virtual/app.ts index a1500ab6c1..55578eda3d 100644 --- a/src/build/virtual/app.ts +++ b/src/build/virtual/app.ts @@ -11,6 +11,7 @@ export default function app(nitro: Nitro) { const hasPlugins = nitro.options.plugins.length > 0; const hasHooks = nitro.options.features?.runtimeHooks ?? hasPlugins; const hasAsyncContext = !!nitro.options.experimental.asyncContext; + const runtimeBaseUrl = nitro.options.features.runtimeBaseURL; const routingImports = [ hasRoutes && "findRoute", @@ -90,6 +91,18 @@ export default function app(nitro: Nitro) { ` };` ); + if (runtimeBaseUrl) { + imports.push(`import { requestWithBaseURL } from "h3";`); + code.push( + ``, + ` const baseUrl = process.env.NITRO_APP_BASE_URL ?? "";`, + ` const baseHandler = appHandler;`, + ` appHandler = (req) => {`, + ` return baseHandler(requestWithBaseURL(req, baseUrl));`, + ` };` + ); + } + if (hasAsyncContext) { imports.push(`import { nitroAsyncContext } from "#nitro/runtime/context";`); code.push( diff --git a/src/types/config.ts b/src/types/config.ts index f7cb88d810..83ba1443ce 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -314,6 +314,11 @@ export interface NitroOptions extends PresetOptions { * Enable WebSocket support. */ websocket?: boolean; + + /** + * Enable NITRO_APP_BASE_URL env var to take effect at runtime + */ + runtimeBaseURL?: boolean; }; /** diff --git a/src/types/global.ts b/src/types/global.ts index 3af8907827..dc8d4fd2cf 100644 --- a/src/types/global.ts +++ b/src/types/global.ts @@ -12,6 +12,7 @@ export interface NitroImportMeta { _asyncContext?: boolean; _tasks?: boolean; _websocket?: boolean; + _runtimeBaseURL?: boolean; } declare global {