mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 13:48:13 +00:00
fix(nuxt3): as backward compatible runtime config for server side (#4295)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
parent
e31c604ac4
commit
ce36e2e211
@ -26,7 +26,7 @@ export const useRuntimeConfig = () => {
|
||||
const nuxtApp = useNuxtApp()
|
||||
if (!nuxtApp.$config) {
|
||||
const runtimeConfig = reactive(nuxtApp.nuxt2Context.app.$config)
|
||||
const copatibilityConfig = new Proxy(runtimeConfig, {
|
||||
const compatibilityConfig = new Proxy(runtimeConfig, {
|
||||
get (target, prop) {
|
||||
if (prop === 'public') {
|
||||
return target.public
|
||||
@ -42,8 +42,8 @@ export const useRuntimeConfig = () => {
|
||||
return true
|
||||
}
|
||||
})
|
||||
nuxtApp.provide('config', copatibilityConfig)
|
||||
nuxtApp.$config = copatibilityConfig
|
||||
nuxtApp.provide('config', compatibilityConfig)
|
||||
nuxtApp.$config = compatibilityConfig
|
||||
}
|
||||
return nuxtApp.$config as RuntimeConfig
|
||||
}
|
||||
|
@ -118,35 +118,39 @@ export function createNuxtApp (options: CreateOptions) {
|
||||
nuxtApp.ssrContext.payload = nuxtApp.payload
|
||||
}
|
||||
|
||||
// Expose runtime config
|
||||
// Expose client runtime-config to the payload
|
||||
if (process.server) {
|
||||
nuxtApp.provide('config', options.ssrContext.runtimeConfig)
|
||||
// Client's runtime-config
|
||||
nuxtApp.payload.config = {
|
||||
public: options.ssrContext.runtimeConfig.public,
|
||||
app: options.ssrContext.runtimeConfig.app
|
||||
}
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
|
||||
// Expose runtime config
|
||||
const runtimeConfig = process.server
|
||||
? options.ssrContext.runtimeConfig
|
||||
: reactive(nuxtApp.payload.config)
|
||||
|
||||
// Backward compatibilty following #4254
|
||||
const compatibilityConfig = new Proxy(runtimeConfig, {
|
||||
get (target, prop) {
|
||||
if (prop === 'public') {
|
||||
return target.public
|
||||
}
|
||||
return target[prop] ?? target.public[prop]
|
||||
},
|
||||
set (target, prop, value) {
|
||||
if (process.server || prop === 'public' || prop === 'app') {
|
||||
return false // Throws TypeError
|
||||
}
|
||||
target[prop] = value
|
||||
target.public[prop] = value
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
nuxtApp.provide('config', compatibilityConfig)
|
||||
|
||||
return nuxtApp
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user