webpack学习第十九步—— PWA

2020-02-14  本文已影响0人  Love小六六

使用http-server

npm install http-server -D
"scripts": {
    "server": "http-server build",
    "start": "webpack-dev-server --config ./config/webpack.common.js",
    "build": "webpack --env.production=abc --config ./config/webpack.common.js"
}
console.log('Hello Dingding')
npm build
npm run server

PWA

npm install workbox-webpack-plugin -D
plugins:[
    new MiniCssExtractPlugin({
        filename: '[name].css',
        chunkFilename: '[name].chunk.css'
    }),
    // 使用WorkBox插件
    new WorkBoxPlugin.GenerateSW({
        clientsClaim: true,
        skipWaiting: true
    })
]
console.log('Hello Dingding')

if('serviceWorker' in navigator) {
    window.addEventListener('load',() => {
        navigator.serviceWorker.register('./service-worker.js')
            .then(register => {
                console.log('success')
            })
            .catch(e => console.log(e))
    })
}
上一篇下一篇

猜你喜欢

热点阅读