-
-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pedro Lamas <[email protected]>
- Loading branch information
1 parent
af23445
commit d5c3b9a
Showing
5 changed files
with
79 additions
and
36 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
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,55 @@ | ||
/// <reference lib="WebWorker" /> | ||
|
||
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute, PrecacheFallbackPlugin } from 'workbox-precaching' | ||
import { NavigationRoute, registerRoute } from 'workbox-routing' | ||
import { StaleWhileRevalidate } from 'workbox-strategies' | ||
import escapeRegExp from '@/util/escape-regexp' | ||
|
||
declare let self: ServiceWorkerGlobalScope | ||
|
||
self.addEventListener('message', (event) => { | ||
if (event.data && event.data.type === 'SKIP_WAITING') { | ||
self.skipWaiting() | ||
} | ||
}) | ||
|
||
precacheAndRoute(self.__WB_MANIFEST) | ||
|
||
cleanupOutdatedCaches() | ||
|
||
const escapedBaseUrl = escapeRegExp(import.meta.env.BASE_URL) | ||
|
||
const denylist = import.meta.env.DEV | ||
? undefined | ||
: [ | ||
'websocket', | ||
'(printer|api|access|machine|server)/', | ||
'webcam[2-4]?/' | ||
].map(item => new RegExp(`^${escapedBaseUrl}${item}`)) | ||
|
||
const allowlist = import.meta.env.DEV | ||
? [/^\/$/] | ||
: undefined | ||
|
||
registerRoute( | ||
new NavigationRoute(createHandlerBoundToURL(`${import.meta.env.BASE_URL}index.html`), { | ||
allowlist, | ||
denylist | ||
}) | ||
) | ||
|
||
registerRoute( | ||
({ sameOrigin, url }) => sameOrigin && url.pathname === `${import.meta.env.BASE_URL}config.json`, | ||
new StaleWhileRevalidate({ | ||
cacheName: 'config', | ||
matchOptions: { | ||
ignoreSearch: true | ||
}, | ||
plugins: [ | ||
new PrecacheFallbackPlugin({ | ||
fallbackURL: `${import.meta.env.BASE_URL}config.json` | ||
}) | ||
] | ||
}), | ||
'GET' | ||
) |
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,7 @@ | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping | ||
|
||
const escapeRegExp = (value: string) => { | ||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
} | ||
|
||
export default escapeRegExp |
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