简体   繁体   English

如何在停止后工作的服务工作者中加载外部脚本?

[英]How to load external script in service worker that will work after stop?

I can't find this infomration.我找不到这个信息。 How to use importScripts that will work after the page is down and refreshed after a while?如何使用在页面关闭并在一段时间后刷新后将起作用的 importScripts?

I have this code:我有这个代码:

self.addEventListener('install', function(evt) {
    self.skipWaiting();
    self.importScripts('https://cdn.jsdelivr.net/npm/idb-keyval/dist/umd.js');
});

I've also tried:我也试过:

self.addEventListener('install', function(event) {
    event.waitUntil(
        self.importScripts('https://cdn.jsdelivr.net/npm/idb-keyval/dist/umd.js')
    );
});
self.addEventListener('activate', (event) => {
    event.waitUntil(
        self.importScripts('https://cdn.jsdelivr.net/npm/idb-keyval/dist/umd.js')
    );
});

and when I open the site after a few minutes, I've got error about missing library.几分钟后我打开网站时,我收到关于缺少库的错误。

What is the proper way to load a file with importScripts that will work?使用有效的importScripts加载文件的正确方法是什么? I just want to keep using this library.我只想继续使用这个库。

I can't find this information anywhere, there are no much examples how to use external libraries in service workers.我在任何地方都找不到此信息,也没有太多示例如何在服务人员中使用外部库。

It doesn't have to be importScripts but this it the only way I know to import external file in Service Worker.它不一定是importScripts ,但这是我知道在 Service Worker 中导入外部文件的唯一方法。 I'm not sure if you have use ES Modules for that.我不确定您是否为此使用了 ES 模块。

EDIT :编辑

I've also tried this:我也试过这个:

self.addEventListener('install', function(event) {
    self.skipWaiting();
    self.importScripts('https://cdn.jsdelivr.net/npm/idb-keyval/dist/umd.js');
    self.idb = idbKeyval;
});

self.addEventListener('activate', function(event) {
    if (!self.idb) {
        self.skipWaiting();
        self.importScripts('https://cdn.jsdelivr.net/npm/idb-keyval/dist/umd.js');
        self.idb = idbKeyval;
    }
});

I've set break point in activate event and it was not executed at all the same as install and my variable got removed.我在activate事件中设置了断点,它没有像安装一样执行,我的变量被删除了。

I was testing by stopping the service worker in dev tools and refresh.我通过在开发工具中停止服务人员并刷新来进行测试。 Each time got error about missing idb .每次都会收到关于缺少idb的错误。

I've also tried using local let variable in the service worker, got the same results idb is undefined, after service worker was stopped.我也尝试在服务工作者中使用本地let变量,在服务工作者停止后得到相同的结果idb未定义。

EDIT2 :编辑2

I've asked the author on GitHub , since this may be issue with the library.我已经在 GitHub 上询问了作者,因为这可能是图书馆的问题。

My other code was working it looked like this:我的其他代码正在运行,看起来像这样:

self.addEventListener('install', function(evt) {
    self.skipWaiting();
    self.importScripts('https://cdn.jsdelivr.net/npm/browserfs');
    BrowserFS.configure({ fs: 'IndexedDB', options: {} }, function (err) {
        if (err) {
            console.log(err);
        } else {
            self.fs = BrowserFS.BFSRequire('fs');
            self.path = BrowserFS.BFSRequire('path');
        }
    });
});

I think that add library to self make it stay in memory and will not be garbage collected.我认为将库添加到 self 使其留在 memory 并且不会被垃圾收集。

So I just used:所以我只是使用:

self.addEventListener('install', function(event) {
    event.waitUntil(
       self.importScripts('https://cdn.jsdelivr.net/npm/idb-keyval/dist/umd.js')
    );
    self.idb = idbKeyval;
});

What is the proper way to load a file with importScripts that will work?使用有效的 importScripts 加载文件的正确方法是什么? I just want to keep using this library.我只想继续使用这个库。

You would usually use importScripts in the global scope of the service worker, outside of any event callbacks:您通常会在服务工作者的全局 scope 中使用importScripts ,在任何事件回调之外:

importScripts('https://cdn.jsdelivr.net/npm/idb-keyval/dist/umd.js');

self.addEventListener('install', function (evt) {
  // your install handler
  // `self.idbKeyval` should be available here, as well as anywhere else
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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