vue实现server-worker的实现方案

最近做一个新的官网(没有任何交互) 采用这个server-worker的技术,可以秒开或者离线访问
具体server-worker的介绍,移步 https://lavas.baidu.com/pwa

测试条件: localhost或者https的才能测试

具体的介绍自己查阅资料
我采用的 offline-plugin 的一个npm包 详见 https://github.com/NekR/offline-plugin

按照上面的方法测试,发现一个的问题的,改了代码也不能在显示当前最新的代码(prodution的环境)

需要修改一个代码:
代码连接:https://github.com/NekR/offline-plugin/blob/master/docs/updates.md

// webpack.prod.conf.js

const OfflinePlugin = require('offline-plugin');

      new OfflinePlugin({
            responseStrategy: 'cache-first', // 缓存优先
            AppCache: false, // 不启⽤appCache
            safeToUseOptionalCaches: true, // Removes warning for about `additional` section usage
            autoUpdate: true, // ⾃动更新
            caches: { // webpack打包后需要换的⽂件正则匹配
                main: [
                    '**/*.js',
                    '**/*.css',
                    /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                    /\.(woff2?|eot|ttf|otf)(\?.*)?$/
                ],
                additional: [
                    ':externals:'
                ]
            },
            externals: [], // 设置外部链接,例如配置 http: //hello.com/getuser,那么在请求这个接⼝的时候就会进⾏接⼝缓存
            excludes: ['**/.*', '**/*.map', '**/*.gz', '**/manifest-last.json'], // 需要过滤的⽂ 件
            autoUpdate: 1000 * 60 / 20,
               // 自动更新 // Tell to OfflinePlugin to generate events for ServiceWorker, console.log to some event
            ServiceWorker: {
                output: './static/sw.js', // 输出⽬录
                publicPath: '/static/sw.js?t=' + new Date(), // sw.js 加载路径
                scope: '/', // 作⽤域(此处有坑)
                minify: true, // 开启压缩
                events: true // 当sw状态改变时候发射对应事件
            }
        })
// App.vue 或者 main.js 等入口文件中。
const runtime = require('offline-plugin/runtime');

runtime.install({
  onUpdating: () => {
    console.log('SW Event:', 'onUpdating');
  },
  onUpdateReady: () => {
    console.log('SW Event:', 'onUpdateReady');
    // Tells to new SW to take control immediately
    runtime.applyUpdate();
  },
  onUpdated: () => {
    console.log('SW Event:', 'onUpdated');
    // Reload the webpage to load into the new version
    window.location.reload();
  },

  onUpdateFailed: () => {
    console.log('SW Event:', 'onUpdateFailed');
  }
});

重新测试一下就ok了

{ "name": "meadia-manage", "version": "1.0.0", "description": "A Vue.js project", "author": "winspread", "private": true, "scripts": { "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", "start": "npm run dev", "build": "node build/build.js" }, "dependencies": { "axios": "^0.18.0", "echarts": "^4.2.1", "element-ui": "^2.15.10", "moment": "^2.29.4", "vue": "^2.5.2", "vue-puzzle-vcode": "^1.1.9", "vue-router": "^3.0.2", "vuex": "^3.1.0" }, "devDependencies": { "autoprefixer": "^7.1.2", "axios-mock-adapter": "^1.16.0", "babel-core": "^6.22.1", "babel-helper-vue-jsx-merge-props": "^2.0.3", "babel-loader": "^7.1.1", "babel-plugin-syntax-jsx": "^6.18.0", "babel-plugin-transform-runtime": "^6.22.0", "babel-plugin-transform-vue-jsx": "^3.5.0", "babel-preset-env": "^1.3.2", "babel-preset-stage-2": "^6.22.0", "chalk": "^2.0.1", "copy-webpack-plugin": "^4.0.1", "css-loader": "^0.28.0", "element-theme-chalk": "^2.15.10", "extract-text-webpack-plugin": "^3.0.0", "file-loader": "^1.1.4", "friendly-errors-webpack-plugin": "^1.6.1", "html-webpack-plugin": "^2.30.1", "mockjs": "^1.0.1-beta3", "node-notifier": "^5.1.2", "node-sass": "^6.0.1", "optimize-css-assets-webpack-plugin": "^3.2.0", "ora": "^1.2.0", "portfinder": "^1.0.13", "postcss-import": "^11.0.0", "postcss-loader": "^2.0.8", "postcss-url": "^7.2.1", "rimraf": "^2.6.0", "sass-loader": "^10.2.0", "semver": "^5.3.0", "shelljs": "^0.7.6", "uglifyjs-webpack-plugin": "^1.1.1", "url-loader": "^0.5.8", "vue-loader": "^13.3.0", "vue-style-loader": "^3.0.1", "vue-template-compiler": "^2.5.2", "webpack": "^3.6.0", "webpack-bundle-analyzer": "^2.9.0", "webpack-dev-server": "^2.9.1", "webpack-merge": "^4.1.0" }, "engines": { "node": ">= 6.0.0", "npm": ">= 3.0.0" }, "browserslist": [ "> 1%", "last 2 versions", "not ie <= 8" ] }
最新发布
04-01
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值