0

I'm trying to use the Service Worker but a the moment of registration I get this error:

Js Error

I got that the service worker can't subscribe because the evaluation of the script failed, but I don't get why "Username" is not defined, cause if I debug it I can see the value inside it, and inside the service worker the value is passed correctly, but I can't figure out why "Username" is not defined. Help please!

This is the start and the end of the Sevice Worker:

The script in my View

This is the Service Worker:

The start and the end of the Sevice Worker

funie200
  • 3,688
  • 5
  • 21
  • 34
ZeroBrush
  • 29
  • 3
  • 1
    Please add your code and error as text to the question, not as an image – funie200 Dec 03 '20 at 10:51
  • Note that the script show in the picture, which shows `var Username = '@user.Username' || "";`, the `|| ""` will never be taken. If `user.Username` is undefined, the JavaScript variable `Username` will be the string "undefined". – Heretic Monkey Sep 27 '21 at 21:13
  • Does this answer your question? [Passing state info into a service worker before \`install\`](https://stackoverflow.com/questions/44424709/passing-state-info-into-a-service-worker-before-install) – Heretic Monkey Sep 27 '21 at 21:14
  • See also [Is there a way to pass some value to a serviceworker to use during the install step?](https://stackoverflow.com/q/40387983/215552) – Heretic Monkey Sep 27 '21 at 21:18

1 Answers1

0

You can inject parameter in url during registration

navigator.serviceWorker.register("my-service-worker.js?someParam=someValue")

and inside my-service-worker.js you can read it as follows

var param = null;

self.addEventListener('install', function (event) {
    param = new URL(location).searchParams.get('someParam');
});

you can add more params using & separator

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345