unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.89k stars 496 forks source link

Compute runtimeConfig at startup #2569

Open cjpearson opened 3 months ago

cjpearson commented 3 months ago

Describe the feature

Our Nuxt application has a fairly large runtime configuration and we've noticed that performance of the application decreases as the size of this config grows. The call to applyEnv takes up a large portion of the execution time. This problem is exacerbated by the fact that the runtime config is calculated for every request.

We've prototyped a simple plugin to compute the runtime config at startup and re-use for each request.

export default defineNitroPlugin((nitroApp: NitroApp) => {
  const runtimeConfig = useRuntimeConfig() as RuntimeConfig
  nitroApp.hooks.hook('request', (event) => {
    event.context.nitro = event.context.nitro ?? {}
    event.context.nitro.runtimeConfig = runtimeConfig
  })
})

So far the results seem to be promising, and I think it might also be beneficial for those with smaller runtime configs. Do you think it makes sense to include something like this in the nitro core or is there a need for each request to have a fresh runtime config?

Additional information