From 065c6ca2fc7adf1d543c2e7619868ce91a1c0945 Mon Sep 17 00:00:00 2001 From: hwj Date: Mon, 13 Jul 2026 15:30:36 +0800 Subject: [PATCH] =?UTF-8?q?perf=EF=BC=9A=E9=A6=96=E5=B1=8F=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=B1=8F=E8=94=BDvue-plugin-hiprint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main.ts b/src/main.ts index dd9f4e50..34fac376 100644 --- a/src/main.ts +++ b/src/main.ts @@ -42,6 +42,28 @@ import Logger from '@/utils/Logger' import VueDOMPurifyHTML from 'vue-dompurify-html' // 解决v-html 的安全隐患 +// 空闲时静默加载打印插件(vue-plugin-hiprint 体积较大且内含 jQuery)。 +// 等浏览器空闲后再动态加载,避免与首屏关键请求争抢连接/带宽而拖慢首屏。 +const setupHiprintWhenIdle = (app: ReturnType) => { + const load = () => { + import('vue-plugin-hiprint') + .then(({ hiPrintPlugin }) => { + app.use(hiPrintPlugin, '$hiprint', false) + hiPrintPlugin.disAutoConnect() + }) + .catch((e) => { + console.error('[hiprint] 打印插件加载失败:', e) + }) + } + // 优先使用 requestIdleCallback,在浏览器空闲时加载;不支持则延迟兜底 + const ric = (window as any).requestIdleCallback + if (typeof ric === 'function') { + ric(load, { timeout: 5000 }) + } else { + setTimeout(load, 3000) + } +} + // 创建实例 const setupAll = async () => { const app = createApp(App) @@ -68,16 +90,8 @@ const setupAll = async () => { app.mount('#app') - // 静默异步加载打印插件(vue-plugin-hiprint 体积较大且内含 jQuery), - // 放到 mount 之后动态加载,避免其 chunk 阻塞首屏渲染与后续请求 - import('vue-plugin-hiprint') - .then(({ hiPrintPlugin }) => { - app.use(hiPrintPlugin, '$hiprint', false) - hiPrintPlugin.disAutoConnect() - }) - .catch((e) => { - console.error('[hiprint] 打印插件加载失败:', e) - }) + // 首屏渲染后,等浏览器空闲再静默加载打印插件 + setupHiprintWhenIdle(app) } setupAll()