林允中 Lîm, Ún-tiong

基礎設施筆記

從 WordPress 到 Sanity + Astro + Cloudflare Workers:一個個人網站的創世紀

· --

TL;DR

  • 最終架構:Astro 6 + Sanity CMS + Cloudflare Workers — 零翻譯層、三語支援(zh-Hant / en / nan-Hant-TW)、全自動部署
  • 踩過 37+ 個坑,每一個都有具體錯誤訊息和修法:Next.js + OpenNext 21 個、版本三方互鎖、Astro 遷移後 6 個、穩定後再挖出 10+
  • 30 分鐘完成框架遷移:三個平行 AI agent 把 Next.js 換成 Astro,刪 6897 行、加 3394 行
  • 核心教訓:翻譯層越少越好,但平台限制不會因為換框架消失

1. 為什麼離開 WordPress

lzong.tw 的定位很明確:一個 signed, public, permanent 的智識身分。不是社群媒體上的碎片,而是可搜尋、可引用、永久存在的個人網站。

WordPress 有什麼問題?它其實什麼都沒做錯。但自架 WordPress 需要 VPS、PHP runtime、MySQL、安全更新 — 我不想維護主機,也沒必要為一個低流量的個人網站花這筆錢。

我要的很簡單:內容能帶著走(schema-as-code,不綁平台),不用耗費心力維運,維運成本趨近於零,順便玩玩新技術、體驗一下高度客製化的產品到底是什麼感覺。

為什麼選 Sanity + Cloudflare Workers?

Sanity 是 headless CMS,schema 用 TypeScript 定義,Studio 獨立部署,API 是 HTTP — 沒有 PHP、沒有 MySQL、沒有伺服器要維護。Cloudflare Workers 跑在全球 300+ 個節點的 edge runtime,免費額度每天 100,000 次請求,而且我的 DNS 已經在 Cloudflare 了。

為什麼不用 Vercel? Vercel 是 Next.js 的親生父母,零設定就能上線。但 Cloudflare 的免費額度比 Vercel 大方、CDN 更快、DNS 已經在 CF 了。Workers Builds 在 2025 年 4 月 GA,提供跟 Vercel 一樣的 git-connected push-to-deploy 體驗。Cloudflare 正在將 Pages 功能整合進 Workers 平台,Workers Builds 是新的推薦方案。值得注意的是 Pages 仍然有 deploy hook(可以從外部 API 觸發 rebuild),但 Workers Builds 沒有 — 這個缺失後面會再提到。

聽起來很美好。

2. 技術選型:Sanity、Workers、三語

Sanity:schema-as-code 的 headless CMS

Sanity 的核心賣點是 schema-as-code — 用 TypeScript 定義文件結構,版本控制跟程式碼一起。Studio 是獨立的 React 應用程式,部署在 lzong.sanity.studio。內容透過 GROQ 查詢語言存取,API 就是標準的 HTTP endpoint。

多語系統用 @sanity/document-internationalization 做 document-level i18n — 每個語言版本是獨立的 Sanity document,共享相同的 slug。這比 field-level i18n(一個 document 裡塞三種語言的欄位)乾淨得多,特別是當文章長度差異大的時候。

Cloudflare Workers:第三種東西

Workers 不是 Node.js,也不是瀏覽器。它是第三種東西 — 有 fetch 但沒有 fs,有 V8 和 WebAssembly 但限制某些 WASM instantiation 方式,有 Web API 但少了一些。這個認知非常重要,因為接下來踩的大部分坑都跟「把 Workers 當成 Node.js」有關。

三語支援

lzong.tw 支援三種語言:zh-Hant(繁體中文)、en(英文)、nan-Hant-TW(台語漢字)。路由用 [lang] prefix:/zh-Hant/posts/slug、/en/posts/slug、/nan-Hant-TW/posts/slug。UI 翻譯集中在 lib/translations.ts,所有元件用 t(lang, key) 取值。

台語翻譯有一個關鍵規則:查教育部臺灣閩南語常用詞辭典,不能靠直覺。「換做」的主要意思是「假使」不是「切換成」;「選擇」的台語是「揀」(kíng);「首頁」是「頭頁」。每一個翻譯都要上 sutian.moe.edu.tw 驗證。

