zoukankan      html  css  js  c++  java
  • ServiceWorker pwa缓存

    index.js

    if ( navigator.serviceWorker ) {
    
        console.log("cache index")
    
        window.addEventListener("load", function(){
            navigator.serviceWorker
                .register('/serversWork.js')
                .then(function(registration) {
                    console.log('service worker 注册成功')
                })
                .catch(function(err) {
                    console.log('servcie worker 注册失败')
                })
        })
        
    }else{
        console.log("chche error")
    }
    

    serversWork.js

    
    let cachelist = [
        "/index.js",
        // "/favicon.ico",
        "/_nuxt/app.js",
        "/_nuxt/runtime.js",
        "/_nuxt/commons.app.js",
        "/_nuxt/pages/index.js",
        // "/_nuxt/930f2fe73ad8f77ebbc1.js",
    ]
    
    self.addEventListener('install', e => {
        e.waitUntil(
            caches.open('my-cache').then(function (cache) {
                // console.log("cache success")
                return cache.addAll(cachelist)
            })
        )
    })
    
    // 拦截所有请求事件
    // 如果缓存中已经有请求的数据就直接用缓存,否则去请求数据
    self.addEventListener('fetch', e => {
        e.respondWith(
            caches.match(e.request).then(function (response) {
                
                if ( response ) {
                    return response
                }
    
                return fetch( e.request );
    
            })
        )
    })
    
    
  • 相关阅读:
    memcached 高级机制(一)
    memcached 简介
    Hibernate
    Linux中的搜索命令
    Linux的常用命令
    Git清除用户名和密码
    关于Git的简单使用
    文件的上传与下载(2)
    关于文件的上传和后台接收
    验证码的制作
  • 原文地址:https://www.cnblogs.com/taoquns/p/10475814.html
Copyright © 2011-2022 走看看