Add sw_template.js to build and improve service worker loading

Updated the Vite config to copy sw_template.js to the static assets during build. Modified backend to load sw_template.js from the static directory if available, falling back to the source assets if not. This ensures the service worker template is correctly served in production builds.
This commit is contained in:
手瓜一十雪 committed 2025-12-24 18:20:51 +08:00
1 parent 0e79341c9f
commit b005df0f2a
2 files changed
+6 -1

No files matched your search

+1
View File
@@ -27,6 +27,7 @@ const ShellBaseConfigPlugin: PluginOption[] = [
targets: [
{ src: '../napcat-native/', dest: 'dist/native', flatten: false },
{ src: '../napcat-webui-frontend/dist/', dest: 'dist/static/', flatten: false },
{ src: '../napcat-webui-backend/src/assets/sw_template.js', dest: 'dist/static/' },
{ src: '../napcat-core/external/napcat.json', dest: 'dist/config/' },
{ src: '../../package.json', dest: 'dist' },
{ src: '../napcat-shell-loader', dest: 'dist' },
+5 -1
View File
@@ -200,7 +200,11 @@ export async function InitWebUi (logger: ILogWrapper, pathWrapper: NapCatPathWra
app.get('/webui/sw.js', async (_req, res) => {
try {
// 读取模板文件
const templatePath = resolve(__dirname, 'src/assets/sw_template.js');
let templatePath = resolve(__dirname, 'static', 'sw_template.js');
if (!existsSync(templatePath)) {
templatePath = resolve(__dirname, 'src', 'assets', 'sw_template.js');
}
let swContent = readFileSync(templatePath, 'utf-8');
// 替换版本号