Solid Start, how to add backend/nitro plugin?

Lets say I want to have this code initializing when i start/restart the server itself, independent of any clients visiting the webapp?
console.log('Plugin started')

setInterval(()=>{ console.log('Plugin running') }, 5000)
console.log('Plugin started')

setInterval(()=>{ console.log('Plugin running') }, 5000)
2 Replies
Adam Goldstein
This is working for me (in prod, keep in mind it won't run for dev servers iirc)
import { defineNitroPlugin } from "nitropack/runtime";

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hookOnce("close", async () => {
console.log("Runs on close");
});
});
import { defineNitroPlugin } from "nitropack/runtime";

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hookOnce("close", async () => {
console.log("Runs on close");
});
});
in a file in the plugins/ direction https://nitro.build/guide/plugins
Plugins - Nitro
Use plugins to extend Nitro's runtime behavior.
Adam Goldstein
I think code at the top level above the export will run only once at start? Not sure if there's a hook for server start

Did you find this page helpful?