fix: static 资源嵌入二进制,修复部署后 Markdown 内容不显示 #2

Merged
kevin merged 1 commits from dsh/go_blog:fix/embed-static-assets into main 2026-08-18 18:18:47 +08:00
Contributor

问题

PR #1 合并部署后,文章/评论内容全部不显示(https://haibara.ai/article/panel-auth-lock-your-ai-panel)。原因:Markdown 渲染的 static/ 资源是从磁盘目录提供的,但线上部署只更新了二进制和 templates,/static/js/markdown.js/static/css/markdown.css 404 → BlogMD 未定义 → 渲染脚本抛错 → 正文与评论全部空白。

修复

  1. go:embed 将 static 资源编入二进制main.go):/static 改由嵌入的 FS 提供,部署时只需要替换可执行文件,不再依赖服务器上有 static/ 目录,杜绝此类部署错位。
  2. 内容永不空白:即使渲染器资源加载失败,文章正文、评论正文、评论预览也会回退为纯文本(HTML 转义)显示,而不是空白页。
  3. install_linux.sh 移除不再需要的 static 目录拷贝;README 补充 static/ 说明。

验证

  • 本地构建通过(go build / go vet
  • 将磁盘 static/ 目录移走后重启服务,/static/js/markdown.js/static/css/markdown.css 仍返回 200(证明资源来自二进制)
  • 正常渲染路径 25 项断言全部通过;渲染器缺失的模拟部署下,正文/评论以纯文本显示,页面无 JS 报错

服务器重新部署方式(任选其一)

# 方式一:重跑安装脚本(会自动 git pull + 编译 + 安装)
sudo bash install_linux.sh

# 方式二:只替换二进制(templates 已是新版)
cd <服务器上的仓库目录> && git pull && go build -o blog_go . && sudo systemctl restart blog_go
## 问题 PR #1 合并部署后,文章/评论内容全部不显示(https://haibara.ai/article/panel-auth-lock-your-ai-panel)。原因:Markdown 渲染的 `static/` 资源是从磁盘目录提供的,但线上部署只更新了二进制和 templates,`/static/js/markdown.js` 与 `/static/css/markdown.css` 404 → `BlogMD` 未定义 → 渲染脚本抛错 → 正文与评论全部空白。 ## 修复 1. **`go:embed` 将 static 资源编入二进制**(`main.go`):`/static` 改由嵌入的 FS 提供,部署时**只需要替换可执行文件**,不再依赖服务器上有 `static/` 目录,杜绝此类部署错位。 2. **内容永不空白**:即使渲染器资源加载失败,文章正文、评论正文、评论预览也会回退为纯文本(HTML 转义)显示,而不是空白页。 3. `install_linux.sh` 移除不再需要的 static 目录拷贝;README 补充 static/ 说明。 ## 验证 - 本地构建通过(`go build` / `go vet`) - 将磁盘 `static/` 目录移走后重启服务,`/static/js/markdown.js`、`/static/css/markdown.css` 仍返回 200(证明资源来自二进制) - 正常渲染路径 25 项断言全部通过;渲染器缺失的模拟部署下,正文/评论以纯文本显示,页面无 JS 报错 ## 服务器重新部署方式(任选其一) ```bash # 方式一:重跑安装脚本(会自动 git pull + 编译 + 安装) sudo bash install_linux.sh # 方式二:只替换二进制(templates 已是新版) cd <服务器上的仓库目录> && git pull && go build -o blog_go . && sudo systemctl restart blog_go ```
dsh added 1 commit 2026-08-18 18:18:16 +08:00
The merged markdown-rendering change served /static from a disk directory,
but the live deployment only replaced the binary and templates, so
/static/js/markdown.js and /static/css/markdown.css 404'd and every
article/comment rendered empty (BlogMD was undefined and threw).

- main.go: serve /static from an embedded FS (go:embed) — deployments
  now only need to replace the executable; no static dir to copy.
- install_linux.sh: drop the static directory copy (no longer needed).
- article.html / comment_list.html: if BlogMD is unavailable, fall back
  to plain (HTML-escaped) text for the article body, comment bodies and
  the comment preview, so content is never blank.
- README: document the static/ directory.

Verified locally: /static/* serve 200 even with the disk directory
removed; normal render path (25 checks) and the fallback path both pass.
kevin merged commit 9c0ba8cd6d into main 2026-08-18 18:18:47 +08:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: kevin/go_blog#2