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()