現代 Web 應用將大量邏輯前置至客戶端,部署品質直接影響使用者體驗與業務指標。本文揭露三個常見且嚴重的部署錯誤,並提供具體解決方案。
為了讓使用者立即拿到新版本,在檔名加入雜湊指紋:main.a1b2c3.js。伺服器為了「通用性」,攔截請求時**剝離指紋**轉發給源站:
# Nginx 錯誤配置
location /js/ {
rewrite ^/js/main\..+\.js$ /js/main.js break; # 移除指紋
proxy_pass https://origin;
}
requirejs.config({
baseUrl: '/js',
paths: {
'jquery': 'lib/jquery.min',
'lodash': 'lib/lodash.min'
},
waitSeconds: 15, // 根據使用者網路環境調整,行動裝置建議 20-30s
bundles: {
'dashboard': ['chart', 'grid', 'export'] // 預載入相關模組
}
});
// 全域錯誤捕獲
window.addEventListener('error', e => {
reportError({
message: e.message,
filename: e.filename,
lineno: e.lineno,
colno: e.colno,
stack: e.error?.stack,
userAgent: navigator.userAgent,
url: location.href,
timestamp: Date.now()
});
});
// Promise 未捕獲拒絕
window.addEventListener('unhandledrejection', e => {
reportError({
message: e.reason?.message || String(e.reason),
stack: e.reason?.stack,
type: 'unhandledrejection'
});
});
// 上報函式(批次、節流、備援)
const errorQueue = [];
function reportError(err) {
errorQueue.push(err);
if (errorQueue.length >= 10 || !window._reportTimer) {
window._reportTimer = setTimeout(flushErrors, 1000);
}
}
function flushErrors() {
const payload = errorQueue.splice(0);
navigator.sendBeacon('/api/error-report', JSON.stringify(payload));
window._reportTimer = null;
}
單純堆疊追蹤往往不足以重現問題,建議同步上報:
這三項改進不需重構業務邏輯,卻能消除最痛的部署風險,讓團隊專注於核心產品價值創造。