forked from arco-design/arco-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add add pwa support for react docs (arco-design#1172)
- Loading branch information
Showing
9 changed files
with
2,205 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
cd site | ||
yarn | ||
|
||
cd .. | ||
yarn | ||
yarn icon | ||
yarn build | ||
|
||
cd site | ||
yarn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const path = require('path'); | ||
const WorkboxWebpackPlugin = require('workbox-webpack-plugin'); | ||
|
||
exports.getPWAConfig = (config, env) => { | ||
const isProd = env === 'prod'; | ||
|
||
if (isProd) { | ||
config.plugins.push( | ||
new WorkboxWebpackPlugin.InjectManifest({ | ||
swSrc: path.resolve(__dirname, '../src/serviceWorker.js'), | ||
dontCacheBustURLsMatching: /\.[0-9a-f]{8}\./, | ||
maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, | ||
}) | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { clientsClaim } from 'workbox-core'; | ||
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'; | ||
import { registerRoute } from 'workbox-routing'; | ||
|
||
clientsClaim(); | ||
|
||
const manifest = self.__WB_MANIFEST; | ||
|
||
const _manifest = manifest.map((item) => { | ||
const { revision, url } = item; | ||
// html won't be served through cdn, stripe out publicPath | ||
if (url && /.html$/.test(url)) { | ||
const paths = url.split('/'); | ||
return { | ||
revision, | ||
url: `/${paths[paths.length - 1]}`, | ||
}; | ||
} | ||
return item; | ||
}); | ||
|
||
precacheAndRoute(_manifest); | ||
|
||
// fallback for history routes | ||
const handlers = [ | ||
{ | ||
regexp: /^(\/(react|docs|showcase))\/en-US/, | ||
dest: '/react-en.html', | ||
}, | ||
{ | ||
regexp: /^(\/(react|docs|showcase))(?!\/en\-US)/, | ||
dest: '/index.html', | ||
}, | ||
]; | ||
|
||
const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); | ||
|
||
handlers.forEach((handler) => { | ||
const { regexp, dest } = handler; | ||
registerRoute(({ request, url }) => { | ||
if (request.mode !== 'navigate') { | ||
return false; | ||
} | ||
const pathname = url.pathname; | ||
|
||
if (pathname.match(fileExtensionRegexp)) { | ||
return false; | ||
} | ||
|
||
return regexp.test(pathname); | ||
}, createHandlerBoundToURL(dest)); | ||
}); | ||
|
||
// trigger skipWaiting via message | ||
self.addEventListener('message', (event) => { | ||
if (event.data && event.data.type === 'SKIP_WAITING') { | ||
self.skipWaiting(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from 'react'; | ||
import { Notification, Button } from '@arco-design/web-react'; | ||
|
||
let refreshing; | ||
|
||
function invokeServiceWorkerUpdateFlow(config, registration) { | ||
const id = 'notification'; | ||
// there is a new service worker available, show the notification | ||
Notification.info({ | ||
id, | ||
closable: true, | ||
duration: 0, | ||
content: config.content, | ||
btn: ( | ||
<span> | ||
<Button | ||
type="secondary" | ||
size="small" | ||
onClick={() => Notification.remove(id)} | ||
style={{ marginRight: 12 }} | ||
> | ||
{config.cancelText} | ||
</Button> | ||
<Button | ||
type="primary" | ||
size="small" | ||
onClick={() => { | ||
if (registration.waiting) { | ||
registration.waiting.postMessage({ type: 'SKIP_WAITING' }); | ||
Notification.remove(id); | ||
} | ||
}} | ||
> | ||
{config.okText} | ||
</Button> | ||
</span> | ||
), | ||
position: 'bottomRight', | ||
}); | ||
} | ||
|
||
export function registerServiceWorker(config) { | ||
if ('serviceWorker' in navigator) { | ||
window.addEventListener('load', async () => { | ||
const swUrl = '/serviceWorker.js'; | ||
navigator.serviceWorker | ||
.register(swUrl) | ||
.then((registration) => { | ||
if (registration.waiting) { | ||
invokeServiceWorkerUpdateFlow(config, registration); | ||
} | ||
|
||
registration.addEventListener('updatefound', () => { | ||
const installingWorker = registration.installing; | ||
if (installingWorker == null) { | ||
return; | ||
} | ||
installingWorker.addEventListener('statechange', () => { | ||
if (registration.waiting) { | ||
// if there's an existing controller (previous Service Worker), show the prompt | ||
if (navigator.serviceWorker.controller) { | ||
invokeServiceWorkerUpdateFlow(config, registration); | ||
} | ||
} | ||
}); | ||
}); | ||
}) | ||
.catch((error) => { | ||
console.error('Error during service worker registration:', error); | ||
}); | ||
|
||
// avoid reload on first time | ||
const oldSw = (await navigator.serviceWorker.getRegistration())?.active?.state; | ||
navigator.serviceWorker.addEventListener('controllerchange', async () => { | ||
if (refreshing) return; | ||
const newSw = (await navigator.serviceWorker.getRegistration())?.active?.state; | ||
if (oldSw === 'activated' && newSw === 'activating') { | ||
// reload on controller change | ||
window.location.reload(); | ||
refreshing = true; | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export function unregisterServiceWorker() { | ||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.ready | ||
.then((registration) => { | ||
registration.unregister(); | ||
}) | ||
.catch((error) => { | ||
console.error(error.message); | ||
}); | ||
} | ||
} |
Oops, something went wrong.