Files
wait4xandGLM-5.3 c4bdf46614 fix(ops): .mjs MIME 修复脚本——pdf.js worker 线上加载失败根因
线上 /_astro/pdf.worker.min.*.mjs 被 nginx 以 application/octet-stream
返回(mime.types 无 mjs 映射),浏览器拒绝以 module worker 加载 →
/papers/ 页 pdf.js 静默回落兜底卡;本地 python 测试床能猜对 MIME 故未
暴露。脚本幂等追加 mjs 到既有 javascript 映射行,-t 门禁+回滚+线上终验。

验证: bash -n 过;线上 worker 头实测 application/octet-stream(病根实证)

Spec: none (运维修复脚本)

Co-authored-by: GLM-5.3
2026-11-02 16:03:30 +08:00

44 lines
2.2 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 修复 .mjs MIME:nginx 未映射 mjs → 以 application/octet-stream 返回,
# 浏览器拒绝以 module worker 加载(pdf.js 在 /papers/ 页失败回落的根因)
# 用法:sudo bash scripts/ops/fix-mjs-mime.sh (幂等 + -t 门禁 + 回滚)
set -euo pipefail
MIME=/etc/nginx/mime.types
BAK=/tmp/mime.types.bak-$(date +%s)
echo "==== ① 现状 ===="
grep -nE "\bmjs\b" "$MIME" || echo "(mime.types 里没有 mjs 映射 ← 这就是病根)"
echo -n "线上实测 worker Content-Type: "
curl -sI --noproxy '*' -m 10 "https://wait4x.online/_astro/$(ls /home/wait4x/Code/Github/my-blog/dist/_astro/ | grep -E 'pdf\.worker.*\.mjs' | head -1)" | grep -i "^content-type" | tr -d '\r'
echo
echo "==== ② 计划 ===="
echo " 在 $MIME 的 types 块内插入:application/javascript mjs;"
echo
read -rp ">> 执行?(y/N) " ok; [ "$ok" = "y" ] || { echo "已取消"; exit 0; }
cp "$MIME" "$BAK"
if ! grep -qE "^\s*application/javascript\s+.*\bmjs\b" "$MIME"; then
# 插到 text/javascript 行尾(同块),保持 types{} 内语法
if grep -qE "^\s*(application|text)/javascript\s+[^;]*js;" "$MIME"; then
# 追加到既有 javascript 映射行(nginx mime.types 标准形态:application/javascript js;)
sed -i -E '0,/^\s*(application|text)\/javascript\s+([^;]*);/s//\1\/javascript \2 mjs;/' "$MIME"
echo " 已在 javascript 映射行追加 mjs"
else
sed -i '0,/types {/s//types {\n application\/javascript mjs;/' "$MIME"
echo " 已在 types 块首插入 mjs"
fi
fi
echo
echo "==== ③ 门禁 ===="
if ! nginx -t; then echo "!! 语法失败,回滚"; cp "$BAK" "$MIME"; nginx -t && systemctl reload nginx; exit 1; fi
systemctl reload nginx && echo " nginx 已重载"
echo
echo "==== ④ 终验 ===="
W=$(ls /home/wait4x/Code/Github/my-blog/dist/_astro/ | grep -E 'pdf\.worker.*\.mjs' | head -1)
CT=$(curl -sI --noproxy '*' -m 10 "https://wait4x.online/_astro/$W" | grep -i "^content-type" | tr -d '\r')
echo " worker $W → $CT(期望含 javascript)"
case "$CT" in *javascript*) echo " ✅ MIME 已修,刷新 https://wait4x.online/papers/agensh/ 应见 PDF";; *) echo " ⚠️ 仍不对(ESA 可能缓存了旧头,稍等或 purge)";; esac