惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
T
Tor Project blog
U
Unit 42
G
Google Developers Blog
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
B
Blog
I
InfoQ
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
小众软件
小众软件
Spread Privacy
Spread Privacy
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
I
Intezer
P
Proofpoint News Feed
A
About on SuperTechFans
S
Securelist
D
DataBreaches.Net
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
大猫的无限游戏
大猫的无限游戏
Cyberwarzone
Cyberwarzone
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
V
Visual Studio Blog
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
Project Zero
Project Zero
博客园 - 司徒正美
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
Vercel News
Vercel News
T
Threatpost
博客园 - Franky
有赞技术团队
有赞技术团队
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题

博客园 - Liaofy

前端新范式:用 AI 提效开发,用 E2E 保证迭代质量 把SPA的加载速度提升一倍,有没有搞头? 使用mumu模拟器抓包 andriod app 如何在本地给 vue-router4 扩展功能? 【eslint 插件开发】禁用 location 跳转外部链接 vue 项目使用 charles 代理线上页面到本地后显示404 避免使用单个数字作为对象的key 高扩展弹出层组件设计实现(H5&小程序) 轻松搞懂POST与PUT的区别 vite(or rollup) 实现 webpackChunkName webapp项目架构畅想(vue3).md vite试玩:老项目启动从6分钟到秒开 修剪AST树减少webapck打包尺寸 【uniapp】改善中大型uniapp小程序项目开发体验 vscode 调试 webpack 打包 5分钟,搞清 Unicode 与 UTF-8 的区别 deAsync.js 真正让异步变同步 盘的转——使用缓动函数完成动画 多个请求间断失败,如何优雅处理?
Vue3 路由优化,使页面初次渲染效率翻倍
Liaofy · 2023-08-08 · via 博客园 - Liaofy

3996 条路由?

addRoute函数用了大约1s才执行完毕。通过观察,发现居然有3996条路由记录。

可是项目并没有这么多的页面啊~

重复路由

let routes: Array<RouteRecordRaw> = [
  {
    path: '/promotion/ticket-list-jegotrip',
    component: () =>
      import(
        /* webpackChunkName: "promotion/ticket-list-jegotrip" */ './pages/ticket-list-jegotrip/index.vue'
      ),
  },
]

const routerFiles = import.meta.globEager('./**/routes.ts')
Object.keys(routerFiles).forEach((key) => {
  routes = [...routes, ...routerFiles[key].default]
  routes.forEach((item) => {
    router.addRoute(item)
  })
})

仔细看,这里的router.addRoute会重复添加路由,由于大部分路由没有填写name,使得addRoute的时候不会自动去重。

修复这个问题倒也简单:

routes.forEach((item) => router.addRoute(item))

const routesFiles = import.meta.globEager('./**/routes.ts')
Object.keys(routesFiles).forEach((key) => {
  const moduleRoutes = routesFiles[key].default || []
  moduleRoutes.forEach((item) => {
    router.addRoute(item)
  })
})

如果真有上千条路由怎么优化呢?

答案还是addRoute。只不过我们不在初始化阶段就添加全部路由,而是在需要的时候添加,就是beforeEach中添加:

const routesFiles = import.meta.globEager('./**/routes.ts')
const allRoutes = Object.keys(routesFiles)
  .map((key) => routesFiles[key].default || [])
  .flat(Infinity)

router.beforeEach((to, from, next) => {
  const { path } = router.resolve(to)

  if (hasAddRoute(path)) return next()

  const matchedRoute = allRoutes.find(i => i.path === path)
  if (!matchedRoute) throw `${path} 不存在`

  router.addRoute(matchedRoute)
  // https://router.vuejs.org/zh/guide/advanced/dynamic-routing.html
  return to.fullPath
})

这里还有一些优化点:

  1. 如何使 hasAddRoute 的效率更高,比如做到时间复杂度为O(1)?
  2. 打开一个页面后,能否预测下一个页面的路由进而提前添加,减少页面跳转时延?
  3. 能否减少路由(allRoutes)生成时间?

等咱们项目真增长到这个程度再说吧,哈哈哈哈~