Skip to content

Commit

Permalink
修复 404 页面跳转
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Jan 1, 2025
1 parent b6bfbf4 commit bd53060
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions templates/layouts/base.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@
<!-- 基础样式文件 -->
<link rel="stylesheet" href="/assets/pageforge.min.css">

<script>
(function () {
document.addEventListener('click', function (e) {
var link = e.target.closest('a');
if (!link) return;
const href = link.getAttribute('href');
if (!href || href.startsWith('http') || href.startsWith('#')) return;
e.preventDefault();
// 获取目标语言
const targetLang = href.split('/')[1];
fetch(href)
.then(response => {
if (response.ok) {
window.location.href = href;
}
else {
window.location.href = `/${targetLang}/404.html`;
}
})
.catch(() => {
window.location.href = `/${targetLang}/404.html`;
});
});
})();
</script>

<!-- 加载主题配置中的额外 CSS 文件 -->
<% if (locals.siteData?.theme?.extraCss && Array.isArray(locals.siteData?.theme?.extraCss)) { %>
<% locals.siteData?.theme?.extraCss.forEach(css_path => { %>
Expand Down Expand Up @@ -141,19 +171,6 @@
<% } %>
</body>
<!-- 处理 404 -->
<script>
(function () {
const i18nEnable = `<%- locals?.siteData?.feature?.i18n?.enable %>`;
if (!i18nEnable) return;
const hasRule = `<%= (locals?.pageData?.rules || []).includes(pageData.noLocalePath) %>`;
if (!hasRule) {
window.location.href = `/<%= pageData.language %>/404.html` || `/404.html`;
}
})();
</script>
<!-- 基础脚本文件 -->
<script src="/assets/pageforge.min.js"></script>
Expand Down

0 comments on commit bd53060

Please sign in to comment.