exebook
exebook

Reputation: 33930

new Worker vs navigator.serviceWorker

What is the difference between new Worker() and navigator.serviceWorker.register()?

var myWorker = new Worker('service-worker.js', {});

Do they both just create the same kind of service worker?

navigator.serviceWorker.register('service-worker.js').then(()=>{})

Please do not downvote, I have 50+ tabs open for days related to service workers, feel overwhelmed and desperate, this whole thing does not make much sense to me regardless (or maybe as a result of) plenty of available documentation.

Upvotes: 4

Views: 747

Answers (1)

They are not the same.

new Worker() creates a Web Worker. This is a separate thread and execution context, but it's still tied to the current page session and will be terminated when the user navigates away from the page.

Service Workers run in a similar environment to Web Workers (they share some APIs), but are not connected to the specific page session, and run briefly in the background in response to network events on their associated site.

Upvotes: 6

Related Questions