3. 第一個架構:Next.js + OpenNext(21 個坑)

Next.js 不能直接跑在 Cloudflare Workers 上 — 它需要 Node.js server。@opennextjs/cloudflare 是一個翻譯層,把 Next.js 的 server code 轉換成 Workers 能跑的格式。

Unknown block type "code", specify a component for it in the `components.types` option

四層。每一層都是一個潛在的斷裂點。以下是全部 21 個坑,分成六類。

Build pipeline 的坑

#1. build script 無限遞迴 — package.json 的 build 設成 npx @opennextjs/cloudflare build,但 OpenNext 內部會呼叫 npm run build,形成遞迴。build 必須是 next build。網路上幾乎沒人提過這個。

#2. pnpm deploy 是保留指令 — pnpm 把 deploy 留給 monorepo 的 workspace 部署功能。部署 script 得改名叫 cf:deploy。

#3. open-next.config.ts 要寫完整 — 空的 default: {} 直接噴錯。必須明確指定 wrapper、converter、proxyExternalRequest、incrementalCache、tagCache、queue。

#4. Cloudflare Dashboard build command 不在 git 裡 — 換了框架、換了 build 指令,但 Dashboard 上的設定是手動的。忘了改就會用舊的 OpenNext 指令跑。

Runtime 相容性的坑

#5. @sanity/client 在 Workers 上炸 — 底層用 get-it 發 HTTP 請求,get-it 走 Node.js 的 https.request。OpenNext 的 unenv polyfill 沒有實作這個 API。

Unknown block type "code", specify a component for it in the `components.types` option

這是整個過程中最關鍵的一個問題。修法:繞過 @sanity/client,用原生 fetch 直接打 Sanity HTTP API。

Unknown block type "code", specify a component for it in the `components.types` option

#6. fs.readFileSync 讓整個 Worker 啟動就炸 — OpenNext 把所有 server-side code 打進一個 Worker。任何 route 裡 import { readFileSync } from 'fs' 會在啟動時執行,不管你訪問的是哪個 route。

#7. @vercel/og 不能用 — 依賴 Vercel 的 edge runtime 內部實作。替代方案:直接用 satori(@vercel/og 的底層)。

#8. edge runtime 設定破壞 OpenNext — API route 裡的 export const config = { runtime: 'edge' } 跟 OpenNext bundling 衝突。Workers 上一切都是 edge,直接移除。

#9. nodejs_compat_v2 反而更嚴格 — 切過去後報 No such module "node:fs"。維持用 nodejs_compat。

Bundle size 的坑

#10. Worker 超過 3MB 免費限制 — Sanity Studio 的 sanity 套件嵌在 Next.js 裡時,gzip 後 ~4.7MB。免費版上限 3MB。

修法:拆出 Studio,獨立部署到 *.sanity.studio(免費)。Studio-only 的套件搬到 devDependencies,Worker 降到 ~1.8MB。

Routing 的坑

#11. Next.js 內建 i18n 不能用 — next.config.ts 的 i18n 需要 Node.js server 做 locale negotiation。改用 middleware + [lang] 動態路由。

#12. getStaticProps 不能在 prerender 時 return redirect — 根路由用 middleware redirect + client-side fallback。

工具鏈的坑

#13. wrangler.jsonc 名稱要跟 Dashboard 一致 — config 裡寫 lzong-tw,Dashboard 上建的是 lzong-tw-sanity,部署直接失敗。

#14. Sanity CLI 不認 Next.js 的 path alias — sanity deploy 用 Vite build,不讀 tsconfig.json 的 baseUrl。要在 sanity.cli.ts 手動加 Vite alias。

#15. sanity deploy 的 build output 要 gitignore — dist/ 和 .sanity/ 會被意外 commit。

#16. sanity deploy 會在 assertValue() 炸 — standalone Studio 沒有 .env.local,Vite build 時 assertValue() 直接丟 error。要把 projectId/dataset 改成 hardcoded fallback。

#17. sanity deploy 要記住 hostname 和 appId — 否則每次都要互動選擇。

