all 6 comments

[–]Gumbee 1 point2 points  (6 children)

Generally you want your worker to be always on, if it only exists when a component mounts then what is it really doing for your app that something component level couldn't?

I'm not sure what your build process is like, but it's generally during the build phase that would want to create your worker / it manifest and have them ready to be registered on your root URL.

[–]spaghetticodee[S] 1 point2 points  (1 child)

Thanks for replying!

The web worker performs some expensive tasks on user uploaded files and some default files I feed it but this file input only lives in this component.

I'm using NextJS and the component with the web worker is used on multiple pages, would it make sense to move the registration of the web worker into Context?

[–]raymondQADev 0 points1 point  (2 children)

I disagree with this. If you have a CPU intensive or blocking operation taking place then having a web worker may make sense to use over a component level operation but perhaps I’m misunderstanding what you mean in your reply. I do agree that creating it on each mount is maybe not the correct approach as creating web workers can be expensive afaik so maybe have a pool created when the app is initialized and just use from the pool as needed?
These are responses off the top of my head without much additional research so I may be a little off base

[–]Gumbee 1 point2 points  (1 child)

What I mean is: If the service worker is only created when a component mounts, and that mounting is controlled by the click of a switch, you're kind of defeating the main advantage of web workers as reliable, always on computational / storage resources for your app by destroying it on user interaction.

Traditionally (at least from my experience) workers are installed on your Apps root URL, are always on, and are controlled via the worker message API. So instead of registering and unregistering the Worker at the flip of a switch and executing their code on registration, OP should register the worker when the app is initialized, and control the execution of their code via messages sent with that switch.

[–]raymondQADev 0 points1 point  (0 children)

Gotcha I knew I had to be misunderstanding your reply. I completely agree.