Skip to content

Commit b40df32

Browse files
404.astro: improve fallback logic
1 parent 0a42684 commit b40df32

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

web/src/pages/404.astro

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,69 @@ import { OLD_WIKI_URL, OLD_WIKI_REDIRECT } from '@src/content.constants';
1212
}
1313
}}>
1414
{OLD_WIKI_REDIRECT && (
15-
<h4>You will be redirected to the old MTA Wiki in <strong><span id="countdown">5</span> seconds...</strong></h4>
15+
<h4>You will be redirected to the old MTA Wiki in <strong><span id="countdown">3</span> seconds...</strong></h4>
1616
)}
1717
</StarlightPage>
1818

1919
<script is:inline define:vars={{ OLD_WIKI_URL, OLD_WIKI_REDIRECT }}>
20-
if (!OLD_WIKI_REDIRECT) {
21-
return;
22-
}
2320

2421
async function tryFallbacks() {
2522
let path = location.pathname;
2623
// Remove trailing slash if any
2724
path = path.endsWith('/') ? path.slice(0, -1) : path;
2825

29-
const tryPath = async (prefix) => {
30-
const testPath = `${prefix}${path}`;
26+
const tryDifferentPath = async (newPath) => {
3127
try {
32-
const res = await fetch(testPath, { method: 'HEAD' });
28+
const res = await fetch(newPath, { method: 'HEAD' });
3329
if (res.ok) {
34-
window.location.replace(testPath);
30+
console.log(`Attempt OK: Redirecting to ${newPath}`);
31+
window.location.replace(newPath);
3532
return true;
3633
}
3734
} catch (_) {}
3835
return false;
3936
};
4037

41-
if (await tryPath('/reference')) return;
42-
if (await tryPath('/articles')) return;
38+
if (!path.startsWith('/reference') &&
39+
await tryDifferentPath('/reference' + path)) return;
40+
if (!path.startsWith('/articles') &&
41+
await tryDifferentPath('/articles' + path)) return;
4342

44-
// Fallback to old wiki
45-
let countdown = 5;
46-
const el = document.getElementById('countdown');
47-
const interval = setInterval(() => {
48-
countdown -= 1;
49-
if (el) el.textContent = countdown;
50-
if (countdown <= 0) {
51-
clearInterval(interval);
52-
window.location.replace(OLD_WIKI_URL + path);
43+
// Try invert case of the first letter after the first slash
44+
let newPath;
45+
if (path.charAt(0) === '/' && path.length > 1) {
46+
const firstChar = path.charAt(1);
47+
if (firstChar) {
48+
const invertedChar = firstChar === firstChar.toLowerCase() ? firstChar.toUpperCase() : firstChar.toLowerCase();
49+
newPath = path.charAt(0) + invertedChar + path.slice(2);
50+
if (await tryDifferentPath(newPath)) return;
5351
}
54-
}, 1000);
52+
}
53+
54+
// Try with articles and reference prefixes again with newPath
55+
if (newPath) {
56+
if (await tryDifferentPath('/reference' + newPath)) return;
57+
if (await tryDifferentPath('/articles' + newPath)) return;
58+
}
59+
60+
// Fallback to old wiki if nothing else worked
61+
if (!OLD_WIKI_REDIRECT) {
62+
console.log('No old wiki redirect configured, staying in 404 page');
63+
return;
64+
}
65+
let countdown = 3;
66+
const el = document.getElementById('countdown');
67+
if (el) {
68+
console.log(`Redirecting to old wiki in ${countdown} seconds...`);
69+
const interval = setInterval(() => {
70+
countdown -= 1;
71+
if (el) el.textContent = countdown;
72+
if (countdown <= 0) {
73+
clearInterval(interval);
74+
window.location.replace(OLD_WIKI_URL + path);
75+
}
76+
}, 1000);
77+
} else { console.error('Countdown element not found'); }
5578
}
5679

5780
tryFallbacks();

0 commit comments

Comments
 (0)