sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
17.81k stars 1.78k forks source link

Allow a task to be formed when the application is initialized. #12138

Closed maietta closed 3 weeks ago

maietta commented 4 weeks ago

Describe the problem

During application start, I expected to be able to perform a task, such as registering a cron job or sending or signal to a remote monitoring service, when the Svelte application has started.

This does not seem possible at the moment.

Describe the proposed solution

The solution would be to provide a init hook or some other way to perform a task when the app is run.

Alternatives considered

Using the custom server option does work, but not ideal.

Importance

i cannot use SvelteKit without it

Additional Information

In my particular case, it's jamming me up for several days while trying to find a work-around that does not involve the custom server option.

maietta commented 4 weeks ago

I should follow up by mentioning this would be with the use of the node-adapter for long-running node processes. Don't know if that really matters, but it's my target enviornment.

maietta commented 4 weeks ago

This could actually be a bug, as the expected behavior would be to place something like console.log("Hello World") in the body of the hooks.server.ts and see it when running the app.

siccovansas commented 3 weeks ago

For some more context see this thread with replies from Rich Harris: https://twitter.com/maietta/status/1781122558110380083

Conduitry commented 3 weeks ago

In dev mode, top-level hooks.server.js code will run on the first incoming request, but in production builds with the Node adapter, it will run when starting the application. If you're seeing something different, that does sound like a bug, but we would need a minimal reproduction of that.

maietta commented 3 weeks ago

In dev mode, top-level hooks.server.js code will run on the first incoming request, but in production builds with the Node adapter, it will run when starting the application. If you're seeing something different, that does sound like a bug, but we would need a minimal reproduction of that.

Okay, interesting!

I'd love to know if this difference between dev mode and prod for node-adapter (or other adapters) was intended to operate differently when choosing to be eager or not when initializing the app.

Because if this is the case and it works when I deploy this, then I can at least for now, work with that. Feels hacky still, but I got to make this work. My tasks need the context of the application, which is why I can't really just include my stuff outside the app in a separate script.

maietta commented 3 weeks ago

I'm going to close this out, but it seems not well documented that the prod vs dev behave different during the application start.

In production, adding something like console.log("hello, world") to the body of hooks.server.ts, does in fact work. In development, it does not.

I'm going to close this ticket out. If this was not intended, then it's a bug. For now, it's not a bug, IMO.