From e84ec61eeb56cc40e818d425932a8c9c57579f80 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Tue, 14 Mar 2023 10:54:59 +0100 Subject: [PATCH] feat(nuxt): support app config for server routes (#19489) --- docs/2.guide/2.directory-structure/3.app-config.md | 3 ++- packages/nuxt/src/core/nitro.ts | 11 +++++++++++ packages/nuxt/src/core/runtime/nitro/config.ts | 1 + test/basic.test.ts | 3 +++ test/fixtures/basic/server/api/app-config.ts | 6 ++++++ 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packages/nuxt/src/core/runtime/nitro/config.ts create mode 100644 test/fixtures/basic/server/api/app-config.ts diff --git a/docs/2.guide/2.directory-structure/3.app-config.md b/docs/2.guide/2.directory-structure/3.app-config.md index f15bfe32a1..319790003d 100644 --- a/docs/2.guide/2.directory-structure/3.app-config.md +++ b/docs/2.guide/2.directory-structure/3.app-config.md @@ -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. ::alert{type=info} -Support for accessing `useAppConfig()` in Nitro and `server` directory is [coming soon](https://github.com/nuxt/nuxt/issues/14670). + +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 diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index dfe2693171..d96fd27fd2 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -47,6 +47,13 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { as: '__publicAssetsURL', name: 'publicAssetsURL', 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[\\/]/] @@ -79,6 +86,10 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { ...nuxt.options.runtimeConfig.nitro } }, + appConfig: nuxt.options.appConfig, + appConfigFiles: nuxt.options._layers.map( + layer => resolve(layer.config.srcDir, 'app.config') + ), typescript: { generateTsConfig: false }, diff --git a/packages/nuxt/src/core/runtime/nitro/config.ts b/packages/nuxt/src/core/runtime/nitro/config.ts new file mode 100644 index 0000000000..3d7d01f46a --- /dev/null +++ b/packages/nuxt/src/core/runtime/nitro/config.ts @@ -0,0 +1 @@ +export const defineAppConfig = (config: any) => config diff --git a/test/basic.test.ts b/test/basic.test.ts index bd80127407..f3ce6b8586 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -1035,6 +1035,9 @@ describe('app config', () => { } expect(html).toContain(JSON.stringify(expectedAppConfig)) + + const serverAppConfig = await $fetch('/api/app-config') + expect(serverAppConfig).toMatchObject({ appConfig: expectedAppConfig }) }) }) diff --git a/test/fixtures/basic/server/api/app-config.ts b/test/fixtures/basic/server/api/app-config.ts new file mode 100644 index 0000000000..8b4c1ff97e --- /dev/null +++ b/test/fixtures/basic/server/api/app-config.ts @@ -0,0 +1,6 @@ +export default defineEventHandler(() => { + const appConfig = useAppConfig() + return { + appConfig + } +})