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