Cannot set runtime config options in the `listen` hook
See original GitHub issueEnvironment
Nuxt CLI v3.0.0-27404337.c9c0171 23:24:46 RootDir: /Volumes/Samsung_T5/GitHub/mai-attire 23:24:47 Nuxt project info: 23:24:47
- Operating System:
Darwin
- Node Version:
v14.18.2
- Nuxt Version:
3.0.0-27404337.c9c0171
- Package Manager:
yarn@1.22.17
- Bundler:
Vite
- User Config:
privateRuntimeConfig
,buildModules
,googleFonts
,css
,vite
,ngrok
- Runtime Modules:
-
- Build Modules:
~/modules/google-fonts
,~/modules/ngrok
Reproduction
nuxt.hook('listen', () => {
const url = 'my.url.com'
nuxt.options.publicRuntimeConfig.ngrok = { url };
})
import config from "#config";
export default class ServerMiddlewarUtils {
constructor() {
console.log("config", config);
}
}
Describe the bug
When creating a module, the only way I can see to set the runtime configs is as shown above - this was also how modules used to do it as well such as the ngrok module.
The new value is not available when importing #config
in server middleware.
Additional context
No response
Logs
No response
Issue Analytics
- State:
- Created a year ago
- Comments:9 (2 by maintainers)
Top GitHub Comments
Isn’t the
listen
hook only available in development mode ?By this point in the server lifecycle, we have already passed runtime configuration to nitro, so changing top-level items doesn’t take effect (but modifying nested objects should work fine): https://stackblitz.com/edit/github-bsr21s.
However, for a dev module, you should be careful not to set runtimeConfig at build-time or the configuration will persist into the built server. It’s also limited to things that can be serialized - so no functions in runtimeConfig, for example.
Finally, it’s also possible to add a template and access this within your serverMiddleware: https://stackblitz.com/edit/github-bsr21s-bf23tt.
Note: We should consider which of these options we recommend (if any) and document this in the module authors guide. cc: @pi0 @clemcode