服务人员随机页面生成器

时间:2020-09-05 15:46:09

标签: javascript service-worker

我正在尝试更新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));
});

0 个答案:

没有答案