Skip to content

Commit 5ad28b4

Browse files
committed
Migrate from vite-plugin-pwa to serwist
1 parent 7649402 commit 5ad28b4

8 files changed

Lines changed: 624 additions & 400 deletions

File tree

deno.lock

Lines changed: 543 additions & 364 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta charset="utf-8"/>
66
<meta name="viewport" content="width=device-width,initial-scale=1"/>
77
<link rel="icon" href="/images/icon.ico" sizes="48x48">
8+
<link rel="manifest" href="/manifest.json">
89
<script type="module" src="/src/app.tsx"></script>
910
</head>
1011
<body>

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"@eslint/css": "^1.4.0",
3333
"@eslint/js": "^10.0.1",
3434
"@rolldown/plugin-babel": "^0.2.3",
35+
"@serwist/vite": "^9.5.11",
36+
"@serwist/window": "^9.5.11",
3537
"@stylistic/eslint-plugin": "^5.10.0",
3638
"@tailwindcss/vite": "^4.3.3",
3739
"@types/node": "^26.1.1",
@@ -51,6 +53,7 @@
5153
"react-icons": "^5.7.0",
5254
"react-router-dom": "^7.18.1",
5355
"react-textarea-autosize": "^8.5.9",
56+
"serwist": "^9.5.11",
5457
"tailwind-csstree": "^0.3.3",
5558
"tailwindcss": "^4.3.3",
5659
"typescript": "^6.0.3",

public/manifest.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "RadixVerse",
3+
"short_name": "RadixVerse",
4+
"display": "standalone",
5+
"theme_color": "#f1f1f1",
6+
"background_color": "#f1f1f1",
7+
"icons": [
8+
{
9+
"src": "images/icon-64x64.png",
10+
"sizes": "64x64",
11+
"type": "image/png"
12+
},
13+
{
14+
"src": "images/icon-192x192.png",
15+
"sizes": "192x192",
16+
"type": "image/png"
17+
},
18+
{
19+
"src": "images/icon-512x512.png",
20+
"sizes": "512x512",
21+
"type": "image/png"
22+
},
23+
{
24+
"src": "images/icon-512x512.png",
25+
"sizes": "512x512",
26+
"type": "image/png",
27+
"purpose": "maskable"
28+
}
29+
]
30+
}

src/app.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createRoot } from 'react-dom/client'
22
import { useState, useEffect } from 'react'
33
import { BrowserRouter, Routes, Route, Link, useLocation, useSearchParams } from 'react-router-dom'
44
import { ErrorBoundary, getErrorMessage } from 'react-error-boundary'
5+
import { getSerwist } from 'virtual:serwist'
56

67
import { LS_RADIXES, AppContext, getCharsLS, sanitizeInput, serializeRadixes, unserializeRadixes } from './common.ts'
78
import { type Radix, createRadixes, createRadix, num2str, str2num, allowedCharaters } from './radixes.ts'
@@ -35,6 +36,14 @@ function App() {
3536
useEffect(() => {
3637
const keyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') setError(undefined) }
3738
document.addEventListener('keydown', keyDown)
39+
40+
if ('serviceWorker' in navigator) {
41+
getSerwist().then(serwist => {
42+
serwist?.addEventListener('installed', () => { console.log('Service worker installed!') })
43+
void serwist?.register()
44+
}).catch((error: unknown) => { console.error('Failed to register service worker:', error) })
45+
}
46+
3847
return () => { document.removeEventListener('keydown', keyDown) }
3948
}, [])
4049

src/sw.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defaultCache } from '@serwist/vite/worker'
2+
import { Serwist, type PrecacheEntry, type SerwistGlobalConfig } from 'serwist'
3+
4+
declare global {
5+
interface WorkerGlobalScope extends SerwistGlobalConfig { // oxlint-disable-line consistent-type-definitions
6+
__SW_MANIFEST: (PrecacheEntry | string)[] | undefined,
7+
}
8+
}
9+
10+
declare const self: ServiceWorkerGlobalScope
11+
12+
const serwist = new Serwist({
13+
precacheEntries: self.__SW_MANIFEST,
14+
skipWaiting: true,
15+
clientsClaim: true,
16+
navigationPreload: true,
17+
runtimeCaching: defaultCache,
18+
})
19+
20+
serwist.addEventListeners()

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"erasableSyntaxOnly": true,
66
"jsx": "react-jsx",
77
"jsxImportSource": "react",
8-
"lib": [ "ESNext", "DOM" ],
8+
"lib": [ "ESNext", "DOM", "WebWorker" ],
99
"module": "nodenext",
1010
"moduleResolution": "nodenext",
1111
"noEmit": true,
1212
"noFallthroughCasesInSwitch": true,
1313
"resolveJsonModule": true,
1414
"skipLibCheck": true,
15-
"types": [ "node", "vite/client" ],
15+
"types": [ "node", "vite/client", "@serwist/vite/typings" ],
1616
"verbatimModuleSyntax": true
1717
}
1818
}

vite.config.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { defineConfig } from 'vite'
33
import react, { reactCompilerPreset } from '@vitejs/plugin-react'
44
import babel from '@rolldown/plugin-babel'
55
import tailwindcss from '@tailwindcss/vite'
6-
import { VitePWA as pwa } from 'vite-plugin-pwa'
7-
8-
const name = 'RadixVerse'
6+
// import { VitePWA as pwa } from 'vite-plugin-pwa'
7+
import { serwist } from '@serwist/vite'
98

109
export default defineConfig({
1110
base: process.env.BASE_URL ?? '/',
@@ -18,36 +17,19 @@ export default defineConfig({
1817
react(),
1918
babel({ presets: [ reactCompilerPreset() ] }),
2019
tailwindcss(),
21-
pwa({
22-
devOptions: { enabled: true },
23-
injectRegister: 'script',
24-
registerType: 'autoUpdate',
25-
includeAssets: [ 'images/*.{ico,png}' ],
26-
manifest: {
27-
name,
28-
short_name: name,
29-
display: 'standalone',
30-
theme_color: '#f1f1f1',
31-
background_color: '#f1f1f1',
32-
icons: [{
33-
src: 'images/icon-64x64.png',
34-
sizes: '64x64',
35-
type: 'image/png',
36-
}, {
37-
src: 'images/icon-192x192.png',
38-
sizes: '192x192',
39-
type: 'image/png',
40-
}, {
41-
src: 'images/icon-512x512.png',
42-
sizes: '512x512',
43-
type: 'image/png',
44-
}, {
45-
src: 'images/icon-512x512.png',
46-
sizes: '512x512',
47-
type: 'image/png',
48-
purpose: 'maskable',
49-
}]
50-
}
51-
})
20+
// pwa({
21+
// devOptions: { enabled: true },
22+
// injectRegister: 'script',
23+
// registerType: 'autoUpdate',
24+
// includeAssets: [ 'images/*.{ico,png}' ],
25+
// manifest:
26+
// })
27+
serwist({
28+
swSrc: 'src/sw.ts',
29+
swDest: 'sw.js',
30+
globDirectory: 'dist',
31+
injectionPoint: 'self.__SW_MANIFEST',
32+
rollupFormat: 'iife',
33+
}),
5234
]
5335
})

0 commit comments

Comments
 (0)