kit
kit copied to clipboard
Worker Threads
Describe the problem
I'd like to let Sveltekit run a regular background task that is CPU-intensive and don't want to block all the apps function. The only way visible to me is using node's worker threads, either directly or using a library like Bree. Problem is, that each worker has to be a separate file that is only referenced by its name. It kinda works in dev mode; but when you run the build step, my worker gets treeshaken out; and even if it did not, the bundler would change the name and may not adapt the reference. I'm greatful about every hint...
Describe the proposed solution
Do support worker threads in some way; or help me how to prevent my worker from being tree-shaken.
Alternatives considered
No response
Importance
i cannot use SvelteKit without it
Additional Information
No response
Importing workers is really a Vite thing.
As far as I know it's limited to web workers
Do support worker threads in some way; or help me how to prevent my worker from being tree-shaken.
What sort of work do you want to put on the worker? Is it something that needs sveltekit specific state or can it just be an additional script you run along side your sveltekit project?
It should be possible to be put into a separate script, as long as it can access the database. I'm using prisma for the database, so the prisma schema and generated client library are in some way part of the svelte project. Is there a way to define a deparate entrypoint in the project for such a script?
You should just be able to import your prisma client into a js/ts file and run it. When you deploy your kit application (I am assuming you are using node adapter) you can copy your script as well and run it as a separate process. Don't forget to make sure the prisma node module is installed wherever you deploy the script, or you could have a build script that will bundle it into a single file - up to you!
Also I found an already open issue (#3653) for worker threads
I could try that; but real support that works in dev and prod would be a lot nicer. Is there any easy way to tell vite to just copy a job-script; or better to bundle it separately?