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:
手瓜一十雪 2025-12-24 18:20:51 +08:00
parent afed164ba1
commit cb8e10cc7e
2 changed files with 6 additions and 1 deletions

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' },

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');
// 替换版本号