{"slug":"improve-dev-speed","title":"俗话都说项目写久了会变成史山...今天我们来铲史...","description":"什么？我只想改一个字看看预览都要等1分钟？这是什么神人项目啊？！","coverImage":"https://b.acofork.com/img/improve-dev-speed-astro.webp","coverWidth":1200,"coverHeight":900,"date":"2026-04-19","tags":["开发"],"pinned":false,"wordCount":1225,"html":"<h2 id=\"引言\">引言</h2><p>众所周知，目前我们用的是基于一个本来非常优雅的静态博客生成器 <a href=\"https://docs.astro.build/zh-cn/getting-started/\" target=\"_blank\" rel=\"noopener noreferrer\">Astro</a> 和一个本来非常优雅的博客主题 <a href=\"https://github.com/saicaca/fuwari\" target=\"_blank\" rel=\"noopener noreferrer\">Fuwari</a> 来制作的该网站</p>\n<p>在这长达2年的魔改中，我们加入了一些神秘的功能和页面，比如： 访问量统计，论坛等等</p>\n<p>这无疑会导致项目变重，最近我们每一次冷启动开发服务器的时间都长达 <strong>几分钟</strong> 。这显然是不正常的，因为 Astro 本着 0JS，按需加载，按需水合。就算我们有几百万个页面，dev也不应该需要这么长的时间来启动。所以，是时候来看看astro dev都干了些什么了</p>\n<h2 id=\"正式开始\">正式开始</h2><p>我们观察到运行 <code>pnpm dev</code> （与 <code>astro dev</code> 等价）时，Vite在准备就绪后，日志会卡在这里很久</p>\n<div class=\"markdown-code\" data-language=\"sql\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-sql\"><span class=\"hljs-number\">16</span>:<span class=\"hljs-number\">40</span>:<span class=\"hljs-number\">06</span> [astro<span class=\"hljs-operator\">-</span>icon] Loaded icons <span class=\"hljs-keyword\">from</span> public<span class=\"hljs-operator\">/</span>icons, fa6<span class=\"hljs-operator\">-</span>brands, fa6<span class=\"hljs-operator\">-</span>regular, fa6<span class=\"hljs-operator\">-</span>solid, material<span class=\"hljs-operator\">-</span>symbols, material<span class=\"hljs-operator\">-</span>symbols<span class=\"hljs-operator\">-</span>light, mingcute, simple<span class=\"hljs-operator\">-</span>icons</code></pre></div><p>不难看出，这是Astro在收集并加载站点中的所有图标，并且在很长一段时间后，站点第一屏出现，时长为...</p>\n<div class=\"markdown-code\" data-language=\"sql\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-sql\"><span class=\"hljs-number\">16</span>:<span class=\"hljs-number\">40</span>:<span class=\"hljs-number\">53</span> [<span class=\"hljs-number\">200</span>] <span class=\"hljs-operator\">/</span> <span class=\"hljs-number\">37527</span>ms</code></pre></div><p>这显然不合理，就算图标再多，他也只是一个不超过10MB大小，不超过几百个的小文件而已。astro dev在默认情况下显然隐瞒了一些东西</p>\n<p>那么我们就需要使用 <code>--verbose</code> 标志，来事无巨细的获取开发服务器究竟被什么东西卡了这么久</p>\n<p><img src=\"https://b.acofork.com/img/improve-dev-speed-improve-dev-speed-6.webp\" alt=\"423\" width=\"423\" height=\"38\" loading=\"eager\" decoding=\"async\" /></p>\n<p>显然，我们会发现 Astro 在 Vite 准备就绪后就开始加载schema了，最典型的就是图片，由于图片在 <code>/src/content/assets</code> 下，Astro会将其当作内容集合去处理，而我们的项目总共有 <strong>1000+</strong> 图片，这会导致所有图片都会走一遍Astro的处理，哪怕我们在 <code>astro.config.mjs</code> 声明了 <a href=\"https://docs.astro.build/zh-cn/guides/images/#%E9%85%8D%E7%BD%AE-no-op-%E9%80%8F%E4%BC%A0%E6%9C%8D%E5%8A%A1\" target=\"_blank\" rel=\"noopener noreferrer\">no-op 透传</a></p>\n<div class=\"markdown-code\" data-language=\"js\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>js</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-js\"><span class=\"hljs-keyword\">export</span> <span class=\"hljs-keyword\">default</span> <span class=\"hljs-title function_\">defineConfig</span>({\n    <span class=\"hljs-attr\">image</span>: {\n        <span class=\"hljs-attr\">service</span>: <span class=\"hljs-title function_\">passthroughImageService</span>(),\n    },</code></pre></div><p>而我们最终上线的时候，图片会被替换为CDN源，尽管这个问题并不影响云端构建（因为 <code>src/content/assets</code> 目录下在构建前就会被清空）。但是它大大拖慢了本地启动开发服务器时的速度</p>\n<p>实际上，我们只需要让 Vite 将图片映射正确即可，我们既不使用 Astro 图片压缩，也不使用响应式图片</p>\n<p>那么，如何让 Astro 不再碰我们的图片呢？</p>\n<p>我们可以将图片从 <code>src/content/assets</code> 目录移动到 <code>public/assets</code> ，由于Public文件夹内的所有内容会被原封不动复制到 <code>dist</code> ，Astro自然也不会对其操作，我们只需要确保图片路径映射正确即可</p>\n<p>的确，这样修改后 Astro 不会再操作图片，但是对于之后的写作，我们也需要将图片放在 <code>public</code> 文件夹</p>\n<p>但 Obsidian 的默认行为就是哪怕是粘贴绝对路径，如 <code>/public</code> 也不会自动添加前面的斜杠，所以我们需要找AI写一个小插件自动加上，否则会因为路径拼接错误导致图片404： <a href=\"https://github.com/afoim/fuwari/tree/main/.obsidian/plugins/fix-public-links/main.js\" target=\"_blank\" rel=\"noopener noreferrer\">https://github.com/afoim/fuwari/tree/main/.obsidian/plugins/fix-public-links/main.js</a></p>\n<p>之后，Astro 将不会碰我们的图片，我们也能一如往常地去写作</p>\n<p>至此，图片优化算是做完了，但是好像有哪里还是不对</p>\n<p>还记得一开始的 <code>astro-icon</code> 加载日志吗</p>\n<div class=\"markdown-code\" data-language=\"sql\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-sql\"><span class=\"hljs-number\">16</span>:<span class=\"hljs-number\">40</span>:<span class=\"hljs-number\">06</span> [astro<span class=\"hljs-operator\">-</span>icon] Loaded icons <span class=\"hljs-keyword\">from</span> public<span class=\"hljs-operator\">/</span>icons, fa6<span class=\"hljs-operator\">-</span>brands, fa6<span class=\"hljs-operator\">-</span>regular, fa6<span class=\"hljs-operator\">-</span>solid, material<span class=\"hljs-operator\">-</span>symbols, material<span class=\"hljs-operator\">-</span>symbols<span class=\"hljs-operator\">-</span>light, mingcute, simple<span class=\"hljs-operator\">-</span>icons</code></pre></div><p>仔细想一下，图标为什么需要我们自己托管呢，无论是NPM还是各种图标站，使用CDN引入都是一个不错的做法</p>\n<p>于是，接下来我们将整个图标库连根拔起，并使用 <a href=\"https://api.iconify.design/\" target=\"_blank\" rel=\"noopener noreferrer\">https://api.iconify.design/</a> CDN来引入图标</p>\n<p>至此，dev的优化算是告一段落了，但在我们优化开发服务器的性能时，顺便修了一个小问题</p>\n<p>之前，对于桌面端的用户，我们会使用流星背景。但实际测试发现由于流星是随机出现且同屏高达 <strong>50个</strong>，这会导致高达 <strong>3000次/秒</strong> 的布局重绘， 所以我们顺便将整个流星背景移除，仅保留一个有质感的静态渐变</p>\n<p><img src=\"https://b.acofork.com/img/improve-dev-speed-improve-dev-speed-7.webp\" alt=\"\" width=\"1924\" height=\"1246\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>最终，开发服务器的启动快如闪电！<strong>仅需7秒</strong> </p>\n<p><img src=\"https://b.acofork.com/img/improve-dev-speed-Kiro_NcMc9NkpG3.webp\" alt=\"\" width=\"1005\" height=\"570\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>而之前...这也太慢了... <strong>近乎1分钟</strong> </p>\n<p><img src=\"https://b.acofork.com/img/improve-dev-speed-Kiro_tYIjKYtTlP.gif\" alt=\"\" width=\"1003\" height=\"572\" loading=\"lazy\" decoding=\"async\" /></p>\n<blockquote>\n<p>实际上并非这么简单</p>\n</blockquote>\n","toc":[{"id":"引言","text":"引言","depth":2},{"id":"正式开始","text":"正式开始","depth":2}]}