From 178c48deda9b4278925c32ce72edf8485d83a7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Fri, 8 Feb 2019 23:24:34 +0800 Subject: [PATCH] feat: support pwa config (#3508) * feat: support pwa config * style: change code style --- config/config.js | 18 +++++--- src/defaultSettings.js | 1 + src/global.js | 99 ++++++++++++++++++++++-------------------- 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/config/config.js b/config/config.js index 887b0614..61fe8e2b 100644 --- a/config/config.js +++ b/config/config.js @@ -5,6 +5,8 @@ import webpackPlugin from './plugin.config'; import defaultSettings from '../src/defaultSettings'; import slash from 'slash2'; +const { pwa, primaryColor } = defaultSettings; + const plugins = [ [ 'umi-plugin-react', @@ -22,12 +24,14 @@ const plugins = [ loadingComponent: './components/PageLoading/index', webpackChunkName: true, }, - pwa: { - workboxPluginMode: 'InjectManifest', - workboxOptions: { - importWorkboxFrom: 'local', - }, - }, + pwa: pwa + ? { + workboxPluginMode: 'InjectManifest', + workboxOptions: { + importWorkboxFrom: 'local', + }, + } + : {}, ...(!process.env.TEST && os.platform() === 'darwin' ? { dll: { @@ -67,7 +71,7 @@ export default { // Theme for antd // https://ant.design/docs/react/customize-theme-cn theme: { - 'primary-color': defaultSettings.primaryColor, + 'primary-color': primaryColor, }, externals: { '@antv/data-set': 'DataSet', diff --git a/src/defaultSettings.js b/src/defaultSettings.js index ad025cae..dfdb5341 100644 --- a/src/defaultSettings.js +++ b/src/defaultSettings.js @@ -10,4 +10,5 @@ module.exports = { disableLocal: false, }, title: 'Ant Design Pro', + pwa: true, }; diff --git a/src/global.js b/src/global.js index 62f8cceb..bf60b418 100644 --- a/src/global.js +++ b/src/global.js @@ -1,54 +1,59 @@ import React from 'react'; import { notification, Button, message } from 'antd'; import { formatMessage } from 'umi/locale'; +import defaultSettings from './defaultSettings'; -// Notify user if offline now -window.addEventListener('sw.offline', () => { - message.warning(formatMessage({ id: 'app.pwa.offline' })); -}); +const { pwa } = defaultSettings; +// if pwa is true +if (pwa) { + // Notify user if offline now + window.addEventListener('sw.offline', () => { + message.warning(formatMessage({ id: 'app.pwa.offline' })); + }); -// Pop up a prompt on the page asking the user if they want to use the latest version -window.addEventListener('sw.updated', e => { - const reloadSW = async () => { - // Check if there is sw whose state is waiting in ServiceWorkerRegistration - // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration - const worker = e.detail && e.detail.waiting; - if (!worker) { - return Promise.resolve(); - } - // Send skip-waiting event to waiting SW with MessageChannel - await new Promise((resolve, reject) => { - const channel = new MessageChannel(); - channel.port1.onmessage = event => { - if (event.data.error) { - reject(event.data.error); - } else { - resolve(event.data); - } - }; - worker.postMessage({ type: 'skip-waiting' }, [channel.port2]); + // Pop up a prompt on the page asking the user if they want to use the latest version + window.addEventListener('sw.updated', e => { + const reloadSW = async () => { + // Check if there is sw whose state is waiting in ServiceWorkerRegistration + // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration + const worker = e.detail && e.detail.waiting; + if (!worker) { + return Promise.resolve(); + } + // Send skip-waiting event to waiting SW with MessageChannel + await new Promise((resolve, reject) => { + const channel = new MessageChannel(); + channel.port1.onmessage = event => { + if (event.data.error) { + reject(event.data.error); + } else { + resolve(event.data); + } + }; + worker.postMessage({ type: 'skip-waiting' }, [channel.port2]); + }); + // Refresh current page to use the updated HTML and other assets after SW has skiped waiting + window.location.reload(true); + return true; + }; + const key = `open${Date.now()}`; + const btn = ( + + ); + notification.open({ + message: formatMessage({ id: 'app.pwa.serviceworker.updated' }), + description: formatMessage({ id: 'app.pwa.serviceworker.updated.hint' }), + btn, + key, + onClose: async () => {}, }); - // Refresh current page to use the updated HTML and other assets after SW has skiped waiting - window.location.reload(true); - return true; - }; - const key = `open${Date.now()}`; - const btn = ( - - ); - notification.open({ - message: formatMessage({ id: 'app.pwa.serviceworker.updated' }), - description: formatMessage({ id: 'app.pwa.serviceworker.updated.hint' }), - btn, - key, - onClose: async () => {}, }); -}); +} -- GitLab