mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): provide fallback values for undefined runtime config (#18586)
This commit is contained in:
parent
6c4cf5cdcb
commit
b8f6243621
@ -423,14 +423,17 @@ export default defineUntypedSchema({
|
|||||||
* @type {typeof import('../src/types/config').RuntimeConfig}
|
* @type {typeof import('../src/types/config').RuntimeConfig}
|
||||||
*/
|
*/
|
||||||
runtimeConfig: {
|
runtimeConfig: {
|
||||||
$resolve: async (val: RuntimeConfig, get) => defu(val, {
|
$resolve: async (val: RuntimeConfig, get) => {
|
||||||
public: {},
|
provideFallbackValues(val)
|
||||||
app: {
|
return defu(val, {
|
||||||
baseURL: (await get('app')).baseURL,
|
public: {},
|
||||||
buildAssetsDir: (await get('app')).buildAssetsDir,
|
app: {
|
||||||
cdnURL: (await get('app')).cdnURL,
|
baseURL: (await get('app')).baseURL,
|
||||||
}
|
buildAssetsDir: (await get('app')).buildAssetsDir,
|
||||||
})
|
cdnURL: (await get('app')).cdnURL,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -445,3 +448,13 @@ export default defineUntypedSchema({
|
|||||||
|
|
||||||
$schema: {}
|
$schema: {}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function provideFallbackValues (obj: Record<string, any>) {
|
||||||
|
for (const key in obj) {
|
||||||
|
if (typeof obj[key] === 'undefined' || obj[key] === null) {
|
||||||
|
obj[key] = ''
|
||||||
|
} else if (typeof obj[key] === 'object') {
|
||||||
|
provideFallbackValues(obj[key])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -52,6 +52,7 @@ describe('pages', () => {
|
|||||||
expect(html).toContain('Hello Nuxt 3!')
|
expect(html).toContain('Hello Nuxt 3!')
|
||||||
// should inject runtime config
|
// should inject runtime config
|
||||||
expect(html).toContain('RuntimeConfig | testConfig: 123')
|
expect(html).toContain('RuntimeConfig | testConfig: 123')
|
||||||
|
expect(html).toContain('needsFallback:')
|
||||||
// composables auto import
|
// composables auto import
|
||||||
expect(html).toContain('Composable | foo: auto imported from ~/components/foo.ts')
|
expect(html).toContain('Composable | foo: auto imported from ~/components/foo.ts')
|
||||||
expect(html).toContain('Composable | bar: auto imported from ~/components/useBar.ts')
|
expect(html).toContain('Composable | bar: auto imported from ~/components/useBar.ts')
|
||||||
|
1
test/fixtures/basic/nuxt.config.ts
vendored
1
test/fixtures/basic/nuxt.config.ts
vendored
@ -51,6 +51,7 @@ export default defineNuxtConfig({
|
|||||||
runtimeConfig: {
|
runtimeConfig: {
|
||||||
privateConfig: 'secret_key',
|
privateConfig: 'secret_key',
|
||||||
public: {
|
public: {
|
||||||
|
needsFallback: undefined,
|
||||||
testConfig: 123
|
testConfig: 123
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user