Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): support PWA #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added packages/client/app/assets/koishi-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/client/app/assets/koishi-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions packages/client/app/sw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://vite-pwa-org.netlify.app/guide/inject-manifest.html
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching'

declare let self: ServiceWorkerGlobalScope

self.addEventListener('install', () => {
self.skipWaiting()
})

self.addEventListener('activate', () => {
self.clients.claim()
})

self.addEventListener('fetch', (event) => {
if (/(.js|.mjs|.css)$/.test(event.request.url)) {
event.respondWith(fetch(event.request))
} else {
event.respondWith(fetch(event.request, { cache: 'no-store' }))
}
})

// Cleanup Outdated Caches
cleanupOutdatedCaches()
precacheAndRoute(self.__WB_MANIFEST)

// Prompt For Update Behavior
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') self.skipWaiting()
})
1 change: 1 addition & 0 deletions packages/client/app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../tsconfig.client",
"compilerOptions": {
"rootDir": ".",
"lib": ["ESNext", "DOM", "WebWorker"]
},
"include": [
".",
Expand Down
32 changes: 31 additions & 1 deletion packages/client/build/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { resolve } from 'path'
import * as vite from 'vite'
import vue from '@vitejs/plugin-vue'
import yaml from '@maikolib/vite-plugin-yaml'
import { VitePWA as pwa } from 'vite-plugin-pwa'

function findModulePath(id: string) {
const path = require.resolve(id).replace(/\\/g, '/')
Expand All @@ -18,6 +19,7 @@ export async function build(root: string, config: vite.UserConfig = {}, isClient
const { rollupOptions = {} } = config.build || {}
return await vite.build({
root,
publicDir: 'assets',
build: {
outDir: cwd + '/plugins/console/dist',
emptyOutDir: true,
Expand Down Expand Up @@ -46,6 +48,7 @@ export async function build(root: string, config: vite.UserConfig = {}, isClient
plugins: [
vue(),
yaml(),
config.plugins,
],
resolve: {
alias: {
Expand All @@ -62,7 +65,34 @@ export async function build(root: string, config: vite.UserConfig = {}, isClient

export default async function () {
// build for console main
const { output } = await build(cwd + '/packages/client/app')
const { output } = await build(cwd + '/packages/client/app', {
plugins: [
pwa({
strategies: 'injectManifest',
srcDir: '.',
filename: 'sw.ts',
injectRegister: 'inline',
manifest: {
name: 'Koishi',
short_name: 'Koishi',
description: 'Koishi 控制台',
theme_color: '#ffffff',
icons: [
{
src: 'koishi-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'koishi-512x512.png',
sizes: '512x512',
type: 'image/png',
},
],
},
}),
],
})

await Promise.all([
copyFile(findModulePath('vue') + '/dist/vue.runtime.esm-browser.prod.js', dist + '/vue.js'),
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"ns-require": "^1.1.4",
"sass": "^1.62.1",
"vite": "^4.3.5",
"vite-plugin-pwa": "^0.14.7",
"vue": "^3.3.2",
"vue-i18n": "^9.2.2",
"vue-router": "^4.2.0"
Expand Down