Vinc3nt's Life

Astro Blog - 修正 astro-yi 主題的 draft content leaded

2024-04-01
develop
blog
astro
bug-fix
security
最後更新:2025-01-26
2分鐘
326字

發現問題

完成 自動排程 Profile 後,過過了幾天發現我的 Profile 會顯示草稿內容。這種情況需要即時處理,要不然草稿可能存在的機敏資料會有外流的風險。

尋找原因

透過對 astro-yi 原始碼的分析,我瞭解到所有內容的產生是透過 getCollection() 函式取得,並進一步傳遞給 Component。 雖然作者有對取得內容做草稿狀態的篩選,但這只侷限於列表顯示的頁面,而 rss.xmlsitemap 依然會產生相對應的資料。 不僅如此, Astro build 依然會將草稿狀態的 .md 檔轉換成 html 檔並暴露檔案。

解決思路

  • 為所有會經手 getCollection() 資料的函式進行 draft 狀態篩選,包含 rsssitemap
  • 使用環境區分篩選邏輯: 我們還是希望本地開發可以看見草稿文章,但 build 時不要產生。我們可以透過 import.meta.env.PROD 分辨環境,當 astro dev 時該值是 false, astro build 時該值是 true
  • 為 draft 文章添加 draft 特效

PR

fix: the blog drafts leaded when building

參考

本文標題:Astro Blog - 修正 astro-yi 主題的 draft content leaded
文章作者:Vincent Lin
發布時間:2024-04-01