Skip to content

Commit 1dfe597

Browse files
Replace middleware redirect by client-side code in 404 page (#42)
1 parent 7795601 commit 1dfe597

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

astro.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export const latest_dir_name = latest_version.replace('.', '_');
1313
export default defineConfig({
1414
site: 'https://icu4x.unicode.org',
1515
base: '',
16+
redirects: {
17+
"/1_5": "/1_5/tutorials/quickstart",
18+
'/1_5/tutorials': '/1_5/tutorials/quickstart',
19+
"/2_0": "/2_0/tutorials/quickstart",
20+
'/2_0/tutorials': '/2_0/tutorials/quickstart',
21+
},
1622
integrations: [
1723
starlight({
1824
title: 'ICU4X',

src/content/docs/404.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: '404'
3+
template: splash
4+
editUrl: false
5+
hero:
6+
title: '404'
7+
tagline: Page not found. Check the URL or try using the search bar.
8+
---
9+
10+
import { latest_dir_name } from '../../../astro.config.mjs';
11+
12+
<script>{`
13+
window.onload = function () {
14+
const latest_dir_name = '${ latest_dir_name }';
15+
const path = window.location.pathname;
16+
// redirect /latest to the latest version
17+
if (path.startsWith('/latest')) {
18+
window.location.replace('/' + latest_dir_name + '/' + path.slice(path.length == 7 ? 7 : 8))
19+
}
20+
// redirect a semver (/2.0.1) to a directory name (/2_0)
21+
let parts = path.match(/^\\/([0-9]+)\\.([0-9])+\\.[0-9]+(\\/.*)?$/)?.groups;
22+
if (parts) {
23+
window.location.replace('/' + parts[1] + '_' + parts[2] + parts[3] || '')
24+
}
25+
};
26+
`}</script>

src/content/docs/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ hero:
1212
file: ../../assets/ICU4X_Logo_B_Glow.png
1313
actions:
1414
- text: Learn More
15-
link: ./2_0/tutorials/quickstart
15+
link: ./latest/tutorials/quickstart
1616
icon: right-arrow
1717
- text: Contribute
1818
link: https://www.github.com/unicode-org/icu4x

src/middleware.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)