What is the lifetime for a variable stored in service worker?
Asked Answered
M

2

13

For example i declare a global variable in service worker like

var cacheVersion  = "v1"; // line no 1 in sw.js

what is the lifetime for this variable?. will this variable be there until service worker gets unregistered or will it be deleted if the service worker was in idle state.

Moffett answered 5/12, 2020 at 12:42 Comment(3)
It likely depends on the browser. Unless it's defined in the spec the then you shouldn't rely on it.Ilailaire
Thank you @Ilailaire for your reply. If I want to get a variable in service worker in all time. Whether I want to store that variable in persistent storage like indexeddb or any other method is there? and now I am having another doubt regarding activate event in sw will this event tigger everytime sw comes from idle state and also in page refresh time.Moffett
These are some pretty basic service worker problems (in other words, you will find great answers in guides made by the big players, like google: developers.google.com/web/fundamentals/primers/service-workers)Ilailaire
H
6

I think @pl4n3th only paints half the picture.

I am using the term "shutdown" to mean the browser still has the service worker registered, but maybe you having been browsing the site for awhile, so it frees it from memory.

Yes the global state is lost when the browser shuts down the service worker. However, when it resumes it, all code at the top level is run again, and hence it will execute this line (if its at the top level):

var cacheVersion  = "v1"; // line no 1 in sw.js

Which means now you have the cacheVersion variable available again. Since its a constant, no problem.

However if say in a fetch callback, you set cacheVersion = "v2". When the service worker is next shutdown, then resumed, the global state will be lost, it will run the javascript again with the top level instruction, hence cacheVersion will once again be "v1".

Hepatic answered 22/1, 2022 at 11:59 Comment(0)
R
4

Any variable stored inside a service worker will only live as long as the service worker. As soon as the browser kills it, your variable is gone. If you want to persist a value, use either indexedDb (accessible by the service worker) or LocalStorage (DOM only access so you'll need to pass it to the service worker by postMessage)

Resilience answered 7/12, 2020 at 15:49 Comment(4)
i am accepting your answer @Resilience .But what about service worker event (like fetch,message event) and its local functions. whether the browser kills those event listener and its functions also? if not, can you explain me why. Because i hope that all sw variables and its functions will store in same place.Moffett
The browser kills the service worker when it considers that there "is nothing more to do" (as far as I understand). This is why you want to tell the browser to "not shutdown" your service worker when processing long tasks. Some exemple: ` if (event.data.type === 'SOME_IMPORTANT_STUF') { event.waituntil(doTheLongImportnatStuff()) } ` Hope this is more clear to you.Resilience
Let us take that the browser killed the service worker. After that if we refresh the page (where the service worker was in active state) will the service worker again starts to compile from beginning (i.e installation-activate-fetch event and so on) or it will go directly to required event there. Anyway Thank you for helping me.Moffett
Nope, the service worker goes through install, activate only once: when a fresh service worker is available. See Jeff Posnick answer : #38835773Resilience

© 2022 - 2024 — McMap. All rights reserved.