当前位置: 首页 > news >正文

字玩FontPlayer开发笔记4 性能优化 首屏加载时间优化

字玩FontPlayer开发笔记4 性能优化 首屏加载时间优化

字玩FontPlayer是笔者开源的一款字体设计工具,使用Vue3 + ElementUI开发,源代码:
github: https://github.com/HiToysMaker/fontplayer
gitee: https://gitee.com/toysmaker/fontplayer

笔记

最近把本地开发已久的项目部署到阿里云上,但是由于笔者买的套餐带宽有限,加载速度非常慢,而SPA又将所有js打包到一个文件中,导致首屏白屏很久。但是提高带宽又比较贵目前还没有必要。无奈之下,我将项目在线体验部署到Github Pages上,暂时速度可以接受。但是首屏加载时间的优化(主要集中在打包js大小)还是个棘手的问题,需要好好研究。目前我采取的方法有:

1. FontAweSome按需加载

原来加载了全部FontAweSome图标:
(main.ts中代码)

import { library } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { fas } from '@fortawesome/free-solid-svg-icons'
import { far } from '@fortawesome/free-regular-svg-icons'
import { fab } from '@fortawesome/free-brands-svg-icons'library.add(fas, far, fab)

现在替换成:
(main.ts中代码)

import { library } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import {faArrowPointer,faCircle,faPercent,faArrowDownWideShort,faPenNib,faSquare,faDrawPolygon,faImage,faFont,faTerminal,faSliders,faTableCells,
} from '@fortawesome/free-solid-svg-icons'
import {faHand,faSquare as faSquare_regular,faCircle as faCircle_regular,
} from '@fortawesome/free-regular-svg-icons'
library.add(faArrowPointer,faCircle,faPercent,faArrowDownWideShort,faPenNib,faSquare,faDrawPolygon,faImage,faFont,faTerminal,faSliders,faTableCells,faHand,faSquare_regular,faCircle_regular,
)

2. Element Icon按需引入

原来加载了全部图标:
(main.ts中代码)

import * as ElementPlusIconsVue from '@element-plus/icons-vue'for (const [key, component] of Object.entries(ElementPlusIconsVue)) {app.component(key, component)
}

现在替换成:
(main.ts中代码)

import { Files, Edit, Upload, Download, Tickets, Setting, List, Tools } from '@element-plus/icons-vue'app.component('Files', Files)
app.component('Edit', Edit)
app.component('Upload', Upload)
app.component('Download', Download)
app.component('Tickets', Tickets)
app.component('Setting', Setting)
app.component('List', List)
app.component('Tools', Tools)

3. 将首屏不用的代码放到最后,防止阻塞页面

笔者项目中引入了opencv.js:

<script src="lib/opencv.js"></script>

opencv.js是个比较大的库,首屏也不需要用到,就放到了html最底端

4. 加载首屏loading动画

在首屏加载文件白屏时,加一个loading动画可以提升用户体验,注意loading动画的css最好放在html头部,而不能放在外部style.css中防止loading字样已经显示,而动画样式还没有加载。

在src/index.html中,添加以下代码:

<div class="empty" style="position: absolute;z-index: -99;top: 0;left: 0;right: 0;bottom: 0;display: flex;flex-direction: column;align-items: center;justify-content: center;
"><div class="enpty-tips">首次打开可能会有些慢,感谢耐心等待</div><div class="empty-loading" style="display: flex;flex-direction: row;gap: 20px;align-items: center;justify-content: center;margin-top: 48px;"><div class="loading-1" style="width: 14px;height: 14px;border-radius: 50%;background-color: black;"></div><div class="loading-2" style="width: 14px;height: 14px;border-radius: 50%;background-color: black;"></div><div class="loading-3" style="width: 14px;height: 14px;border-radius: 50%;background-color: black;"></div></div>
</div>
<style>body, html {width: 100%;height: 100%;position: relative;padding: 0;margin: 0;overflow: hidden;}.empty {.empty-loading {.loading-1 {animation: loading-1 2s;animation-iteration-count: infinite;}.loading-2 {animation: loading-2 2s;animation-iteration-count: infinite;}.loading-3 {animation: loading-3 2s;animation-iteration-count: infinite;}}}@keyframes loading-1 {0% {opacity: 1;}25% {opacity: 0.5;}50% {opacity: 1;}}@keyframes loading-2 {0% {opacity: 1;}50% {opacity: 0.5;}75% {opacity: 1;}}@keyframes loading-3 {0% {opacity: 1;}75% {opacity: 0.5;}100% {opacity: 1;}}
</style>

5. treeshaking

vite自动支持treeshaking,可以将程序中没有引用的模块清除掉,减小最终打包文件大小。

6. 可视化检查

可以使用rollup-plugin-visualizer库可视化查看各个模块大小,进行进一步排查处理。
在vite.config.ts中设置如下:

import { visualizer } from 'rollup-plugin-visualizer'export default defineConfig({plugins: [vue(),visualizer({open: true,})],//...
})

运行后会自动生成可视化分析图。

首屏加载时间性能优化是个需要深入研究的问题,笔者在这方面还非常初级,也非常渴望学习,解决项目中的问题,非常欢迎交流讨论!

http://www.lryc.cn/news/516209.html

相关文章:

  • RabbitMQ案例
  • 智能工厂的设计软件 应用场景的一个例子:为AI聊天工具添加一个知识系统 之13 方案再探之4:特定于领域的模板 之 div模型(完整版)
  • WebRtc02:WebRtc架构、目录结构、运行机制
  • 数据结构复习 (顺序查找,对半查找,斐波那契查找,插值查找,分块查找)
  • el-input输入框需要支持多输入,最后传输给后台的字段值以逗号分割
  • C# 枚举格式字符串
  • 【51单片机-零基础chapter1】
  • 记录:导出功能:接收文件流数据进行导出(vue3)
  • 基于Spring Boot + Vue3实现的在线汽车保养维修预约管理系统源码+文档
  • PHP框架+gatewayworker实现在线1对1聊天--接收消息(7)
  • 18.1、网络安全策略分类 流程 内容
  • 深入理解连接池:从数据库到HTTP的优化之道
  • 【2025最新计算机毕业设计】基于SpringBoot+Vue智慧养老医护系统(高质量源码,提供文档,免费部署到本地)【提供源码+答辩PPT+文档+项目部署】
  • 关于使用vue-cropperjs上传一张图后,再次上传时,裁剪的图片不更新的问题
  • 学习threejs,导入VTK格式的模型
  • 大麦抢票科技狠活
  • PostgreSQL 表达式
  • WPF区域导航+导航参数使用+路由守卫+导航日志
  • Springboot启动报错:Failed to start bean ‘documentationPluginsBootstrapper‘
  • qt-C++笔记之动画框架(Qt Animation Framework)入门
  • C++26 函数契约(Contract)概览
  • Flink CDC 自定义函数处理 SQLServer XML类型数据 映射 doris json字段方案
  • F.interpolate函数
  • 华为交换机---自动备份配置到指定ftp/sftp服务器
  • nginx学习之路-nginx配置https服务器
  • UCAS 24秋网络认证技术 CH10 SSL 复习
  • 【linux内核分析-存储】EXT4源码分析之“文件删除”原理【七万字超长合并版】(源码+关键细节分析)
  • 代码随想录 day62 第十一章 图论part11
  • springboot571基于协同过滤算法的私人诊所管理系统(论文+源码)_kaic
  • Uniapp Android 本地离线打包(详细流程)