xugaoyi / vuepress-theme-vdoing

🚀一款简洁高效的VuePress知识管理&博客(blog)主题
https://doc.xugaoyi.com
MIT License
4.45k stars 1.14k forks source link

[bug]: 大page+大sidebar下打包卡住 #582

Open 13588936722 opened 2 years ago

13588936722 commented 2 years ago

问题描述

大page+大sidebar下打包卡住

  1. page数量在800页,sidebar也是800,层级5级
  2. 打包卡在Rendering static HTML,一直卡着

重现步骤

只要page到了800页,以及sidebar是自动读取目录生成的,打包就会出现这个bug。

重现链接(如有)

问题截图(如有)

依赖版本

补充说明(可选)

13588936722 commented 2 years ago

问题原因: vdoing/util/index.js 文件的 resolveSidebarItems 函数被无限的调用(7517513003 ~= 17亿次),而且resolvePage返回的数据太大了,导致打包卡住了。

页面数:751 resolveSidebarItems 调用次数:3003

13588936722 commented 2 years ago

进一步分析: resolveSidebarItems 这个函数的获取sidebar的时候,是幂等的,只跟regularPath以及pages有关系,pages是页面数量,贯穿全局的不变量,regularPath 是页面当前的地址,但是通过regularPath 解析出来的base、config是固定的几种,所以可以使用缓存来缓解这个压力。

13588936722 commented 2 years ago

最终代码如下: ` const sideBarCacheMap = new Map() let count = 0 export function resolveSidebarItems (page, regularPath, site, localePath) { console.log('count===', count++) const { pages, themeConfig } = site

const localeConfig = localePath && themeConfig.locales ? themeConfig.locales[localePath] || themeConfig : themeConfig

const pageSidebarConfig = page.frontmatter.sidebar || localeConfig.sidebar || themeConfig.sidebar if (pageSidebarConfig === 'auto') { return resolveHeaders(page) }

const sidebarConfig = localeConfig.sidebar || themeConfig.sidebar if (!sidebarConfig) { return [] } else { const { base, config } = resolveMatchingConfig(regularPath, sidebarConfig) if (config === 'auto') { return resolveHeaders(page) } if (!config) { return [] } if (sideBarCacheMap.has(base)) { return sideBarCacheMap.get(base) } sideBarCacheMap.set(base, config.map(item => resolveItem(item, pages, base))) return sideBarCacheMap.get(base) } } `

13588936722 commented 2 years ago

因为github的太难配了,本地又有gitlab,搞不清楚双账户的配置。 @xugaoyi 你修复一下吧

13588936722 commented 2 years ago

@xugaoyi 靓仔,咋说

xugaoyi commented 2 years ago

暂时还没时间做测试,晚些时候再看。你先用本地主题包🤪

xugaoyi commented 2 years ago

好奇你的网站是什么内容?有超过800个页

13588936722 commented 2 years ago
  1. 你直接测试下执行次数呗。
  2. 公司文档迁移。