nginx配置
location ~* "^(.+)\.js$" {
set $original_js $1;
add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
add_header Pragma "no-cache";
add_header Expires "0";
# 防止循环重定向
if ($original_js ~ "-tw$") {
break;
}
# 启用 ETag
etag on;
# 只有当请求头 lang=tw 时才重定向到 -tw.js
if ($http_cookie ~* "lang=tw") {
add_header Cache-Control "public, max-age=3600";
add_header Vary "Cookie";
rewrite ^(.+)\.js$ $1-tw.js last;
break;
}
# 优先尝试 -tw.js 文件,如果不存在则使用原文件
try_files $original_js.js $uri =404;
}
# 设置语言 Cookie 并重定向
location /setLang {
# 获取参数
set $lang_value $arg_lang;
set $redirect_url $arg_d;
# 如果 lang 参数为空,设置默认值
if ($lang_value = "") {
set $lang_value "cn";
}
# 如果重定向 URL 为空,设置默认重定向到首页
if ($redirect_url = "") {
set $redirect_url "/";
}
# 添加 Cookie 并重定向
add_header Set-Cookie "lang=$lang_value; Path=/; Max-Age=31536000"; # 有效期1年
# ⭐ 关键:添加清除缓存的响应头 ⭐
add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
add_header Pragma "no-cache";
add_header Expires "0";
# 清除其他可能的缓存头
add_header Surrogate-Control "no-store";
return 302 $redirect_url;
}
vite打包
function decodeChineseUnicodeSmart(text) {
return text.replace(/"(?:\\.|[^"\\])*"/g, (match) => {
const decoded = match.replace(/\\u([0-9a-fA-F]{4})/g, (full, hex) => {
const codePoint = parseInt(hex, 16);
// 中文字符范围判断
if (codePoint >= 0x4E00 && codePoint <= 0x9FFF) {
return String.fromCharCode(codePoint);
}
return full;
});
return decoded;
});
}
const fun = (dir) => {
fs.readdirSync(dir).forEach(file => {
const filePath = path.resolve(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
fun(filePath);
} else {
if (file.endsWith('.js')) {
// 仅替换结果的.js
const twFile = file.replace('.js', '-tw.js');
const converter = OpenCC.Converter({ from: 'cn', to: 'tw' });
const content = fs.readFileSync(filePath, 'utf-8');
const ret = converter(decodeChineseUnicodeSmart(content));
fs.writeFileSync(path.resolve(dir, twFile), ret);
}
}
})
};
fun(path.resolve(__dirname, '../../dist'));