Running code at runtime only
Hi, I have some server-side code I need to run ONLY during runtime, not during pre-render / buildtime etc. How can I do?
8 Replies
Hi @Nico. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To run server-side code only during runtime (not at build time or pre-render), you should place your code in locations that are executed exclusively at runtime. In Nuxt 3, the build context (used during build/pre-render) is strictly separated from the runtime context (used when your app is actually running on the server or in serverless environments). They do not share state or code, except for runtime configuration. Here’s how you can achieve runtime-only execution:
- Use server API routes or middleware: Place your code in the
Put your server-side runtime code in
server/api
or server/middleware
directories. Files here are only executed at runtime, when an HTTP request is made to the corresponding endpoint or middleware. They are not run during build or pre-render phases. For example:
- Avoid placing runtime code in nuxt.config or modules: Code in nuxt.config
and Nuxt modules is executed during the build context, not at runtime. Only use these for build-time configuration or setup, not for runtime logic Runtime Context vs. Build Context.
- Use runtime composables in server routes: If you need to access runtime config or other runtime-only features, use composables like useRuntimeConfig()
inside your server API handlers useRuntimeConfig.
In summary:Put your server-side runtime code in
server/api
or server/middleware
files. These are only executed at runtime, not during build or pre-render, ensuring your logic runs only when your app is actually serving requests.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@244171974790742018> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
No, I need it to be executed without API request @kapa.ai
Let me check the knowledge base for you again. One moment! :books:
----------------------
If you need to run server-side code only at runtime (not during build or pre-render), and you do not want it to be triggered by an API request, the knowledge sources suggest a few options, but none describe a direct, general-purpose "runtime-only" hook outside of request/response cycles or API/middleware handlers.
However, you can use Nitro server plugins to extend Nitro's runtime behavior. Nitro server plugins are executed at runtime, not during build or pre-render. You can use the
- Place your code in a Nitro server plugin for runtime-only execution. - There is no documented way in the provided sources to run arbitrary server-side code at runtime outside of request/response cycles or Nitro plugin initialization. If you need more specific lifecycle hooks (e.g., "on server start"), the knowledge sources do not provide explicit documentation for such hooks in Nuxt 3/Nitro. If this is a strict requirement, you may need to consult the Nitro documentation directly or explore advanced server customization. In short:
Use a Nitro server plugin for runtime-only code, but the knowledge sources do not describe a general-purpose runtime hook outside of request/response or plugin initialization addServerPlugin. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
addServerPlugin
utility in a Nuxt module to register such a plugin. Here’s an example from the documentation:
You register this plugin in your module using addServerPlugin
. The code inside the plugin (outside of the hooks) will run when the Nitro server starts up at runtime, not at build time addServerPlugin.
If you need to run code once when the server starts (not per request), you can place it at the top level of your Nitro plugin file. But be aware: most hooks and plugin code are designed to run in response to server events (like requests).
Summary:- Place your code in a Nitro server plugin for runtime-only execution. - There is no documented way in the provided sources to run arbitrary server-side code at runtime outside of request/response cycles or Nitro plugin initialization. If you need more specific lifecycle hooks (e.g., "on server start"), the knowledge sources do not provide explicit documentation for such hooks in Nuxt 3/Nitro. If this is a strict requirement, you may need to consult the Nitro documentation directly or explore advanced server customization. In short:
Use a Nitro server plugin for runtime-only code, but the knowledge sources do not describe a general-purpose runtime hook outside of request/response or plugin initialization addServerPlugin. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@244171974790742018> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
No, I want to run the code once, when the server starts at build time
@kapa.ai
Let me check the knowledge base for you again. One moment! :books:
----------------------
To run code once when the server starts at build time in Nuxt 3, you should use a Nuxt build-time hook. The most relevant hook for this purpose is the ready hook, which is called after Nuxt initialization, when the Nuxt instance is ready to work. You can add this hook in your nuxt.config file:
This approach ensures your code runs once during the build/startup phase, not at runtime or per request Nuxt Configuration: hooks Nuxt Hooks (build time).
If you need to perform cleanup when the server is closing, you can also use the close hook in a similar way:
These hooks are designed for build-time/server initialization tasks, not for runtime or per-request logic.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@244171974790742018> Kapa.ai is still learning and improving, please let me know how I did by reacting below.