chore: add pwa
This commit is contained in:
@@ -83,6 +83,36 @@ func initStaticRoutes(root *gin.RouterGroup) {
|
|||||||
root.GET("/logo.svg", func(ctx *gin.Context) {
|
root.GET("/logo.svg", func(ctx *gin.Context) {
|
||||||
serveSingleFile(ctx, "logo.svg", "image/svg+xml", "public, max-age=86400")
|
serveSingleFile(ctx, "logo.svg", "image/svg+xml", "public, max-age=86400")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// PWA 相关路由处理
|
||||||
|
initPWARoutes(root)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initPWARoutes(root *gin.RouterGroup) {
|
||||||
|
// PWA 相关文件处理
|
||||||
|
pwaRootFiles := map[string]string{
|
||||||
|
"/sw.js": "application/javascript",
|
||||||
|
"/registerSW.js": "application/javascript",
|
||||||
|
"/manifest.webmanifest": "application/manifest+json",
|
||||||
|
"/favicon.ico": "image/x-icon",
|
||||||
|
"/pwa-icon-192.png": "image/png",
|
||||||
|
"/pwa-icon-512.png": "image/png",
|
||||||
|
}
|
||||||
|
|
||||||
|
for path, contentType := range pwaRootFiles {
|
||||||
|
pPath := path
|
||||||
|
pType := contentType
|
||||||
|
root.GET(pPath, func(ctx *gin.Context) {
|
||||||
|
file := strings.TrimPrefix(pPath, "/")
|
||||||
|
serveSingleFile(ctx, file, pType, "public, no-cache")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 动态匹配 workbox-*.js (Vite PWA 生成的库文件)
|
||||||
|
root.GET("/workbox-:hash.js", func(ctx *gin.Context) {
|
||||||
|
file := "workbox-" + ctx.Param("hash") + ".js"
|
||||||
|
serveSingleFile(ctx, file, "application/javascript", "public, max-age=31536000, immutable")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveSingleFile(ctx *gin.Context, filename string, contentType string, cache string) {
|
func serveSingleFile(ctx *gin.Context, filename string, contentType string, cache string) {
|
||||||
|
|||||||
Generated
+4782
File diff suppressed because it is too large
Load Diff
@@ -49,6 +49,7 @@
|
|||||||
"tw-animate-css": "^1.4.0",
|
"tw-animate-css": "^1.4.0",
|
||||||
"typescript": "~5.9.3",
|
"typescript": "~5.9.3",
|
||||||
"vite": "^7.3.1",
|
"vite": "^7.3.1",
|
||||||
|
"vite-plugin-pwa": "^1.2.0",
|
||||||
"vite-plugin-static-copy": "^2.3.0",
|
"vite-plugin-static-copy": "^2.3.0",
|
||||||
"vue-tsc": "^3.1.4"
|
"vue-tsc": "^3.1.4"
|
||||||
},
|
},
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 546 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 546 KiB |
+39
-1
@@ -2,6 +2,7 @@ import { defineConfig } from 'vite'
|
|||||||
import vue from '@vitejs/plugin-vue'
|
import vue from '@vitejs/plugin-vue'
|
||||||
import tailwindcss from '@tailwindcss/vite'
|
import tailwindcss from '@tailwindcss/vite'
|
||||||
import { viteStaticCopy } from 'vite-plugin-static-copy'
|
import { viteStaticCopy } from 'vite-plugin-static-copy'
|
||||||
|
import { VitePWA } from 'vite-plugin-pwa'
|
||||||
import { fileURLToPath, URL } from 'node:url'
|
import { fileURLToPath, URL } from 'node:url'
|
||||||
import { writeFileSync, readFileSync, readdirSync, statSync, existsSync, unlinkSync } from 'node:fs'
|
import { writeFileSync, readFileSync, readdirSync, statSync, existsSync, unlinkSync } from 'node:fs'
|
||||||
import { join, resolve } from 'node:path'
|
import { join, resolve } from 'node:path'
|
||||||
@@ -55,7 +56,44 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}),
|
}),
|
||||||
compressionPlugin()
|
compressionPlugin(),
|
||||||
|
VitePWA({
|
||||||
|
registerType: 'autoUpdate',
|
||||||
|
injectRegister: 'auto',
|
||||||
|
includeAssets: ['favicon.ico', 'logo.svg', 'pwa-icon-192.png', 'pwa-icon-512.png'],
|
||||||
|
manifest: {
|
||||||
|
name: 'Baihu Panel',
|
||||||
|
short_name: 'Baihu',
|
||||||
|
description: '白虎面板 - 现代化的服务器管理面板',
|
||||||
|
theme_color: '#ffffff',
|
||||||
|
background_color: '#ffffff',
|
||||||
|
display: 'standalone',
|
||||||
|
icons: [
|
||||||
|
{
|
||||||
|
src: 'pwa-icon-192.png',
|
||||||
|
sizes: '192x192',
|
||||||
|
type: 'image/png'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: 'pwa-icon-512.png',
|
||||||
|
sizes: '512x512',
|
||||||
|
type: 'image/png'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: 'pwa-icon-512.png',
|
||||||
|
sizes: '512x512',
|
||||||
|
type: 'image/png',
|
||||||
|
purpose: 'any maskable'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
workbox: {
|
||||||
|
maximumFileSizeToCacheInBytes: 10 * 1024 * 1024 // 10MB to allow monaco-editor files
|
||||||
|
},
|
||||||
|
devOptions: {
|
||||||
|
enabled: true
|
||||||
|
}
|
||||||
|
})
|
||||||
],
|
],
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
|
|||||||
Reference in New Issue
Block a user