diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 62779112d1..cdfe7aab61 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -255,17 +255,19 @@ export function createNuxtApp (options: CreateOptions) { } // Expose runtime config - const runtimeConfig = process.server - ? options.ssrContext!.runtimeConfig - : reactive(nuxtApp.payload.config) + const runtimeConfig = process.server ? options.ssrContext!.runtimeConfig : reactive(nuxtApp.payload.config) + // TODO: remove in v3.5 // Backward compatibility following #4254 const compatibilityConfig = new Proxy(runtimeConfig, { - get (target, prop) { - if (prop === 'public') { - return target.public + get (target, prop: string) { + if (prop in target) { + return target[prop] } - return target[prop] ?? target.public[prop] + if (process.dev && prop in target.public) { + console.warn(`[nuxt] [runtimeConfig] You are trying to access a public runtime config value (\`${prop}\`) directly from the top level. This currently works (for backward compatibility with Nuxt 2) but this compatibility layer will be removed in v3.5. Instead, you can update \`config['${prop}']\` to \`config.public['${prop}']\`.`) + } + return target.public[prop] }, set (target, prop, value) { if (process.server || prop === 'public' || prop === 'app') { diff --git a/test/fixtures/basic/pages/index.vue b/test/fixtures/basic/pages/index.vue index 8661c1760f..1db9072306 100644 --- a/test/fixtures/basic/pages/index.vue +++ b/test/fixtures/basic/pages/index.vue @@ -4,7 +4,7 @@ Basic fixture

Hello Nuxt 3!

-
RuntimeConfig | testConfig: {{ config.testConfig }}
+
RuntimeConfig | testConfig: {{ config.public.testConfig }}
Composable | foo: {{ foo }}
Composable | bar: {{ bar }}
Composable | template: {{ templateAutoImport }}
diff --git a/test/fixtures/basic/plugins/async-plugin.ts b/test/fixtures/basic/plugins/async-plugin.ts index 09289c778b..05924f6336 100644 --- a/test/fixtures/basic/plugins/async-plugin.ts +++ b/test/fixtures/basic/plugins/async-plugin.ts @@ -6,7 +6,7 @@ export default defineNuxtPlugin(async (/* nuxtApp */) => { return { provide: { asyncPlugin: () => config1 && config1 === config2 - ? 'Async plugin works! ' + config1.testConfig + (data.value?.baz ? 'useFetch works!' : 'useFetch does not work') + ? 'Async plugin works! ' + config1.public.testConfig + (data.value?.baz ? 'useFetch works!' : 'useFetch does not work') : 'Async plugin failed!' } }