微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

服务人员随机页面生成器

如何解决服务人员随机页面生成器

我正在尝试更新sw.js上的抓取内容,以随机选择三个html页面之一。 1.html; 2.html和3.html。我应该能够测试一下,然后随机拉起。似乎什么都没发生。

self.addEventListener('install',event => {
    // fires when the browser installs the app
    // here we're just logging the event and the contents
    // of the object passed to the event. the purpose of this event
    // is to give the service worker a place to setup the local 
    // environment after the installation completes.
    console.log(`Event fired: ${event.type}`);
    console.dir(event);
});

self.addEventListener('activate',event => {
    // fires after the service worker completes its installation. 
    // It's a place for the service worker to clean up from prevIoUs 
    // service worker versions
    console.log(`Event fired: ${event.type}`);
    console.dir(event);
});

self.addEventListener('fetch',event => {
    // Fires whenever the app requests a resource (file or data)
    // normally this is where the service worker would check to see
    // if the requested resource is in the local cache before going
    // to the server to get it. There's a whole chapter in the book
    // covering different cache strategies,so I'm not going to say 
    // any more about this here
    console.log(`Fetching ${event.request.url}`);
    // console.dir(event.request);
    // Next,go get the requested resource from the network,// nothing fancy going on here.
    event.respondWith(fetch(event.request));
});

self.addEventListener('fetch',event => {
    let ran = Math.floor(Math.random() *3) +1
    let ranPage = ran.toString() + '.html';
    console.log(`Fetching random ${ranPage}'}`);
    event.respondWith(fetch(ranPage));
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。