feat(web): pwa offline caching, dockerized SPA, full-stack nginx smoke

This commit is contained in:
2026-09-07 16:33:50 +08:00
parent 1bd7688ee6
commit 96e96fec51
11 changed files with 133 additions and 8 deletions
+4
View File
@@ -47,6 +47,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
setToken(null);
setTok(null);
qc.clear();
if (typeof caches !== "undefined") {
void caches.delete("booklib-api");
void caches.delete("booklib-immutable");
}
},
}),
[meQ.data, meQ.isPending, meQ.isError, token, qc],
+1
View File
@@ -4,6 +4,7 @@ import { createRoot } from "react-dom/client";
import App from "./App";
import { queryClient } from "./lib/qc";
import "./index.css";
import "./pwa";
createRoot(document.getElementById("root")!).render(
<QueryClientProvider client={queryClient}>
+4
View File
@@ -0,0 +1,4 @@
import { registerSW } from "virtual:pwa-register";
// autoUpdate:新版本静默装好即用;个人应用无需升级确认弹窗
registerSW({ immediate: true });
+1 -1
View File
@@ -12,7 +12,7 @@
"isolatedModules": true,
"skipLibCheck": true,
"noEmit": true,
"types": ["vite/client"]
"types": ["vite/client", "vite-plugin-pwa/client"]
},
"include": ["src", "test"]
}
+44 -2
View File
@@ -1,8 +1,50 @@
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import { VitePWA } from "vite-plugin-pwa";
import tailwindcss from "@tailwindcss/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [react(), tailwindcss()],
plugins: [
react(),
tailwindcss(),
VitePWA({
registerType: "autoUpdate",
includeAssets: ["icon.svg"],
manifest: {
name: "BookLib 个人书库",
short_name: "BookLib",
description: "个人书库/漫画库在线阅读",
theme_color: "#09090b",
background_color: "#09090b",
display: "standalone",
icons: [{ src: "icon.svg", sizes: "any", type: "image/svg+xml", purpose: "any" }],
},
workbox: {
globPatterns: ["**/*.{js,css,html,svg,woff2}"],
navigateFallback: "/index.html",
runtimeCaching: [
{
// ?v={hash} 的封面/CBZ 页/原文件 = 不可变 → cache-first(spec §6.1/§6.3)
urlPattern: ({ url }) =>
url.pathname.startsWith("/api/books/") &&
url.search.includes("v=") &&
(/\/cover$/.test(url.pathname) || /\/pages\/\d+$/.test(url.pathname) || /\/file$/.test(url.pathname)),
handler: "CacheFirst",
options: { cacheName: "booklib-immutable", expiration: { maxEntries: 3000, purgeOnQuotaError: true } },
},
{
// 普通 API 请求 network-first,离线可读最近一次列表/进度
urlPattern: ({ url }) => url.pathname.startsWith("/api/"),
handler: "NetworkFirst",
options: {
cacheName: "booklib-api",
networkTimeoutSeconds: 5,
expiration: { maxAgeSeconds: 3600, maxEntries: 200 },
},
},
],
},
}),
],
server: { proxy: { "/api": "http://localhost:8080" } },
});