committed by
陈帅
16 changed files with 163 additions and 3 deletions
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
@ -0,0 +1,39 @@ |
|||||
|
import { Modal, message } from 'antd'; |
||||
|
import { formatMessage } from 'umi/locale'; |
||||
|
|
||||
|
// 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 => { |
||||
|
Modal.confirm({ |
||||
|
title: formatMessage({ id: 'app.pwa.serviceworker.updated' }), |
||||
|
content: formatMessage({ id: 'app.pwa.serviceworker.updated.hint' }), |
||||
|
okText: formatMessage({ id: 'app.pwa.serviceworker.updated.ok' }), |
||||
|
onOk: 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; |
||||
|
}, |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,6 @@ |
|||||
|
export default { |
||||
|
'app.pwa.offline': 'You are offline now', |
||||
|
'app.pwa.serviceworker.updated': 'New content is available', |
||||
|
'app.pwa.serviceworker.updated.hint': 'Please press the "Refresh" button to reload current page', |
||||
|
'app.pwa.serviceworker.updated.ok': 'Refresh', |
||||
|
}; |
||||
@ -0,0 +1,7 @@ |
|||||
|
export default { |
||||
|
'app.pwa.offline': 'Você está offline agora', |
||||
|
'app.pwa.serviceworker.updated': 'Novo conteúdo está disponível', |
||||
|
'app.pwa.serviceworker.updated.hint': |
||||
|
'Por favor, pressione o botão "Atualizar" para recarregar a página atual', |
||||
|
'app.pwa.serviceworker.updated.ok': 'Atualizar', |
||||
|
}; |
||||
@ -0,0 +1,6 @@ |
|||||
|
export default { |
||||
|
'app.pwa.offline': '当前处于离线状态', |
||||
|
'app.pwa.serviceworker.updated': '有新内容', |
||||
|
'app.pwa.serviceworker.updated.hint': '请点击“刷新”按钮或者手动刷新页面', |
||||
|
'app.pwa.serviceworker.updated.ok': '刷新', |
||||
|
}; |
||||
@ -0,0 +1,6 @@ |
|||||
|
export default { |
||||
|
'app.pwa.offline': '當前處於離線狀態', |
||||
|
'app.pwa.serviceworker.updated': '有新內容', |
||||
|
'app.pwa.serviceworker.updated.hint': '請點擊“刷新”按鈕或者手動刷新頁面', |
||||
|
'app.pwa.serviceworker.updated.ok': '刷新', |
||||
|
}; |
||||
@ -0,0 +1,17 @@ |
|||||
|
{ |
||||
|
"name": "ant-design-pro", |
||||
|
"short_name": "antd-pro", |
||||
|
"display": "standalone", |
||||
|
"start_url": "./?utm_source=homescreen", |
||||
|
"theme_color": "#002140", |
||||
|
"background_color": "#001529", |
||||
|
"icons": [ |
||||
|
{ |
||||
|
"src": "icons/icon-192x192.png", |
||||
|
"sizes": "192x192" |
||||
|
},{ |
||||
|
"src": "icons/icon-128x128.png", |
||||
|
"sizes": "128x128" |
||||
|
} |
||||
|
] |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
/* globals workbox */ |
||||
|
/* eslint-disable no-restricted-globals */ |
||||
|
workbox.core.setCacheNameDetails({ |
||||
|
prefix: 'antd-pro', |
||||
|
suffix: 'v1', |
||||
|
}); |
||||
|
// Control all opened tabs ASAP
|
||||
|
workbox.clientsClaim(); |
||||
|
|
||||
|
/** |
||||
|
* Use precaching list generated by workbox in build process. |
||||
|
* https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.precaching
|
||||
|
*/ |
||||
|
/* eslint-disable no-underscore-dangle */ |
||||
|
workbox.precaching.precacheAndRoute(self.__precacheManifest || []); |
||||
|
|
||||
|
/** |
||||
|
* Register a navigation route. |
||||
|
* https://developers.google.com/web/tools/workbox/modules/workbox-routing#how_to_register_a_navigation_route
|
||||
|
*/ |
||||
|
workbox.routing.registerNavigationRoute('/index.html'); |
||||
|
|
||||
|
/** |
||||
|
* Use runtime cache: |
||||
|
* https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.routing#.registerRoute
|
||||
|
* |
||||
|
* Workbox provides all common caching strategies including CacheFirst, NetworkFirst etc. |
||||
|
* https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.strategies
|
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* Handle API requests |
||||
|
*/ |
||||
|
workbox.routing.registerRoute(/\/api\//, workbox.strategies.networkFirst()); |
||||
|
|
||||
|
/** |
||||
|
* Handle third party requests |
||||
|
*/ |
||||
|
workbox.routing.registerRoute( |
||||
|
/^https:\/\/gw.alipayobjects.com\//, |
||||
|
workbox.strategies.networkFirst() |
||||
|
); |
||||
|
workbox.routing.registerRoute( |
||||
|
/^https:\/\/cdnjs.cloudflare.com\//, |
||||
|
workbox.strategies.networkFirst() |
||||
|
); |
||||
|
workbox.routing.registerRoute(/\/color.less/, workbox.strategies.networkFirst()); |
||||
|
|
||||
|
/** |
||||
|
* Response to client after skipping waiting with MessageChannel |
||||
|
*/ |
||||
|
addEventListener('message', event => { |
||||
|
const replyPort = event.ports[0]; |
||||
|
const message = event.data; |
||||
|
if (replyPort && message && message.type === 'skip-waiting') { |
||||
|
event.waitUntil( |
||||
|
self |
||||
|
.skipWaiting() |
||||
|
.then( |
||||
|
() => replyPort.postMessage({ error: null }), |
||||
|
error => replyPort.postMessage({ error }) |
||||
|
) |
||||
|
); |
||||
|
} |
||||
|
}); |
||||
Loading…
Reference in new issue