N
Nuxt4w ago
Fatalis

NuxtHub - useRuntimeConfig not working as expected in server middleware

useRuntimeConfig is returning config defaults instead of env overrides (cloudflare/nuxthub) when called in a middleware in server/middleware. Also breaks useRuntimeConfig calls in normal server routes if useRuntimeConfig is called anywhere in a middlewhere. What am I not understanding?
3 Replies
kapa.ai
kapa.ai4w ago
Hi @Fatalis. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, I can offer some insights into your issue with useRuntimeConfig in server middleware. The problem you're experiencing is likely related to how useRuntimeConfig is being called in your server middleware. According to the Nuxt documentation on useRuntimeConfig, when using useRuntimeConfig in server routes (which includes server middleware), it's recommended to pass the event as an argument:
export default defineEventHandler((event) => {
const config = useRuntimeConfig(event)
})
export default defineEventHandler((event) => {
const config = useRuntimeConfig(event)
})
Passing the event is optional, but it's recommended to get the runtime config overwritten by environment variables at runtime for server routes. This might explain why you're seeing the config defaults instead of the env overrides. Additionally, there have been some recent changes and issues related to useRuntimeConfig usage: 1. A recent pull request addressed an issue with passing the event to useRuntimeConfig in the Nuxt renderer. This suggests that there have been ongoing improvements to how runtime config works, especially in server-side contexts. 2. Another issue mentioned problems with useRuntimeConfig in certain modules after Nuxt versions >3.14.0, stemming from changes in nitropack. The recommendation was to only call useRuntimeConfig inside a function. Given these points, here are some suggestions: 1. Ensure you're passing the event to useRuntimeConfig in your server middleware. 2. Check that you're using the latest version of Nuxt, as there have been recent fixes related to this issue. 3. Make sure you're calling useRuntimeConfig inside a function in your middleware, not at the top level. If you're still experiencing issues after trying these suggestions, it might be worth creating a minimal reproduction of the problem and opening an issue on the Nuxt GitHub repository. Nuxt Runtime Config Documentation provides more detailed information on how runtime config should be set up and used, which might help you troubleshoot further. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@83273076162166784> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Fatalis
FatalisOP4w ago
export default defineEventHandler((event) => { const { test } = useRuntimeConfig(event); } this is what I have and it doesn't work NUXT_TEST is set and works fine in normal server routes

Did you find this page helpful?