摘要:為了解決這個(gè)問(wèn)題,我的解決方案是使用,這樣就可以將緩存到本地,再次發(fā)布后,會(huì)使舊的失效,重新請(qǐng)求并緩存。結(jié)語(yǔ)幾分鐘就搞定了,然后把之前的一個(gè)基于的后臺(tái)模板項(xiàng)目也升級(jí)了,如果有相同需求的可以參考下。
為什么要給 VUE 項(xiàng)目添加 PWA
為什么要添加?因?yàn)椴还苁遣渴鹪?IIS,還是 nginx,每次應(yīng)用部署后,再次訪問(wèn)因?yàn)榕f的 js 已經(jīng)不存在,所以頁(yè)面訪問(wèn)的時(shí)候會(huì)整個(gè)報(bào)錯(cuò),報(bào)錯(cuò)的結(jié)果就是一個(gè)白屏。
為了解決這個(gè)問(wèn)題,我的解決方案是使用 PWA ,這樣就可以將 js 緩存到本地,再次發(fā)布后,service-worker.js 會(huì)使舊的 js 失效,重新請(qǐng)求并緩存 js。
如果對(duì)于問(wèn)題這個(gè)有更好的解決方案,務(wù)必請(qǐng)大佬指定一二
安裝 PWA 的相關(guān)依賴包yarn 安裝
yarn add sw-precache-webpack-plugin --dev yarn add uglify-es --dev
npm 安裝
npm install sw-precache-webpack-plugin --dev-dev npm install uglify-es --dev-dev添加修改相關(guān)配置
下面這些文件忘記出處是哪,Github也能搜到一些,之前寫的 PWA 的 Demo 里面拿過(guò)來(lái)的~
添加 build/service-worker-dev.jsself.addEventListener("install", () => self.skipWaiting()) self.addEventListener("activate", () => { self.clients.matchAll({ type: "window" }).then(windowClients => { for (let windowClient of windowClients) { // Force open pages to refresh, so that they have a chance to load the // fresh navigation response from the local dev server. windowClient.navigate(windowClient.url) } }) })添加 build/service-worker-prod.js
;(function() { "use strict" // Check to make sure service workers are supported in the current browser, // and that the current page is accessed from a secure origin. Using a // service worker from an insecure origin will trigger JS console errors. var isLocalhost = Boolean( window.location.hostname === "localhost" || // [::1] is the IPv6 localhost address. window.location.hostname === "[::1]" || // 127.0.0.1/8 is considered localhost for IPv4. window.location.hostname.match( /^127(?:.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ ) ) window.addEventListener("load", function() { if ( "serviceWorker" in navigator && (window.location.protocol === "https:" || isLocalhost) ) { navigator.serviceWorker .register("service-worker.js") .then(function(registration) { // updatefound is fired if service-worker.js changes. registration.onupdatefound = function() { // updatefound is also fired the very first time the SW is installed, // and there"s no need to prompt for a reload at that point. // So check here to see if the page is already controlled, // i.e. whether there"s an existing service worker. if (navigator.serviceWorker.controller) { // The updatefound event implies that registration.installing is set var installingWorker = registration.installing installingWorker.onstatechange = function() { switch (installingWorker.state) { case "installed": // At this point, the old content will have been purged and the // fresh content will have been added to the cache. // It"s the perfect time to display a "New content is // available; please refresh." message in the page"s interface. break case "redundant": throw new Error( "The installing " + "service worker became redundant." ) default: // Ignore } } } } }) .catch(function(e) { console.error("Error during service worker registration:", e) }) } }) })()添加 build/load-minified.js
"use strict" const fs = require("fs") const UglifyJS = require("uglify-es") module.exports = function(filePath) { const code = fs.readFileSync(filePath, "utf-8") const result = UglifyJS.minify(code) if (result.error) return "" return result.code }修改 build/webpack.prod.conf.js
首先引用sw-precache-webpack-plugin和build/load-minified
const SWPrecacheWebpackPlugin = require("sw-precache-webpack-plugin") const loadMinified = require("./load-minified")
為 webpack 插件 HtmlWebpackPlugin 添加參數(shù) serviceWorkerLoader: `
plugins: [ .... new HtmlWebpackPlugin({ filename: process.env.NODE_ENV === "testing" ? "index.html" : config.build.index, template: "index.html", inject: true, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true // more options: // https://github.com/kangax/html-minifier#options-quick-reference }, // necessary to consistently work with multiple chunks via CommonsChunkPlugin chunksSortMode: "dependency", serviceWorkerLoader: `` }),
并在最后添加 SWPrecacheWebpackPlugin 插件
// service worker caching new SWPrecacheWebpackPlugin({ cacheId: "ysj-admin", filename: "service-worker.js", staticFileGlobs: ["dist/**/*.{js,html,css}"], minify: true, stripPrefix: "dist/" })在 /index.html 中注入 service-worker.js
<%= htmlWebpackPlugin.options.serviceWorkerLoader %>
如下所示
<%= htmlWebpackPlugin.options.serviceWorkerLoader %>
至此,添加完畢,build 之后查看緩存中是否包含 js 檢驗(yàn)結(jié)果
注意:PWA 應(yīng)用需要在本地上運(yùn)行或者 https 協(xié)議下, 要保證你的頁(yè)面是安全頁(yè)面。結(jié)語(yǔ)
幾分鐘就搞定了,然后把之前的一個(gè)基于VUE的后臺(tái)模板項(xiàng)目也升級(jí)了,如果有相同需求的可以參考下。
倉(cāng)庫(kù)地址:https://github.com/yimogit/me...
線上預(yù)覽:https://vue-admin.yimo.link/
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/97634.html
摘要:不是一個(gè)新名詞,早在年已經(jīng)提出這個(gè)思想,但是直到的發(fā)布,終于可以在中添加到主屏,只有安卓和都能使用的基本功能,它才算是真的開(kāi)始走近大眾。 原文鏈接:https://ssshooter.com/2018-09... PWA(Progressive Web Apps)雖然是網(wǎng)頁(yè)應(yīng)用,但是可以帶來(lái)媲美原生的用戶體驗(yàn),其中包含離線可用,后臺(tái)推送等功能。PWA 不是一個(gè)新名詞,早在 2015 年...
摘要:免費(fèi)升級(jí)到升級(jí)到后,服務(wù)器可以開(kāi)啟版本,對(duì)比性能和緩存各方面要更好,還有其他新特性,可以啟動(dòng)功能,更好的進(jìn)行離線緩存,更好的離線體驗(yàn)。 showImg(https://segmentfault.com/img/remote/1460000012773891?w=370&h=661); 在線地址:https://fancy.czero.cn 手機(jī)掃描二維碼查看: showImg(http...
摘要:前端每周清單專注前端領(lǐng)域內(nèi)容,以對(duì)外文資料的搜集為主,幫助開(kāi)發(fā)者了解一周前端熱點(diǎn)分為新聞熱點(diǎn)開(kāi)發(fā)教程工程實(shí)踐深度閱讀開(kāi)源項(xiàng)目巔峰人生等欄目。 前端每周清單專注前端領(lǐng)域內(nèi)容,以對(duì)外文資料的搜集為主,幫助開(kāi)發(fā)者了解一周前端熱點(diǎn);分為新聞熱點(diǎn)、開(kāi)發(fā)教程、工程實(shí)踐、深度閱讀、開(kāi)源項(xiàng)目、巔峰人生等欄目。歡迎關(guān)注【前端之巔】微信公眾號(hào)(ID:frontshow),及時(shí)獲取前端每周清單;本文則是對(duì)于...
摘要:雖然有著各種各樣的不同,但是相同的是,他們前端優(yōu)化不完全指南前端掘金篇幅可能有點(diǎn)長(zhǎng),我想先聊一聊閱讀的方式,我希望你閱讀的時(shí)候,能夠把我當(dāng)作你的競(jìng)爭(zhēng)對(duì)手,你的夢(mèng)想是超越我。 如何提升頁(yè)面渲染效率 - 前端 - 掘金Web頁(yè)面的性能 我們每天都會(huì)瀏覽很多的Web頁(yè)面,使用很多基于Web的應(yīng)用。這些站點(diǎn)看起來(lái)既不一樣,用途也都各有不同,有在線視頻,Social Media,新聞,郵件客戶端...
摘要:另外,單頁(yè)應(yīng)用因?yàn)閿?shù)據(jù)前置到了前端,不利于搜索引擎的抓取。所以我們需要對(duì)自己的單頁(yè)應(yīng)用進(jìn)行一些優(yōu)化。 前言 最近秋招之余空出時(shí)間來(lái)按自己的興趣動(dòng)手做了一個(gè)項(xiàng)目,一個(gè)基于vue-cli3.0, vue,typescript的移動(dòng)端pwa,現(xiàn)在趁熱打鐵,將這個(gè)項(xiàng)目從開(kāi)發(fā)到部署整個(gè)過(guò)程記錄下來(lái),并將從這個(gè)項(xiàng)目中學(xué)習(xí)到的東西分享出來(lái),如果大家有什么意見(jiàn)或補(bǔ)充也可以在評(píng)論區(qū)提出。先介紹一下這個(gè)項(xiàng)...
閱讀 4090·2021-10-19 13:23
閱讀 2430·2021-09-09 11:37
閱讀 2689·2019-08-29 15:20
閱讀 3580·2019-08-29 11:08
閱讀 1788·2019-08-26 18:27
閱讀 1846·2019-08-23 12:20
閱讀 3145·2019-08-23 11:54
閱讀 2688·2019-08-22 15:19