diff --git a/src/runtime/config.ts b/src/runtime/config.ts index 84cef7ca7d..25814cf858 100644 --- a/src/runtime/config.ts +++ b/src/runtime/config.ts @@ -12,13 +12,12 @@ const ENV_PREFIX_ALT = _inlineRuntimeConfig.nitro.envPrefix ?? process.env.NITRO_ENV_PREFIX ?? "_"; // Runtime config -let appliedRuntimeConfig = {}; const _sharedRuntimeConfig = _deepFreeze( _applyEnv(klona(_inlineRuntimeConfig)) ); const getRuntimeConfig = () => ({ ..._sharedRuntimeConfig, - ...appliedRuntimeConfig, + ...envToRuntimeObject(globalThis.__env__ ?? {}), }); export function useRuntimeConfig< @@ -39,8 +38,8 @@ export function useRuntimeConfig< return runtimeConfig; } -export function applyEnvToRuntimeConfig( - env: Record, +export function envToRuntimeObject( + env: Record, prefixes = [ENV_PREFIX, "NUXT_", ENV_PREFIX_ALT] ) { const safeEnv = Object.fromEntries( @@ -50,8 +49,7 @@ export function applyEnvToRuntimeConfig( ) .map(([key, value]) => [formatKey(key), value]) ); - appliedRuntimeConfig = safeEnv; - return useRuntimeConfig(); + return safeEnv; } const formatKey = (key: string) => diff --git a/src/runtime/entries/cloudflare-module.ts b/src/runtime/entries/cloudflare-module.ts index 13f7d70293..f8393840e3 100644 --- a/src/runtime/entries/cloudflare-module.ts +++ b/src/runtime/entries/cloudflare-module.ts @@ -8,7 +8,6 @@ import { // @ts-ignore Bundled by Wrangler // See https://github.com/cloudflare/kv-asset-handler#asset_manifest-required-for-es-modules import manifest from "__STATIC_CONTENT_MANIFEST"; -import { applyEnvToRuntimeConfig } from "../config"; import { requestHasBody } from "../utils"; import { nitroApp } from "#internal/nitro/app"; import { useRuntimeConfig } from "#internal/nitro"; @@ -52,7 +51,6 @@ export default { // Expose latest env to the global context globalThis.__env__ = env; - applyEnvToRuntimeConfig(env); return nitroApp.localFetch(url.pathname + url.search, { context: { cf: (request as any).cf, diff --git a/src/runtime/entries/cloudflare-pages.ts b/src/runtime/entries/cloudflare-pages.ts index 7a82ad941f..4efac76702 100644 --- a/src/runtime/entries/cloudflare-pages.ts +++ b/src/runtime/entries/cloudflare-pages.ts @@ -3,7 +3,6 @@ import type { Request as CFRequest, EventContext, } from "@cloudflare/workers-types"; -import { applyEnvToRuntimeConfig } from "../config"; import { requestHasBody } from "#internal/nitro/utils"; import { nitroApp } from "#internal/nitro/app"; import { isPublicAssetURL } from "#internal/nitro/virtual/public-assets"; @@ -39,7 +38,6 @@ export default { // Expose latest env to the global context globalThis.__env__ = env; - applyEnvToRuntimeConfig(env); return nitroApp.localFetch(url.pathname + url.search, { context: { cf: request.cf, diff --git a/test/tests.ts b/test/tests.ts index 3cc31f45c3..8dd526df79 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -396,6 +396,8 @@ export function testNitro( const { data } = await callHandler({ url: "/config", }); + console.log(data.sharedRuntimeConfig); + console.log(data.runtimeConfig); expect(data.runtimeConfig.hello).toBe("world"); expect(data.runtimeConfig.helloThere).toBe("general"); expect(data.runtimeConfig.secret).toBeUndefined();