Skip to content

Commit

Permalink
feat(404): ✨ add redirection to 404
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Liuxf committed May 16, 2024
1 parent 7e31652 commit 473afe1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
41 changes: 41 additions & 0 deletions 404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="{{ site.lang | default: " en-US" }}">

<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}">
<script>
const paths = window.location.href.split('/');
const pattern = /^(zh-CN|zh-TW|en-US|fr-FR)$/;
let lang_index = -1;
for (let i = 0; i < paths.length; ++i) {
if (pattern.test(paths[i])) {
lang_index = i;
break;
}
}

if (lang_index !== -1) {
const lang = paths[lang_index];
if (lang === 'zh-CN') {
window.location.href = paths.slice(0, 4).join('/') + '/unavailable';
} else if (lang === 'en-US' || lang === 'zh-TW') {
const new_paths = paths.slice(0, lang_index).concat('zh-CN').concat(paths.slice(lang_index + 1));
window.location.href = new_paths.join('/');
} else {
const new_paths = paths.slice(0, lang_index).concat('en-US').concat(paths.slice(lang_index + 1));
window.location.href = new_paths.join('/');
}
} else {
window.location.href = paths.slice(0, 4).join('/') + '/unavailable';
}
</script>
</head>

<body>
<h2>404</h2>
</body>

</html>
13 changes: 7 additions & 6 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ <h2>{{ site.description | default: site.github.project_tagline }}</h2>
</option>
</select>
<script>
const paths = window.location.href.split('/');
const current_url = window.location.href;
const paths = current_url.split('/');
const pattern = /^(zh-CN|zh-TW|en-US|fr-FR)$/;
let lang_index = -1;
document.getElementById("select-lang").value = 'en-US';
Expand Down Expand Up @@ -121,10 +122,10 @@ <h2>{{ site.description | default: site.github.project_tagline }}</h2>
back.innerText = 'Back';
break;
}
} else if (paths.length > 1 && paths[paths.length - 2] === 'blogs') {
back.innerText = 'Home';
} else {
} else if (paths.length == 5 && current_url.endsWith('/')) {
back.style.display = 'none';
} else {
back.innerText = 'Home';
}

const returnToBackPage = () => {
Expand All @@ -134,8 +135,8 @@ <h2>{{ site.description | default: site.github.project_tagline }}</h2>
} else {
window.location.href = paths.slice(0, lang_index + 1).join('/') + '/';
}
} else if (paths.length > 1 && paths[paths.length - 2] === 'blogs') {
window.location.href = paths.slice(0, paths.length - 2).join('/') + '/';
} else {
window.location.href = paths.slice(0, 4).join('/') + '/'
}
};

Expand Down
5 changes: 5 additions & 0 deletions unavailable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Unavailable

This resource is unavailable.

## [Back to Home](./)

0 comments on commit 473afe1

Please sign in to comment.