更新 Dockerfile 和 docker-entrypoint.sh,動態替換 index.html 的標題與 favicon,改善社群爬蟲抓取效果

This commit is contained in:
koko 2025-08-21 17:26:33 +08:00
parent 30a632fd0c
commit d0a9aec12a
3 changed files with 5 additions and 15 deletions

View File

@ -33,8 +33,8 @@ COPY --from=builder /app/dist /usr/share/nginx/html
# 暴露 Nginx 預設的 80 端口
EXPOSE 80
# 2025-08-20
LABEL changelog="2025-08-20: 動態設定網頁標題與 favicon與logo與預設build_img標題由 VITE_APP_TITLE 環境變數控制favicon、logo、build_img.jpg 由 VITE_FILE_API_BASEURL 動態插入docker-entrypoint.sh 寫入 env.js 供前端讀取。"
# 2025-08-21
LABEL changelog="2025-08-21: index.html 的 <title> 及 favicon 由 docker-entrypoint.sh 啟動時根據環境變數直接替換社群爬蟲可正確抓取env.js 仍供前端 JS 讀取其他動態參數。"
# Nginx 已經預設啟動,所以不需要 CMD 指令
COPY docker-entrypoint.sh /docker-entrypoint.sh

View File

@ -1,3 +1,5 @@
#!/bin/sh
echo "window.env = { VITE_API_BASEURL: '${VITE_API_BASEURL}', VITE_FILE_API_BASEURL: '${VITE_FILE_API_BASEURL}', VITE_MQTT_BASEURL: '${VITE_MQTT_BASEURL}', VITE_APP_TITLE: '${VITE_APP_TITLE}' };" > /usr/share/nginx/html/env.js
sed -i "s|<title>.*</title>|<title>${VITE_APP_TITLE}</title>|g" /usr/share/nginx/html/index.html
sed -i "s|<link rel=\"icon\" href=\".*\"|<link rel=\"icon\" href=\"${VITE_FILE_API_BASEURL}/upload/favicon.ico\"|g" /usr/share/nginx/html/index.html
exec "$@"

View File

@ -11,19 +11,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cvilux EMS</title>
<script src="/env.js"></script>
<script>
// 動態設定 favicon
if (window.env && window.env.VITE_FILE_API_BASEURL) {
var link = document.createElement("link");
link.rel = "icon";
link.href = window.env.VITE_FILE_API_BASEURL + "/upload/favicon.ico";
document.head.appendChild(link);
}
// 動態設定網頁標題
if (window.env && window.env.VITE_APP_TITLE) {
document.title = window.env.VITE_APP_TITLE;
}
</script>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://code.jquery.com/ui/1.13.3/jquery-ui.js"></script>
<!-- <script type="text/javascript" src="/requirejs/config.js"></script> -->