Debug 與前端的坑

#18. Wrangler observability 要手動開 — wrangler.jsonc 加 "observability": { "enabled": true } 才有 runtime logs。

#19. 500 錯誤要用 wrangler tail — Build log 只記錄 build 階段的錯誤。Runtime 錯誤需要 wrangler tail 或 Dashboard real-time logs。

#20. Tailwind container class 有 padding 不對稱 — 某些 viewport 寬度下左右留白不等。用 max-w-4xl mx-auto px-6 md:px-8 取代。

#21. Dark mode anti-flash — 要在 HTML parse 前就設好 <html> 的 class,否則先渲染 light mode 再跳 dark mode 會閃一下。用 inline script 在 <head> 裡解決。

4. 版本地獄與頓悟

21 個坑全部修完了。網站上線了。然後 Sanity Dashboard 說:請升級到 v5。

升級 Sanity 5 後,@sanity/visual-editing 和 next-sanity 要求 Next.js 16。升級 Next.js 16 後:

Unknown block type "code", specify a component for it in the `components.types` option

OpenNext 1.17.1 不支援 Next.js 16。它的 peer dependencies 宣稱支援 ^16.1.5,但 runtime 直接炸。

降回 Next.js 15?最新的 Sanity 套件要 Next 16。降回 Sanity 4?Dashboard 又催你升 5。

Unknown block type "code", specify a component for it in the `components.types` option

三方互鎖,無解。

回頭看那 21 個坑,有一個共同的根源:

Unknown block type "code", specify a component for it in the `components.types` option

OpenNext 是一個翻譯層。它把 Next.js 的 Node.js server 行為翻譯成 Workers 的 edge runtime 行為。每一個「坑」都是翻譯失真的結果:

  • https.request not implemented → 翻譯層沒 polyfill
  • fs.readFileSync crash → 翻譯層的 bundling 方式不對
  • edge runtime config 衝突 → 翻譯層的概念模型跟 Next.js 不同
  • Next.js 16 不相容 → 翻譯層跟不上上游的 breaking change

問題不是個別的坑,是翻譯層本身。

如果翻譯層在你的 stack 裡製造了 90% 的問題,解法不是修好每一個問題 — 是移除翻譯層。

5. 30 分鐘的 Astro 遷移

Astro 有原生的 Cloudflare Workers adapter(@astrojs/cloudflare),不需要翻譯層。

Unknown block type "code", specify a component for it in the `components.types` option

少了兩層。我用三個平行 Claude Code agent 做遷移:

Unknown block type "code", specify a component for it in the `components.types` option

73 個檔案變更。刪掉 6897 行 Next.js code,新增 3394 行 Astro code。加上 merge 衝突處理和驗證,前後 30 分鐘完成。

結果對比:

Unknown block type "code", specify a component for it in the `components.types` option

