From 6576bc81be2b393e2d046695868594e595021d06 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 23 Sep 2022 21:40:46 +0100 Subject: [PATCH] fix: apply runtimeConfig proxy directly to ctx.config --- src/app.ts | 3 +++ src/runtime/composables.ts | 18 +----------------- src/runtime/config.plugin.mjs | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 src/runtime/config.plugin.mjs diff --git a/src/app.ts b/src/app.ts index 00955b011..fc8d2e6b9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -101,6 +101,9 @@ export async function setupAppBridge (_options: any) { } }) + // Normalize runtimeConfig with a proxy + addPlugin({ src: resolve(distDir, 'runtime/config.plugin.mjs') }) + addPlugin({ src: resolve(distDir, 'runtime/error.plugin.server.mjs'), mode: 'server' diff --git a/src/runtime/composables.ts b/src/runtime/composables.ts index a089993e9..428b57cdb 100644 --- a/src/runtime/composables.ts +++ b/src/runtime/composables.ts @@ -27,24 +27,8 @@ export const useRuntimeConfig = () => { if (nuxtApp._config) { return nuxtApp._config as RuntimeConfig } - const config = reactive(nuxtApp.$config) - nuxtApp._config = new Proxy(config, { - get (target, prop) { - if (prop === 'public') { - return target.public - } - return target[prop] ?? target.public[prop] - }, - set (target, prop, value) { - if (prop === 'public' || prop === 'app') { - return false // Throws TypeError - } - target[prop] = value - target.public[prop] = value - return true - } - }) + nuxtApp._config = reactive(nuxtApp.$config) return nuxtApp._config as RuntimeConfig } diff --git a/src/runtime/config.plugin.mjs b/src/runtime/config.plugin.mjs new file mode 100644 index 000000000..e921266a6 --- /dev/null +++ b/src/runtime/config.plugin.mjs @@ -0,0 +1,21 @@ +export default (ctx) => { + const config = ctx.$config + ctx.$config = new Proxy(config, { + get (target, prop) { + if (prop === 'public') { + return target.public + } + return target[prop] ?? target.public?.[prop] + }, + set (target, prop, value) { + if (prop === 'public' || prop === 'app') { + return false // Throws TypeError + } + target[prop] = value + if ('public' in target) { + target.public[prop] = value + } + return true + } + }) +}