{"slug":"svaf-next-seo","title":"纯前端 SPA 的 SEO 自救指南：我是如何让博客和论坛被百度谷歌乖乖收录的","description":"众所周知，现在流行前后端分离，而 SPA 更是许多个人网站的首选。但 SPA 天生的 SEO 劣势（爬虫不执行 JS）让很多人头疼不已。这篇文章就来复盘一下， 我（二叉树树）的网站是如何通过边缘 Worker + 路由级元数据 + 结构化数据 + 预渲染四管齐下，让搜索引擎和社交爬虫在 SPA 里也能畅游的。","coverImage":"https://b.acofork.com/img/cover-svaf-next-seo.webp","coverWidth":800,"coverHeight":600,"date":"2026-07-22","tags":["开发"],"pinned":false,"wordCount":4404,"html":"<aside class=\"markdown-alert markdown-alert--caution\"><div class=\"markdown-alert-title\"><svg class=\"markdown-alert-icon\" aria-hidden=\"true\" width=\"18\" height=\"18\" 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=\"M12 16h.01\"></path><path d=\"M12 8v4\"></path><path d=\"M15.312 2a2 2 0 0 1 1.414.586l4.688 4.688A2 2 0 0 1 22 8.688v6.624a2 2 0 0 1-.586 1.414l-4.688 4.688a2 2 0 0 1-1.414.586H8.688a2 2 0 0 1-1.414-.586l-4.688-4.688A2 2 0 0 1 2 15.312V8.688a2 2 0 0 1 .586-1.414l4.688-4.688A2 2 0 0 1 8.688 2z\"></path></svg><span>注意</span></div><div class=\"markdown-alert-body\"><p>本文使用 DeepSeek-V4-Flash 编写。</p>\n</div></aside><h2 id=\"引言\">引言</h2><p>众所周知，现在是个做网站的都要把前后端分离一下。我赶了一趟晚集，在 2026 年的时候也把网站从 Next.js 迁移到了 Vite + React Router 7 的纯 SPA（单页应用）。</p>\n<p>为什么要这么干？因为受够了每次改一行代码就要重新构建整个站点的 SSG 模式（如果你还有 100 多篇博文和几百张图片，你也会疯掉的）。具体可以看我之前那篇文章：</p>\n<p><a href=\"/posts/micro-blog-service/\">整个博客非得塞到网站源码吗？不！纯前端的前后端分离全部搞定！</a></p>\n<p>但问题来了：SPA 是靠 JavaScript 渲染页面的。搜索引擎爬虫虽然进步了很多，大部分已经能跑 JS，但社交平台（微信、Telegram、Discord）的爬虫和部分搜索引擎在抓取链接分享卡片时，依然不执行 JavaScript。这意味着如果你的网站是个 SPA，别人发你链接到群里，分享卡片只能看到一片空白和默认标题，极其掉价。</p>\n<p>所以我不光要把网站做成 SPA，还要让它在各种爬虫面前表现得像传统多页网站一样优雅。这篇文章就从头到尾盘一盘我是怎么做到的。</p>\n<h2 id=\"问题拆解\">问题拆解</h2><p>爬虫访问一个 SPA 页面时，大概下面这几个东西是会出问题的：</p>\n<ol>\n<li><strong>标题和描述</strong> —— SPA 的 HTML 里只有一个默认 <code>&lt;title&gt;</code>，JavaScript 执行后才能改成正确标题，但爬虫不等 JS</li>\n<li><strong>分享卡片（OG/Twitter meta）</strong> —— 同上理由，Meta 标签也没机会被改</li>\n<li><strong>内容本身</strong> —— 爬虫看不到文章正文，觉得这是个空页面</li>\n<li><strong>结构化数据</strong> —— JSON-LD 这玩意也是 JS 注入的，爬虫拿不到</li>\n<li><strong>Sitemap</strong> —— 动态页面没法预先知道有哪些路径</li>\n<li><strong>RSS</strong> —— 前端 SPA 本身没有 <code>/rss.xml</code></li>\n</ol>\n<p>每一个问题都需要策略性地解决。下面进入正题。</p>\n<h2 id=\"第一层-共用路由元数据表\">第一层：共用路由元数据表</h2><p>最核心的出发点：我需要一个<strong>客户端和边缘都能读</strong>的路由表。每一页的 title、description、是否允许索引，都统一写在这个表里，两侧永不漂移。</p>\n<p>做法非常简单粗暴——写一个纯数据+纯函数的 TypeScript 文件，不引入任何浏览器或 Node API：</p>\n<div class=\"markdown-code\" data-language=\"ts\"><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>ts</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-ts\"><span class=\"hljs-keyword\">export</span> <span class=\"hljs-keyword\">const</span> <span class=\"hljs-attr\">STATIC_ROUTE_META</span>: <span class=\"hljs-title class_\">Record</span>&lt;<span class=\"hljs-built_in\">string</span>, <span class=\"hljs-title class_\">RouteMeta</span>&gt; = {\n  <span class=\"hljs-string\">'/'</span>: { <span class=\"hljs-attr\">title</span>: <span class=\"hljs-string\">''</span>, <span class=\"hljs-attr\">description</span>: <span class=\"hljs-string\">'二叉树树的个人网站 ……'</span> },\n  <span class=\"hljs-string\">'/posts'</span>: { <span class=\"hljs-attr\">title</span>: <span class=\"hljs-string\">'博客文章'</span>, <span class=\"hljs-attr\">description</span>: <span class=\"hljs-string\">'……'</span> },\n  <span class=\"hljs-string\">'/forum'</span>: { <span class=\"hljs-attr\">title</span>: <span class=\"hljs-string\">'论坛社区'</span>, <span class=\"hljs-attr\">description</span>: <span class=\"hljs-string\">'……'</span> },\n  <span class=\"hljs-string\">'/draw'</span>: { <span class=\"hljs-attr\">title</span>: <span class=\"hljs-string\">'AI 生图'</span>, <span class=\"hljs-attr\">description</span>: <span class=\"hljs-string\">'……'</span> },\n  <span class=\"hljs-comment\">// ... 其他所有路由</span>\n};\n<span class=\"hljs-keyword\">export</span> <span class=\"hljs-keyword\">function</span> <span class=\"hljs-title function_\">resolveRouteMeta</span>(<span class=\"hljs-params\"><span class=\"hljs-attr\">pathname</span>: <span class=\"hljs-built_in\">string</span></span>): <span class=\"hljs-title class_\">RouteMeta</span> { ... }</code></pre></div><p>客户端路由切换时，<code>SeoManager</code> 组件通过 <code>useLocation()</code> 拿到当前路径，查这张表，调用 <code>applySeo()</code> 重写 <code>&lt;head&gt;</code> 里的 title、description、canonical、OG、Twitter 和 JSON-LD 标签。</p>\n<p>这东西看起来很简单，但它解决了一个本质问题：<strong>路由和元数据不再是写死的 HTML，而是可编程的。</strong> 我新增一个页面时，只要在这个表加一条记录，前端和边缘就都有了对应 meta，不需要到处改。</p>\n<h2 id=\"第二层-边缘-worker-注入-爬虫的救命稻草\">第二层：边缘 Worker 注入——爬虫的救命稻草</h2><p>光客户端写 meta 是不够的，爬虫进来看不到。解决方案是让 Cloudflare Worker 在返回 HTML 之前，先用 <code>HTMLRewriter</code> 把 meta 标签替换成正确的值。</p>\n<p>这个 Worker 只处理 SPA 路由路径（没有对应静态文件的路径），逻辑很简单：</p>\n<div class=\"markdown-code\" data-language=\"text\"><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>text</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-text\">请求进来 → 从 ASSETS 拿到 index.html → 用 HTMLRewriter 重写 title/description/canonical/OG → 返回</code></pre></div><p>但关键在于，它和客户端<strong>共用同一张路由表</strong> —— 没错，Worker 直接 import 了 <code>src/lib/seo/route-meta.ts</code>。这意味着前后端使用同一份数据源，不会出现 \"客户端显示的是这个标题，爬虫抓到的却是另一个\" 的割裂局面。</p>\n<p>具体实现大概这样：</p>\n<div class=\"markdown-code\" data-language=\"ts\"><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>ts</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-ts\"><span class=\"hljs-keyword\">let</span> rewriter = <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">HTMLRewriter</span>()\n  .<span class=\"hljs-title function_\">on</span>(<span class=\"hljs-string\">'title'</span>, { <span class=\"hljs-title function_\">element</span>(<span class=\"hljs-params\">el</span>) { el.<span class=\"hljs-title function_\">setInnerContent</span>(title); } })\n  .<span class=\"hljs-title function_\">on</span>(<span class=\"hljs-string\">'meta[name=\"description\"]'</span>, <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">SetContent</span>(meta.<span class=\"hljs-property\">description</span>))\n  .<span class=\"hljs-title function_\">on</span>(<span class=\"hljs-string\">'link[rel=\"canonical\"]'</span>, <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">SetHref</span>(canonical))\n  .<span class=\"hljs-title function_\">on</span>(<span class=\"hljs-string\">'meta[property=\"og:title\"]'</span>, <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">SetContent</span>(title))\n  .<span class=\"hljs-title function_\">on</span>(<span class=\"hljs-string\">'meta[property=\"og:image\"]'</span>, <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">SetContent</span>(ogImage));\n<span class=\"hljs-comment\">// ... 更多 meta 标签</span>\n<span class=\"hljs-keyword\">return</span> rewriter.<span class=\"hljs-title function_\">transform</span>(assetRes);</code></pre></div><p>这样一来，微信、Telegram、Discord 的爬虫来抓链接分享卡片时，拿到的是一个已被正确注入 OG 标签的完整 HTML，分享卡片就能正常显示了。</p>\n<h2 id=\"第三层-博客文章边缘预渲染\">第三层：博客文章边缘预渲染</h2><p>对于静态页面，注入 title 和 description 已经足够了。但对于博客文章这种内容型页面，爬虫如果能直接看到正文，对 SEO 是极大的加分。</p>\n<p>我的博客内容来源于另一个独立的 eleventy 仓库（生成 <code>posts.json</code> 索引 + Markdown 原文），存储在 <code>raw-posts.2x.nz</code> 域名下。Worker 在收到 <code>/posts/&lt;slug&gt;</code> 请求时，会：</p>\n<ol>\n<li>从边缘缓存（或回源）拉取 <code>posts.json</code> 索引，找到文章元数据</li>\n<li>拉取对应的 <code>.md</code> 原文</li>\n<li>用和客户端<strong>完全同一套</strong>的 markdown 渲染管线（<code>markdown-it</code> + callout + hljs）渲染成 HTML</li>\n<li>把渲染后的 HTML 注入到 <code>&lt;div id=\"root\"&gt;</code> 中</li>\n</ol>\n<p>这就意味着非 JS 爬虫在访问文章页时，直接就能拿到包含完整正文的 HTML。而浏览器端 React 挂载时，<code>createRoot().render()</code> 会直接替换掉预渲染的内容，不存在 hydration 冲突——因为 SPA 本身就是 CSR，不是 SSR。</p>\n<p>效果？来看看实际搜索结果就知道了。</p>\n<p>Google 搜索 \"二叉树树\"，首页第一就是本站，正文片段也能正常展示：</p>\n<p><img src=\"https://b.acofork.com/img/search-google.webp\" alt=\"Google 搜索 &quot;二叉树树&quot; 结果\" width=\"1080\" height=\"2376\" loading=\"eager\" decoding=\"async\" /></p>\n<p>百度同样有很好的收录表现：</p>\n<p><img src=\"https://b.acofork.com/img/search-baidu.webp\" alt=\"百度搜索 &quot;二叉树树&quot; 结果\" width=\"1080\" height=\"2376\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>Bing 也没落下，同样排名靠前：</p>\n<p><img src=\"https://b.acofork.com/img/search-bing.webp\" alt=\"Bing 搜索 &quot;二叉树树&quot; 结果\" width=\"1080\" height=\"2376\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>更让人惊喜的是，这套 SEO 方案不只让网站名称能搜到，一些技术关键词也有不错的权重。比如搜索 \"Cloudflare优选\"，在 Google 和 Bing 上本站同样位列前排：</p>\n<p><img src=\"https://b.acofork.com/img/search-google-cf.webp\" alt=\"Google 搜索 &quot;Cloudflare优选&quot; 结果\" width=\"1080\" height=\"2376\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><img src=\"https://b.acofork.com/img/search-bing-cf.webp\" alt=\"Bing 搜索 &quot;Cloudflare优选&quot; 结果\" width=\"1080\" height=\"2376\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>这就是边缘 Worker + 路由元数据 + 预渲染 + 结构化数据四管齐下的成果。</p>\n<p>而对于 <code>/posts</code> 列表页，Worker 也会生成一个简单的文章链接列表注入 <code>#root</code>，方便爬虫发现所有文章入口：</p>\n<div class=\"markdown-code\" data-language=\"html\"><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=\"m18 16 4-4-4-4\"></path><path d=\"m6 8-4 4 4 4\"></path><path d=\"m14.5 4-5 16\"></path></svg><span>html</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-html\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1</span>&gt;</span>博客文章<span class=\"hljs-tag\">&lt;/<span class=\"hljs-name\">h1</span>&gt;</span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul</span>&gt;</span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li</span>&gt;</span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">time</span>&gt;</span>2026-07-17<span class=\"hljs-tag\">&lt;/<span class=\"hljs-name\">time</span>&gt;</span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a</span> <span class=\"hljs-attr\">href</span>=<span class=\"hljs-string\">\"/posts/micro-blog-service\"</span>&gt;</span>整个博客非得塞到网站源码吗？...<span class=\"hljs-tag\">&lt;/<span class=\"hljs-name\">a</span>&gt;</span><span class=\"hljs-tag\">&lt;/<span class=\"hljs-name\">li</span>&gt;</span>\n  ...\n<span class=\"hljs-tag\">&lt;/<span class=\"hljs-name\">ul</span>&gt;</span></code></pre></div><h2 id=\"第四层-论坛帖子结构化数据与-seo\">第四层：论坛帖子结构化数据与 SEO</h2><p>论坛相对博客来说更特殊一些：内容来自用户生成，动态性更强，首页列表页不需要预渲染（因为数据一直变），但详情页必须要让爬虫能看懂。</p>\n<p>论坛详情页的做法是这样的：</p>\n<ol>\n<li><strong>边缘 Worker 侧</strong>：拦截 <code>/forum/post/&lt;id&gt;</code> 请求，调用论坛后端 API（<code>/api/posts/&lt;id&gt;</code>）获取帖子数据，注入 title/description/OG，并附带 <code>DiscussionForumPosting</code> 类型的 JSON-LD 结构化数据</li>\n<li><strong>客户端侧</strong>：<code>PostContent</code> 组件在拿到帖子数据后，调用 <code>applySeo()</code> 覆写为真实数据——包括 <code>DiscussionForumPosting</code> 类型的 JSON-LD，其中包含了点赞数、评论数、作者等交互数据</li>\n</ol>\n<p>这个 <code>DiscussionForumPosting</code> 结构化数据是 Google 富媒体结果中的一种，可以在搜索结果中显示帖子标题、作者、点赞数和评论数——对论坛帖子来说非常有用。</p>\n<p>客户端代码大概长这样：</p>\n<div class=\"markdown-code\" data-language=\"ts\"><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>ts</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-ts\"><span class=\"hljs-title function_\">applySeo</span>({\n  <span class=\"hljs-attr\">title</span>: p.<span class=\"hljs-property\">title</span>,\n  <span class=\"hljs-attr\">description</span>: <span class=\"hljs-title function_\">makeExcerpt</span>(p.<span class=\"hljs-property\">content</span> || <span class=\"hljs-string\">''</span>),\n  <span class=\"hljs-attr\">path</span>: <span class=\"hljs-string\">`/forum/post/<span class=\"hljs-subst\">${id}</span>`</span>,\n  <span class=\"hljs-attr\">ogType</span>: <span class=\"hljs-string\">'article'</span>,\n  <span class=\"hljs-attr\">jsonLd</span>: {\n    <span class=\"hljs-string\">'@context'</span>: <span class=\"hljs-string\">'https://schema.org'</span>,\n    <span class=\"hljs-string\">'@type'</span>: <span class=\"hljs-string\">'DiscussionForumPosting'</span>,\n    <span class=\"hljs-attr\">headline</span>: p.<span class=\"hljs-property\">title</span>,\n    <span class=\"hljs-attr\">interactionStatistic</span>: [\n      { <span class=\"hljs-string\">'@type'</span>: <span class=\"hljs-string\">'InteractionCounter'</span>,\n        <span class=\"hljs-attr\">interactionType</span>: <span class=\"hljs-string\">'https://schema.org/LikeAction'</span>,\n        <span class=\"hljs-attr\">userInteractionCount</span>: p.<span class=\"hljs-property\">likeCount</span> ?? <span class=\"hljs-number\">0</span> },\n      { <span class=\"hljs-string\">'@type'</span>: <span class=\"hljs-string\">'InteractionCounter'</span>,\n        <span class=\"hljs-attr\">interactionType</span>: <span class=\"hljs-string\">'https://schema.org/CommentAction'</span>,\n        <span class=\"hljs-attr\">userInteractionCount</span>: p.<span class=\"hljs-property\">commentCount</span> ?? <span class=\"hljs-number\">0</span> },\n    ],\n  },\n});</code></pre></div><p>这里有个关键点：论坛的 API 调用在边缘 Worker 中做了 5 分钟边缘缓存 + 一次自动重试。内容源 Worker 的冷启动偶尔会超时，重试一次能显著降低爬虫拿到兜底 meta 的概率——搜索引擎爬虫没有那么耐心，你响应慢了它就直接走了。</p>\n<h2 id=\"第五层-sitemap-动态生成\">第五层：Sitemap 动态生成</h2><p>传统 SSG 的 sitemap 是构建时生成的静态 XML 文件。但我的博客文章是存在独立仓库的，论坛帖子更是用户实时发布的，不可能在构建时知道所有路径。</p>\n<p>解决方案还是在 Worker 层面动态生成。<code>/sitemap.xml</code> 请求进来后，Worker 同时做三件事：</p>\n<ol>\n<li>拉取 <code>posts.json</code> 索引获取所有博客文章路径</li>\n<li>调用论坛 API <code>/api/posts?limit=100</code> 获取最新帖子路径</li>\n<li>合并所有静态页面路径（从路由表自动收集）</li>\n</ol>\n<p>然后拼装成一个完整的 <code>urlset</code> 返回，带 5 分钟缓存：</p>\n<div class=\"markdown-code\" data-language=\"ts\"><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>ts</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-ts\"><span class=\"hljs-keyword\">const</span> urls = [\n  ...<span class=\"hljs-title function_\">indexableStaticPaths</span>().<span class=\"hljs-title function_\">map</span>(<span class=\"hljs-function\"><span class=\"hljs-params\">p</span> =&gt;</span> <span class=\"hljs-string\">`&lt;url&gt;&lt;loc&gt;<span class=\"hljs-subst\">${SITE_URL}</span><span class=\"hljs-subst\">${p}</span>&lt;/loc&gt;&lt;/url&gt;`</span>),\n  ...posts.<span class=\"hljs-title function_\">map</span>(<span class=\"hljs-function\"><span class=\"hljs-params\">p</span> =&gt;</span> <span class=\"hljs-string\">`&lt;url&gt;&lt;loc&gt;<span class=\"hljs-subst\">${SITE_URL}</span>/posts/<span class=\"hljs-subst\">${p.slug}</span>&lt;/loc&gt;&lt;/url&gt;`</span>),\n  ...forumPosts.<span class=\"hljs-title function_\">map</span>(<span class=\"hljs-function\"><span class=\"hljs-params\">p</span> =&gt;</span> <span class=\"hljs-string\">`&lt;url&gt;&lt;loc&gt;<span class=\"hljs-subst\">${SITE_URL}</span>/forum/post/<span class=\"hljs-subst\">${p.id}</span>&lt;/loc&gt;&lt;/url&gt;`</span>),\n];</code></pre></div><p>如果 URL 数量超过 <code>SITEMAP_SHARD_SIZE</code>（我设为 10000），还会自动分片成 sitemap 索引 + 多个分片文件，适配将来内容增长。</p>\n<p>Edge 缓存 5 分钟的意思是：Google 来抓 sitemap 时，大部分情况直接命中缓存，只有首次或缓存过期才会触发一次完整的回源构建，对性能几乎无影响。</p>\n<h2 id=\"第六层-rss-其实纯透传就够了\">第六层：RSS —— 其实纯透传就够了</h2><p>RSS 我采取了一个非常偷懒但非常聪明的做法：<strong>不做任何复杂处理，直接从后端透传。</strong></p>\n<p>博客的内容源（eleventy 仓库）在构建时已经生成了完整的 <code>rss.xml</code>，带有 <code>content:encoded</code> 全文和 <code>media:content</code> 封面图。Worker 只需要把这个文件透传给用户就行：</p>\n<div class=\"markdown-code\" data-language=\"ts\"><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>ts</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-ts\"><span class=\"hljs-keyword\">const</span> upstream = <span class=\"hljs-keyword\">await</span> <span class=\"hljs-title function_\">fetch</span>(<span class=\"hljs-string\">`<span class=\"hljs-subst\">${env.POSTS_DOMAIN}</span>/rss.xml`</span>);\n<span class=\"hljs-keyword\">return</span> <span class=\"hljs-keyword\">new</span> <span class=\"hljs-title class_\">Response</span>(upstream.<span class=\"hljs-property\">body</span>, {\n  <span class=\"hljs-attr\">headers</span>: { <span class=\"hljs-string\">'content-type'</span>: <span class=\"hljs-string\">'application/rss+xml; charset=utf-8'</span> },\n});</code></pre></div><p>这样一来，RSS 阅读器（如 Folo、Feedly）拿到的 feed 链接是 <code>2x.nz/rss.xml</code>，但内容的真正来源是 <code>raw-posts.2x.nz/rss.xml</code>。用户感知不到这层代理，看到的只有一个统一的域名。</p>\n<p>为什么这么做？因为在博客前后端分离后，如果你让 RSS 阅读器直接去读 <code>raw-posts.2x.nz/rss.xml</code>，用户会看到一个丑陋的原始域名，并且点文章链接会跳到后端域名而非美化后的前端 SPA。透传 + 链接重写一次解决两个问题。</p>\n<h2 id=\"第七层-uniform-url-无尾斜杠大统一\">第七层：Uniform URL —— 无尾斜杠大统一</h2><p>你可能觉得这不算 SEO，但在 Google 看来，<code>/posts/x</code> 和 <code>/posts/x/</code> 是两个不同的 URL。如果两种形态同时存在，收录会分裂，浏览量会被记成两条，PageRank 也会被稀释。</p>\n<p>我是怎么解决的？<strong>四面围堵：</strong></p>\n<ol>\n<li><strong>边缘 Worker</strong>：任何带尾斜杠的路径（根路径除外）直接 301 重定向到无斜杠版本</li>\n<li><strong>SPA 路由内部</strong>：<code>TrailingSlashRedirect</code> 组件兜底 SPA 内部导航（history API 不走边缘）</li>\n<li><strong>sitemap</strong>：生成的 URL 全部使用 <code>canonicalPath()</code> 统一去斜杠</li>\n<li><strong>浏览量统计</strong>：<code>normalizePathname()</code> 同样标准化路径后再上报</li>\n</ol>\n<p>四者必须保持同一个形态，任何一处漂移都会导致分裂。这在测试里很容易漏掉，但一旦出了幺蛾子，你的搜索流量就会莫名其妙地下降一截。</p>\n<h2 id=\"第八层-结构化数据的完整覆盖\">第八层：结构化数据的完整覆盖</h2><p>最后串一下所有 JSON-LD 结构化数据的覆盖：</p>\n<table>\n<thead>\n<tr>\n<th>页面类型</th>\n<th>JSON-LD 类型</th>\n<th>用途</th>\n</tr>\n</thead>\n<tbody><tr>\n<td>整站</td>\n<td><code>WebSite</code> + <code>Person</code></td>\n<td>品牌搜索、知识图谱</td>\n</tr>\n<tr>\n<td>所有可索引页</td>\n<td><code>BreadcrumbList</code></td>\n<td>Google 搜索结果里的面包屑路径</td>\n</tr>\n<tr>\n<td>博客文章</td>\n<td><code>BlogPosting</code></td>\n<td>富媒体文章卡（标题+描述+日期+封面+作者）</td>\n</tr>\n<tr>\n<td>论坛帖子</td>\n<td><code>DiscussionForumPosting</code></td>\n<td>论坛帖子富媒体结果（点赞数+评论数）</td>\n</tr>\n<tr>\n<td>工具/登录/管理等</td>\n<td>无（<code>noindex: true</code>）</td>\n<td>不让这些页面进索引</td>\n</tr>\n</tbody></table>\n<p>面包屑是自动计算的——从路由表的路径前缀逐级推导。比如 <code>/posts/xxx</code> 会自动生成 \"二叉树树 › 博客文章 › &lt;文章标题&gt;\" 的 BreadcrumbList。这意味着我不需要手动为每篇文章维护一个面包屑配置。</p>\n<h2 id=\"总结\">总结</h2><p>总结一下这套架构的全貌：</p>\n<div class=\"markdown-code\" data-language=\"text\"><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>text</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-text\">用户/爬虫请求 → Cloudflare Worker\n  ↓\n  请求是静态资源（JS/CSS/图片）？→ 直接返回 ASSETS，不走 Worker\n  请求是 SPA 路由（无对应文件）？→ Worker 开始工作\n    ↓\n  1. 解析 pathname\n  2. 查路由表 → 拿到基础 meta（title/description/robots）\n  3. /posts/&lt;slug&gt;？→ 拉 posts.json + .md → 预渲染全文 HTML → BlogPosting JSON-LD\n  4. /forum/post/&lt;id&gt;？→ 调用论坛 API → DiscussionForumPosting JSON-LD\n  5. /sitemap.xml？→ 收集全站路径 → 输出 sitemap（自动分片）\n  6. /rss.xml？→ 透传后端 RSS\n  7. 其他路径 → 只注入 meta 标签\n    ↓\n  使用 HTMLRewriter 改写 index.html → 返回给客户端\n    ↓\n  客户端 React 挂载 → SeoManager 按路由表覆写 meta → 数据加载后 applySeo 覆写动态详情</code></pre></div><p>这套方案的好处是：</p>\n<ul>\n<li><strong>低成本</strong>：没有 SSR 服务器，没有 Node.js 后端，全靠 Cloudflare Worker 的边缘计算能力</li>\n<li><strong>同源同数据</strong>：客户端和边缘共用路由表，不会出现前后端 meta 不一致的尴尬</li>\n<li><strong>渐进增强</strong>：爬虫看到的是预渲染版本，用户看到的是完整 SPA，各取所需</li>\n<li><strong>自动扩展</strong>：新增页面只要加路由表记录，sitemap 和 meta 自动跟进</li>\n</ul>\n<p>当然也有一些代价和取舍：</p>\n<ul>\n<li><code>posts.json</code> 携带所有文章元数据供搜索与边缘预读，但列表翻页实际走的是 <code>posts-{n}.json</code> 分片（每片 30 篇），不依赖全量索引的大小</li>\n<li>Worker 的 <code>HTMLRewriter</code> 有 512 个 handler 的上限，好在静态页面的 meta 标签数量固定，不受内容规模影响</li>\n<li>博客正文的预渲染只在边缘做，如果文章渲染失败，爬虫看到的还是空壳——所以要有优雅的降级策略</li>\n</ul>\n<p>但总的来说，这套方案在 \"纯前端 SPA\" 和 \"搜索引擎友好\" 之间找到了一个不错的平衡点。对于个人网站和小型社区来说，已经绰绰有余了。</p>\n<p>你听懂了吗？</p>\n<p>有问题欢迎在论坛或评论区留言讨论~</p>\n<h2 id=\"2x-nz-全自动开发焚诀\"><a href=\"http://2x.nz\" target=\"_blank\" rel=\"noopener noreferrer\">2x.nz</a> 全自动开发焚诀</h2><div class=\"markdown-code\" data-language=\"markdown\"><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>markdown</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-markdown\">你是一个运行在纯文本模式下的全托管 AI 工程师（无视觉多模态能力）。\n请直接将“当前工作区目录”作为开发与测试环境，自主跑通并验证以下业务需求。\n\n================================================================\n【业务需求与变更目标】\n<span class=\"hljs-section\">{{REQ}}\n================================================================</span>\n\n【自主执行闭环流程】\n\n阶段一：项目架构感知与环境准备\n<span class=\"hljs-bullet\">1.</span> 静态分析：读取当前工作区目录结构、依赖文件（如 package.json）及源码，梳理项目架构并定位目标改动文件。\n<span class=\"hljs-bullet\">2.</span> 本地服务启动：启动本地 Dev Server（例如 <span class=\"hljs-code\">`npm run dev`</span> / <span class=\"hljs-code\">`npx serve . -p {{DEV_PORT}}`</span>）。\n<span class=\"hljs-bullet\">3.</span> 动态结构感知：通过 Node.js 执行 Headless Chromium (Playwright)，以指定 Viewport 访问本地开发地址，获取 DOM 树结构、组件选择器及初始计算样式 (Computed Style)。\n\n阶段二：代码修改与功能实现\n<span class=\"hljs-bullet\">1.</span> 根据需求修改源码（HTML/CSS/JS/组件等），确保符合标准的响应式或交互规范。\n<span class=\"hljs-bullet\">2.</span> 为关键新增交互节点配置清晰的语义化属性（如 id、class 或 data-testid），以供自动化断言定位。\n\n阶段三：无视觉自动化测试与代码自愈\n编写并执行 Playwright/Node 测试脚本，完成以下逻辑断言：\n<span class=\"hljs-bullet\">1.</span> <span class=\"hljs-strong\">**静态/动态状态断言**</span>：通过 <span class=\"hljs-code\">`window.getComputedStyle()`</span> 与 DOM 状态检查目标元素的显隐（如 display/visibility/rect 坐标）、属性值及结构变化。\n<span class=\"hljs-bullet\">2.</span> <span class=\"hljs-strong\">**交互动作断言**</span>：模拟用户行为（点击、输入、切换等），等待渲染更新后校验页面状态是否符合预期的交互逻辑。\n<span class=\"hljs-bullet\">3.</span> <span class=\"hljs-strong\">**环境适配断言**</span>（若涉及响应式）：在不同的 Viewport 尺寸下测试，验证布局与样式切换正常。\n<span class=\"hljs-bullet\">4.</span> <span class=\"hljs-strong\">**异常日志审计**</span>：读取控制台 Log 和 Network 状态，确保无未捕获的 JS 异常或 404/500 请求。\n<span class=\"hljs-emphasis\">*注：若任意断言失败，自主分析原因、修正代码，重复阶段三，直至测试 100% 通过。*</span>\n\n阶段四：收束、部署与生产验证\n<span class=\"hljs-bullet\">1.</span> 测试全部通过后，创建 Git 提交：\n<span class=\"hljs-bullet\">2.</span> 执行生产构建/部署命令，将代码上线\n<span class=\"hljs-bullet\">3.</span> 驱动 Playwright 访问生产环境 HTTP 地址，重新运行阶段三的自动化功能与样式回归断言。\n<span class=\"hljs-bullet\">4.</span> 全部正确后，向用户汇报修改内容与自动化验证结果。</code></pre></div>","toc":[{"id":"引言","text":"引言","depth":2},{"id":"问题拆解","text":"问题拆解","depth":2},{"id":"第一层-共用路由元数据表","text":"第一层：共用路由元数据表","depth":2},{"id":"第二层-边缘-worker-注入-爬虫的救命稻草","text":"第二层：边缘 Worker 注入——爬虫的救命稻草","depth":2},{"id":"第三层-博客文章边缘预渲染","text":"第三层：博客文章边缘预渲染","depth":2},{"id":"第四层-论坛帖子结构化数据与-seo","text":"第四层：论坛帖子结构化数据与 SEO","depth":2},{"id":"第五层-sitemap-动态生成","text":"第五层：Sitemap 动态生成","depth":2},{"id":"第六层-rss-其实纯透传就够了","text":"第六层：RSS —— 其实纯透传就够了","depth":2},{"id":"第七层-uniform-url-无尾斜杠大统一","text":"第七层：Uniform URL —— 无尾斜杠大统一","depth":2},{"id":"第八层-结构化数据的完整覆盖","text":"第八层：结构化数据的完整覆盖","depth":2},{"id":"总结","text":"总结","depth":2},{"id":"2x-nz-全自动开发焚诀","text":"2x.nz 全自动开发焚诀","depth":2}]}