Skip to content

Commit

Permalink
feat: use custom service worker
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas committed Oct 11, 2023
1 parent af23445 commit d5c3b9a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<meta name="apple-mobile-web-app-title" content="fluidd" />
<link rel="apple-touch-icon" sizes="180x180" href="/img/icons/apple-touch-icon-180x180.png" />
<link rel="mask-icon" href="/img/icons/safari-pinned-tab.svg" color="#000000" />
<meta name="msapplication-TileImage" content="/img/icons/mstile-150x150.png" />
<meta name="msapplication-TileImage" content="%BASE_URL%img/icons/mstile-150x150.png" />
<meta name="msapplication-TileColor" content="#000000" />

<script type="module" src="/src/main.ts"></script>
Expand Down
5 changes: 1 addition & 4 deletions src/store/console/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { MutationTree } from 'vuex'
import { defaultState } from './state'
import { Globals } from '@/globals'
import { ConsoleEntry, ConsoleFilter, ConsoleFilterType, ConsoleState } from './types'
import escapeRegExp from '@/util/escape-regexp'

const _compileExpression = (filter: ConsoleFilter): RegExp => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
function escapeRegExp (s: string) {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
switch (filter.type) {
case ConsoleFilterType.Contains:
return new RegExp(`.*${escapeRegExp(filter.value)}.*`, 'i')
Expand Down
55 changes: 55 additions & 0 deletions src/sw.ts
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'
)
7 changes: 7 additions & 0 deletions src/util/escape-regexp.ts
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
46 changes: 15 additions & 31 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,20 @@ import version from './vite.config.inject-version'
export default defineConfig({
plugins: [
VitePWA({
srcDir: 'src',
filename: 'sw.ts',
strategies: 'injectManifest',
includeAssets: [
'**/*.svg',
'**/*.png',
'**/*.ico',
'*.json'
],
workbox: {
injectManifest: {
globPatterns: [
'**/*.{js,css,html,ttf,woff,woff2,wasm}'
],
maximumFileSizeToCacheInBytes: 4 * 1024 ** 2,
navigateFallbackDenylist: [
/^\/websocket/,
/^\/(printer|api|access|machine|server)\//,
/^\/webcam[2-4]?\//
],
runtimeCaching: [
{
urlPattern: (options) => (options.sameOrigin && options.url.pathname.startsWith('/config.json')),
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'config',
matchOptions: {
ignoreSearch: true
},
precacheFallback: {
fallbackURL: 'config.json'
}
}
}
]
maximumFileSizeToCacheInBytes: 4 * 1024 ** 2
},
manifest: {
name: 'fluidd',
Expand All @@ -54,23 +37,23 @@ export default defineConfig({
background_color: '#000000',
icons: [
{
src: '/img/icons/android-chrome-192x192.png',
src: 'img/icons/android-chrome-192x192.png',
sizes: '192x192',
type: 'image/png'
},
{
src: '/img/icons/android-chrome-512x512.png',
src: 'img/icons/android-chrome-512x512.png',
sizes: '512x512',
type: 'image/png'
},
{
src: '/img/icons/android-chrome-maskable-192x192.png',
src: 'img/icons/android-chrome-maskable-192x192.png',
sizes: '192x192',
type: 'image/png',
purpose: 'maskable'
},
{
src: '/img/icons/android-chrome-maskable-512x512.png',
src: 'img/icons/android-chrome-maskable-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'maskable'
Expand All @@ -79,21 +62,21 @@ export default defineConfig({
shortcuts: [
{
name: 'Configuration',
url: '/#/configure',
url: '#/configure',
icons: [
{
src: '/img/icons/shortcut-configuration-96x96.png',
src: 'img/icons/shortcut-configuration-96x96.png',
sizes: '96x96',
type: 'image/png'
}
]
},
{
name: 'Settings',
url: '/#/settings',
url: '#/settings',
icons: [
{
src: '/img/icons/shortcut-settings-96x96.png',
src: 'img/icons/shortcut-settings-96x96.png',
sizes: '96x96',
type: 'image/png'
}
Expand All @@ -103,7 +86,8 @@ export default defineConfig({
},
devOptions: {
enabled: true,
type: 'module'
type: 'module',
navigateFallback: 'index.html'
}
}),
vue(),
Expand Down

0 comments on commit d5c3b9a

Please sign in to comment.