feat(nuxt): support app config for server routes (#19489)

This commit is contained in:
pooya parsa 2023-03-14 10:54:59 +01:00 committed by GitHub
parent 22cf86edf0
commit e84ec61eeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 1 deletions

View File

@ -38,7 +38,8 @@ export default defineAppConfig({
When adding `theme` to the `app.config`, Nuxt uses Vite or webpack to bundle the code. We can universally access `theme` both when server-rendering the page and in the browser using [useAppConfig](/docs/api/composables/use-app-config) composable. When adding `theme` to the `app.config`, Nuxt uses Vite or webpack to bundle the code. We can universally access `theme` both when server-rendering the page and in the browser using [useAppConfig](/docs/api/composables/use-app-config) composable.
::alert{type=info} ::alert{type=info}
Support for accessing `useAppConfig()` in Nitro and `server` directory is [coming soon](https://github.com/nuxt/nuxt/issues/14670). <!-- TODO: Remove banner when releasing Nuxt on stable channel -->
Support for accessing `useAppConfig()` in Nitro and `server` directory is [coming soon](https://github.com/nuxt/nuxt/pull/19489) and available on [edge-channel](https://nuxt.com/docs/guide/going-further/edge-channel).
:: ::
```js ```js

View File

@ -47,6 +47,13 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
as: '__publicAssetsURL', as: '__publicAssetsURL',
name: 'publicAssetsURL', name: 'publicAssetsURL',
from: resolve(distDir, 'core/runtime/nitro/paths') from: resolve(distDir, 'core/runtime/nitro/paths')
},
{
// TODO: Remove after https://github.com/unjs/nitro/issues/1049
as: 'defineAppConfig',
name: 'defineAppConfig',
from: resolve(distDir, 'core/runtime/nitro/config'),
priority: -1
} }
], ],
exclude: [...excludePattern, /[\\/]\.git[\\/]/] exclude: [...excludePattern, /[\\/]\.git[\\/]/]
@ -79,6 +86,10 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
...nuxt.options.runtimeConfig.nitro ...nuxt.options.runtimeConfig.nitro
} }
}, },
appConfig: nuxt.options.appConfig,
appConfigFiles: nuxt.options._layers.map(
layer => resolve(layer.config.srcDir, 'app.config')
),
typescript: { typescript: {
generateTsConfig: false generateTsConfig: false
}, },

View File

@ -0,0 +1 @@
export const defineAppConfig = (config: any) => config

View File

@ -1035,6 +1035,9 @@ describe('app config', () => {
} }
expect(html).toContain(JSON.stringify(expectedAppConfig)) expect(html).toContain(JSON.stringify(expectedAppConfig))
const serverAppConfig = await $fetch('/api/app-config')
expect(serverAppConfig).toMatchObject({ appConfig: expectedAppConfig })
}) })
}) })

View File

@ -0,0 +1,6 @@
export default defineEventHandler(() => {
const appConfig = useAppConfig()
return {
appConfig
}
})