简体   繁体   中英

Inject parameter in Service Worker

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

JS错误

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:

我视图中的脚本

This is the Service Worker:

Service Worker 的开始和结束

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM