Skip to content

把 404 頁接上未匹配的路徑 - #44

Merged
hoshimoe merged 1 commit into
mainfrom
fix/serve-404-page-hoshivel-web
Sep 3, 2026
Merged

把 404 頁接上未匹配的路徑#44
hoshimoe merged 1 commit into
mainfrom
fix/serve-404-page-hoshivel-web

Conversation

@hoshimoe

@hoshimoe hoshimoe commented Sep 3, 2026

Copy link
Copy Markdown
Member

hoshivel.com 上任何不存在的路徑都回一份零位元組的 404。瀏覽器把空 body
畫成白頁、分頁標題退回原始 URL——看起來像整個站掛了,而不是路徑打錯。

姊妹站 sr-web#64 是同一個缺陷、
同一行修法,已合併並部署驗過(/zzz-not-here404 15777)。

原因不在頁面

src/pages/404.astro 在,四語文案也都在 src/i18n/ui.tsastro build 一直
寫出 dist/404.html(46,185 bytes)。缺的是 wrangler.jsonc
assets.not_found_handling:預設值 "none" 對未匹配的請求回空 body,
從不打開 404.html

頁面在、產物在、部署成功——三者單獨看都正常,所以沒有任何一步會報錯。

改動

  • wrangler.jsoncassets.not_found_handling 設為 "404-page"這是修正本身。
  • test/not-found.test.mjs:釘住上面那一行。拿掉它,第一條斷言就紅,其餘三條仍綠。
  • README.md:〈部署〉記下這個鍵與本機的確認方式(埠取自本站的區塊 26820-26829)。
  • .gitignore.wrangler/npx wrangler dev 的本機狀態)。

頁面文案不動。 本站的 404 依 404.astro 檔頭的既有決定只用正體中文
「靜態站無法從 Referer 可靠判斷語言,預設使用正體中文」);sr-web 那邊加了
英文副標是因為它的檔頭註解本來就寫了要有,兩站在這一點上刻意不同。要不要統一
是另一個文案決定,不在本 PR。

驗證

本機以 npx wrangler dev 跑 Workers 真正的資產路由,同一份 dist/ 做 A/B,
只差設定那一行:

路徑 不帶該鍵 "404-page"
/zzz-not-here 404 0 404 46185
/nope 404 0 404 46185
/en/nope 404 0 404 46185
/ 200 61327 200 61327

「不帶」那一欄與正式站現況一致。body 內含 <title>404 — Hoshivel</title>
這片夜空還沒有這顆星返回首頁

  • hoshi test — 全部通過(17.8s)
  • npm test — 20 passed(新增 4 條)
  • astro check + astro build — 33 pages

未動 src/i18n/,因此不需要 npm run fonts

合併並部署後對正式站驗一次:https://hoshivel.com/zzz-not-here 要回 404
非空的 body。

hoshivel.com 上任何不存在的路徑都回一份零位元組的 404,瀏覽器把空 body 畫成
白頁、分頁標題退回原始 URL——看起來像整個站掛了,而不是路徑打錯。

原因不在頁面:src/pages/404.astro 在,四語文案也都在 src/i18n/ui.ts,
astro build 一直寫出 dist/404.html(46 KB)。缺的是 wrangler.jsonc 的
assets.not_found_handling——預設值 "none" 對未匹配的請求回空 body,
從不打開 404.html。改為 "404-page"。

與姊妹站 sr-web 同一個缺陷、同一行修法;sr-web 已合併並部署驗過。理由見
workspace 的 decisions/infrastructure/Workers-靜態站要顯式開啟-404-頁.md。

test/not-found.test.mjs 釘住這一行,拿掉它第一條就紅。頁面文案不動——本站的
404 依 404.astro 檔頭的既有決定只用正體中文。
@hoshimoe
hoshimoe merged commit 7a14095 into main Sep 3, 2026
6 checks passed
@hoshimoe
hoshimoe deleted the fix/serve-404-page-hoshivel-web branch September 3, 2026 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant