@@ -12,46 +12,69 @@ import { OLD_WIKI_URL, OLD_WIKI_REDIRECT } from '@src/content.constants';
12
12
}
13
13
}} >
14
14
{ 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 >
16
16
)}
17
17
</StarlightPage >
18
18
19
19
<script is:inline define:vars ={ { OLD_WIKI_URL , OLD_WIKI_REDIRECT }} >
20
- if (!OLD_WIKI_REDIRECT) {
21
- return;
22
- }
23
20
24
21
async function tryFallbacks() {
25
22
let path = location.pathname;
26
23
// Remove trailing slash if any
27
24
path = path.endsWith('/') ? path.slice(0, -1) : path;
28
25
29
- const tryPath = async (prefix) => {
30
- const testPath = `${prefix}${path}`;
26
+ const tryDifferentPath = async (newPath) => {
31
27
try {
32
- const res = await fetch(testPath , { method: 'HEAD' });
28
+ const res = await fetch(newPath , { method: 'HEAD' });
33
29
if (res.ok) {
34
- window.location.replace(testPath);
30
+ console.log(`Attempt OK: Redirecting to ${newPath}`);
31
+ window.location.replace(newPath);
35
32
return true;
36
33
}
37
34
} catch (_) {}
38
35
return false;
39
36
};
40
37
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;
43
42
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;
53
51
}
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'); }
55
78
}
56
79
57
80
tryFallbacks();
0 commit comments