10

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.

Olian04
  • 6,480
  • 2
  • 27
  • 54
LiterallyNoOne
  • 101
  • 1
  • 5
  • It likely depends on the browser. Unless it's defined in the spec the then you shouldn't rely on it. – Olian04 Dec 05 '20 at 12:46
  • Thank you @Olian04 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. – LiterallyNoOne Dec 05 '20 at 17:49
  • 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: https://developers.google.com/web/fundamentals/primers/service-workers) – Olian04 Dec 06 '20 at 00:39

2 Answers2

5

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".

run_the_race
  • 1,344
  • 2
  • 36
  • 62
3

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)

pl4n3th
  • 121
  • 3
  • i am accepting your answer @pl4n3th .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. – LiterallyNoOne Dec 11 '20 at 17:25
  • 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. – pl4n3th Dec 13 '20 at 10:54
  • 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. – LiterallyNoOne Dec 13 '20 at 17:39
  • Nope, the service worker goes through install, activate only once: when a fresh service worker is available. See Jeff Posnick answer : https://stackoverflow.com/questions/38835273/when-does-code-in-a-service-worker-outside-of-an-event-handler-run/38835274#38835274 – pl4n3th Dec 14 '20 at 19:51