所有 21 個坑中的 OpenNext 相關問題(#1, #3, #5-10)直接消失。它們不是被修好了,是不存在了。

6. Astro 之後的坑(#22-27)

以為換了框架就結束了?沒有。遷移之後又踩了六個。

#22. Tailwind v4 + Astro 要用 @tailwindcss/vite — @astrojs/tailwind integration 只支援 Tailwind v3。v4 要用 @tailwindcss/vite 作為 Vite plugin,不走 integration。

#23. Cloudflare Dashboard 設定不在 git 裡 — build command、deploy command、環境變數都在 Dashboard 手動設定。換框架後忘了改 build command,結果 CI 還在跑舊的 npx @opennextjs/cloudflare build。一定要在 CLAUDE.md 記下正確的 Dashboard 設定。

#24. satori 的 yoga-wasm 被 Workers 禁止 — OG image 生成用 satori(把 JSX 渲染成 SVG),但 satori 底層依賴 yoga-wasm。Workers 支援 WebAssembly,但限制某些 instantiation 方式(例如從動態來源呼叫 new WebAssembly.Instance())。yoga-wasm 恰好觸發了這個限制:

Unknown block type "code", specify a component for it in the `components.types` option

修法:放棄 satori,用純 SVG 字串模板拼接。沒有 WASM、沒有 JSX、沒有依賴。

#25. 框架遷移的連鎖反應 — 刪掉 Next.js 的 components/ 目錄後,Sanity Studio 的 plugins 還在 import 它們。OpenGraphPreview import components/OpenGraphImage、AuthorAvatarPreviewPane import components/AuthorAvatar、OpenGraphInput import components/OpenGraphImage 的常數。每一個都要找出來、內聯或重寫。

#26. 舊框架的依賴殘留 — sanity.cli.ts 用了 @next/env 來讀 .env.local,但這個套件在遷移時被移除了。要改成 dotenv。sanity.config.ts 裡的 presentationTool 和 previewDocumentNode 依賴 Next.js 的 preview mode API route,也要移除。

#27. Astro API endpoint 不支援 .tsx — 以為把 .ts 改成 .tsx 就能用 JSX,結果 Astro 的 endpoint routing 只認 .ts,.tsx 直接 404。這就是為什麼 OG image 最後改成 SVG 字串模板而不是 JSX。

這六個坑有一個共同的教訓:框架遷移不是刪舊檔、建新檔就完了。每一個依賴你舊框架的東西 — 套件、CMS plugin、CLI config、API pattern — 都會在你以為結束的時候冒出來。

7. WordPress 內容搬家

架構穩了,下一步是把 WordPress 的舊文章搬過來。聽起來應該是最簡單的部分。不是。

流程

Unknown block type "code", specify a component for it in the `components.types` option

分成兩步。import-wordpress.ts 負責把 WXR(WordPress eXtended RSS)轉成 drafts/*.md 檔案 — 解析 XML、抽出 HTML、strip Gutenberg block comments、用 turndown 轉 Markdown、自動分類。產出的 .md 可以人工校稿。確認後再用 pnpm import:draft 推進 Sanity。

WordPress 的隱藏地雷

URL-encoded slug — WordPress 的中文 slug 是 URL-encoded 的(%e5%be%9e-wordpress-%e5%88%b0)。直接拿來當 Sanity slug 會很醜,要 decode 後重新命名。

Gutenberg block comments — WordPress 5.0+ 的內容裡埋了 <!-- wp:paragraph --> 之類的 block comment。turndown 不會自動清掉,要在轉換前用 regex strip。

Unknown block type "code", specify a component for it in the `components.types` option

Mixed HTML — 有些舊文章混用 Classic Editor 和 Gutenberg block,HTML 結構不一致。有些段落是 <p> 包裹,有些是裸文字加 <br>。turndown 的輸出需要人工校稿。

Category 自動分類 — WordPress 的 category 對應到 Sanity 的 category field:society → essay、software-engineering → tech。用 mapping table 自動轉換,不匹配的 fallback 到 essay。

import-draft.ts 的演進

這個腳本改了很多版:

  1. CRLF 問題 — Windows 上 ^---\n 不匹配 \r\n。frontmatter 解析靜默失敗。改成 \r?\n。
  2. dotenv 問題 — npx tsx 不會自動載入 env 檔。Secrets 放 .env.secret(gitignored),public IDs 放 .env.local(committed)。腳本要手動 config({ path: '.env.secret' }); config({ path: '.env.local' })。
  3. perspective: raw — Sanity API 預設只回傳 published document。要找 drafts.* 開頭的文件必須設 perspective: 'raw'。
  4. drafts. prefix* — client.create() 不帶 _id 會直接建 published document。要建 draft 必須用 createOrReplace() + drafts. prefixed ID。

8. 多語系統

Document-level i18n

用 @sanity/document-internationalization,每個語言版本是獨立的 Sanity document,各自有隨機 UUID 作為 _id。語言版本之間的關聯透過 translation.metadata document 管理 — 這個 metadata document 持有所有語言版本的 reference,讓 Studio 知道哪些 document 是同一篇文章的不同語言。

好處:每個語言版本的內容長度可以完全不同,不用硬塞在同一個 document 的陣列欄位裡。壞處:document 數量 × 3(加上 metadata document)。對一個個人部落格來說不是問題。

中央翻譯檔

lib/translations.ts 集中管理所有 UI 字串:

Unknown block type "code", specify a component for it in the `components.types` option

所有元件用 t(lang, 'writing') 取值,不在元件裡硬寫文字。

台語翻譯的正確做法

台語翻譯不能靠直覺、不能靠 Google 翻譯、不能靠「大概是這樣」。必須查教育部臺灣閩南語常用詞辭典(sutian.moe.edu.tw)。

幾個實際修正:

  • 「切換」→「換」(不是「換做」,「換做」主要意思是「假使」)
  • 「語言選擇」→「揀語言」(「揀」kíng 是原生台語,不是「選擇」的直譯)
  • 「首頁」→「頭頁」(不是「主頁」也不是「首頁」的直譯)

Slug uniqueness 改為 language-scoped

Sanity 的 slug validation 預設是全域唯一。這代表 zh-Hant 和 en 版不能共用同一個 slug — 英文版得加 -en 後綴,很醜。

修法:在 post schema 的 slug field 加自訂 isUnique,scope by language。zh-Hant 和 en 現在可以共享同一個 slug,因為它們的 language 欄位不同。

9. 自動化品質管控

手動維護教訓清單、手動檢查術語、手動驗證連結 — 這些事做一次可以,長期一定會退化。我從另一個專案(sugar-dating)移植了一套 hook system,用機械化的方式強制執行品管。

6 個 Claude Code hooks

Unknown block type "code", specify a component for it in the `components.types` option

核心哲學:不靠人的自律,靠機械化的流程。lesson-surfacing.mjs 裡有一個 LESSON_MAP — 檔案路徑 pattern 對應到相關的 CLAUDE.md 規則。你修 lib/sanity.fetch.ts,它會自動提醒你 @sanity/client 不能用在 Workers 上。你修 src/styles/global.css,它會提醒你用 CSS custom properties 不要 hardcode。

Memory anti-bloat guard 會在你寫 ~/.claude/ memory 而不是 CLAUDE.md 時發出警告。Git commit doc gate 會在你 commit 程式碼但沒更新 CLAUDE.md 時提醒(加 [no-lesson] 到 commit message 可以跳過)。

台灣術語 lint

27 條規則,用 hook 強制執行。中文技術文章必須使用台灣用語:

  • 數據 → 資料
  • 算法 → 演算法
  • 服務器 → 伺服器
  • 調試 → 除錯
  • 文件 → 檔案(指 file 時)
  • 網絡 → 網路
  • 應用程序 → 應用程式
  • 集成 → 整合
  • 雲計算 → 雲端運算

翻譯英文到中文時同樣適用。參考 tw-tech-terminology-converting-prompt 的 600+ 詞對照表。

Codex CLI 交叉驗證

用 OpenAI 的 Codex CLI 對整個 codebase 做自動化 review。不是跑一次就完 — 跑了 7 輪。

每一輪都會找到前一次修復引入的新問題。例如:第 3 輪修了 pagePath 沒傳到文章頁,第 4 輪發現 404 頁面還在吐 og:url。第 6 輪修了 strictNullChecks,第 7 輪發現 env.d.ts 沒建。

目標不是「跑一次沒 error」,是「Codex 沒話說」— ALL PASS 是死目標,迭代到達成為止。

WebFetch 攔截 hook

連結驗證不能用 WebFetch(只能檢查 HTTP status,不能確認頁面是否真的渲染正確)。用 Playwright browser_navigate + browser_snapshot 驗證連結是否活著、內容是否可存取。

有一個血淚教訓:polish agent 把一個 Facebook 連結標記為「dead link with no context」然後刪掉了。但那個連結是作者的原始寫作動機。Codex 接著把英文翻譯也標記為「fabricated content」。Polish agent 不能刪原始內容,必須先跟 WordPress XML source 交叉驗證。

10. Studio 客製化

Sanity Studio 預設是一個通用的 document list,但當內容類型和語言多起來之後,需要自訂 structure 才能快速找到東西。

Category 與語言篩選

Studio 的 document list 有五個篩選入口:Essays(📝)、Tech(🔧)、zh-Hant(🇹🇼)、English(🇺🇸)、WP Published(✅)。是扁平的分類,不是巢狀的 — 點 Essays 看所有語言的 essay,點 zh-Hant 看所有分類的中文文章。另外有 All Posts 顯示未篩選的全部文件。

PostStats:文章統計面板

Studio 的 Posts 選單裡嵌入了一個 Statistics 面板(React component PostStats),用 GROQ count query + perspective: 'raw' 即時顯示文章數量統計。

這裡有一個微妙的計數問題:因為 document-level i18n,一篇文章可能有 3 個 document(zh-Hant / en / nan-Hant-TW),再加上 draft 和 published 版本,document 數量會遠超實際文章數。PostStats 顯示多組數字:

  • Unique article count — 以 slug 去重,代表實際有幾篇文章(分 Essays / Tech)
  • Published / Drafts — 分開顯示已發佈和草稿的 document 數
  • By language — zh-Hant / en 各有多少 document

這讓你一眼就能看出內容量,而不是被 document 數量誤導。

wpStatus 唯讀欄位

從 WordPress 匯入且原本已發佈的文章帶有 wpStatus: "publish" 欄位,在 Studio 裡設為 readOnly。未發佈的文章不會有這個欄位。這個欄位的用途是追蹤 WordPress 原始發佈狀態,不應該手動修改 — 它是匯入歷史的記錄,不是 Sanity 的發佈狀態。

.id() 的隱形陷阱(#39)

自訂 Studio structure 時,S.documentList() 必須呼叫 .id()。漏掉 .id() 不會在 build 時報錯,TypeScript 也不會抱怨,但 Studio 載入時會直接白屏,console 裡只有一個模糊的 error:id is required for document lists。注意:S.listItem() 不需要 .id(),只有 S.documentList() 需要。

11. 穩定後的坑(#28-37+)

網站上線、功能正常之後,用 Codex CLI 做了 7 輪 review,又挖出一批。這些不是遷移的坑,是「以為穩了其實沒穩」的坑。

#28. SEO meta 幾乎全缺 — Astro 遷移後沒人補 og:title、og:url、canonical、hreflang、twitter:card、robots.txt。首頁連 <meta name="description"> 都沒有。框架遷移只管「能跑」,不管「能被找到」。

#29. "latest" 版本 × 9 — package.json 裡 9 個 Sanity 套件用 "latest"。每次 pnpm install 結果不同,CI 不可重現。踩完之後的規則:永遠 pin 版本。

#30. OG image SVG 要先 wrap 再 escape — 如果先 escapeXml() 再 wrapText(),&amp; 可能被切斷在不同的 <tspan> 元素之間,產生無效 SVG。正確順序是 wrap first, escape each line。

#31. strictNullChecks: false + 缺 env.d.ts — 遷移時帶來的 TypeScript 設定退化。strictNullChecks 被關掉、src/env.d.ts(Astro 的 type 宣告)沒建。6 個 type error 靜默消失,一行 /// <reference types="astro/client" /> 就能修。

#32. Sanity slug 全域唯一 — Sanity 的 slug validation 不 scope by language。zh-Hant 和 en 版的文件不能共用 slug。解法:自訂 isUnique,scope by language field。

#33. npx tsx 不會載入 env 檔 — Sanity CLI 用 dotenv 自動載 .env.local,但獨立腳本不會。Secrets(如 Sanity Editor token)放在 .env.secret(gitignored),public IDs 放 .env.local(committed)。腳本要手動載入兩個:

Unknown block type "code", specify a component for it in the `components.types` option

#34. Windows CRLF 破壞 frontmatter regex — ^---\n 在 Windows 上不匹配 \r\n。frontmatter 解析靜默失敗,所有欄位 fallback 到預設值。必須用 \r?\n。

#35. Sanity client.create() 不帶 _id 會直接 publish — 以為 create() 建的是 draft,其實 Sanity 自動生成的 ID 不帶 drafts. prefix = 直接 published。要建 draft 必須用 createOrReplace() + drafts. prefixed ID。

#36. npx sanity documents delete 不會刪 drafts.* 文件 — CLI 的 delete 指令只刪 published document。draft-prefix 的文件會變成隱形孤兒,導致 slug 衝突、Studio 報錯。必須用 transaction API + perspective: 'raw' 全掃全殺。

#37. prerender = true 的頁面是靜態的 — Sanity 內容更新後,Cloudflare 上的頁面不會自動反映。必須 pnpm cf:deploy 重新 build。沒有 webhook、沒有 ISR — 就是全量重建。

#38. NEXT_PUBLIC_ env vars 是故意的* — sanity.api.ts 裡的 NEXT_PUBLIC_SANITY_PROJECT_ID 看起來像 Next.js 殘留,但它們對應 .env.local 的變數名,而 Sanity Studio 的 Vite build 會讀這些。不要改名。

#39. Studio structure S.documentList() 的 .id() 必填 — 自訂 structure 裡,S.documentList() 必須呼叫 .id()(S.listItem() 不需要)。漏了不會在 build 時報錯,但 Studio 載入時會白屏:id is required for document lists。

12. 自動部署

前端部署

push to master → Cloudflare Workers Builds 自動觸發 → pnpm build(runs astro build)→ npx wrangler deploy。

Workers Builds 是 2025 年 4 月 GA 的 git-connected CI/CD。但它有一個關鍵限制:Workers Builds 沒有 deploy hook — 只能透過 git push 觸發,不能從外部呼叫 API 觸發 rebuild。(相比之下,Cloudflare Pages 有 deploy hook,但 Cloudflare 正在將 Pages 功能整合進 Workers 平台。)

這代表 Sanity 內容更新後,前端不會自動 rebuild。

Sanity Webhook → GitHub Action

Workers Builds 沒有 deploy hook,所以繞道 GitHub Action 作為 workaround:

Unknown block type "code", specify a component for it in the `components.types` option

Sanity Webhook 在內容 publish 時呼叫 GitHub API 送 repository_dispatch 事件,觸發 master branch 上的 GitHub Action。Action 接收事件後執行 pnpm build + wrangler deploy。

這條路比 Workers Builds 多一步,但解決了「內容更新不觸發 rebuild」的問題。如果 Workers Builds 未來支援 deploy hook,這個 GitHub Action 就可以移除。

Studio 部署

pnpm sanity:deploy → 部署到 lzong.sanity.studio。這是 Sanity 的免費託管,不需要自己管伺服器。

13. 結論:五個教訓

一、翻譯層越少越好。 每一層翻譯都是一個版本衝突、一個 polyfill gap、一個 breaking change 的潛在來源。Sanity → fetch → Astro → Workers 比 Sanity → next-sanity → Next.js → OpenNext → Workers 少了兩個斷裂點。如果 adapter 是問題,換框架,不要繼續加 workaround。

二、平台限制不會因為換框架消失。 Workers 限制某些 WASM instantiation 方式、不支援 fs、不支援 https.request — 這些跟你用什麼框架無關。換框架解決的是翻譯層的問題,不是平台的問題。分清楚哪些坑屬於哪一層,才知道該修什麼。

三、Build 成功不等於能跑。 最痛的 bug 都是 runtime 才出現的。[unenv] https.request is not implemented yet!、Wasm code generation disallowed by embedder — 這些不會出現在 build log 裡。一定要開 observability + wrangler tail,部署後立刻驗證。

四、機械化記錄教訓,不要靠人的自律。 6 個 hook、27 條術語規則、commit doc gate — 不是因為我記性差,是因為人類在「趕著修 bug」的時候不會想到更新文件。Hook 不會忘記。CLAUDE.md 是 canonical source,memory 和 README 都是衍生物。

五、迭代驗證到工具沒話說。 Codex 7 輪、ALL PASS 是死目標。每一輪都會找到前一次修復引入的新問題。跑一次 review 然後宣布「完成」是自欺欺人。設一個硬指標,迭代到達成為止。

Stack Summary

Unknown block type "code", specify a component for it in the `components.types` option

這篇文章記錄的是 2026 年 3 月的狀態。OpenNext 可能在未來版本修復 Next.js 16 的相容性,Workers 也可能放開 WASM 限制。但「翻譯層越少越好」、「分清楚哪一層的問題」、「機械化記錄教訓」這三個原則不會過時。


--

留言