fix(bridge, nuxt3): backward compatibility to access runtime config in client (#4283)

This commit is contained in:
pooya parsa 2022-04-12 11:04:26 +02:00 committed by GitHub
parent 2dadd4f0b3
commit 81bf1c065e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -25,7 +25,25 @@ export const useHydration = mock()
export const useRuntimeConfig = () => { export const useRuntimeConfig = () => {
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()
if (!nuxtApp.$config) { if (!nuxtApp.$config) {
nuxtApp.$config = reactive(nuxtApp.nuxt2Context.app.$config) const runtimeConfig = reactive(nuxtApp.nuxt2Context.app.$config)
const copatibilityConfig = new Proxy(runtimeConfig, {
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.provide('config', copatibilityConfig)
nuxtApp.$config = copatibilityConfig
} }
return nuxtApp.$config as RuntimeConfig return nuxtApp.$config as RuntimeConfig
} }

View File

@ -127,7 +127,24 @@ export function createNuxtApp (options: CreateOptions) {
app: options.ssrContext.runtimeConfig.app app: options.ssrContext.runtimeConfig.app
} }
} else { } else {
nuxtApp.provide('config', reactive(nuxtApp.payload.config)) const runtimeConfig = reactive(nuxtApp.payload.config)
const copatibilityConfig = new Proxy(runtimeConfig, {
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.provide('config', copatibilityConfig)
} }
return nuxtApp return nuxtApp