Skip to content

Commit 2b5f2a0

Browse files
committed
fix: 404 redirects
1 parent d4286a9 commit 2b5f2a0

1 file changed

Lines changed: 61 additions & 15 deletions

File tree

scripts/resources/custom404.html

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,83 @@
88
<meta http-equiv="refresh" content="1; url=https://ui5.github.io/cli" />
99
</noscript>
1010
<script>
11-
(function name(params) {
11+
(function () {
1212
"use strict";
1313

1414
var url = new URL(window.location);
1515
var path = url.pathname.split("/");
16-
var pathRegex = /\/cli\/(v[0-9]+|stable|next)/gi
16+
var pathRegex = /\/(v[0-9]+|stable|next)/gi;
1717

18-
// For "old" links- keep the path but try to redirect to the stable release
19-
if (!url.pathname.match(pathRegex)) {
18+
// Check if URL contains a version pattern
19+
var hasVersion = url.pathname.match(pathRegex);
20+
21+
if (!hasVersion) {
22+
// No version in URL - add stable
23+
// e.g., /cli/ → /cli/stable/
2024
path.splice(2, 0, "stable");
25+
url.pathname = path.join("/");
26+
window.location = url.toString();
27+
return;
28+
}
29+
30+
// Find the version part in the path
31+
var versionIndex = path.findIndex((elem) => elem.match(/^(v[0-9]+|stable|next)$/));
32+
33+
if (versionIndex > -1) {
34+
var version = path[versionIndex];
35+
36+
// Dynamically check if version exists by trying to fetch its index.html
37+
var versionPath = path.slice(0, versionIndex + 1).join("/") + "/";
38+
var testUrl = url.origin + versionPath;
39+
40+
// Create a test request to see if version exists
41+
fetch(testUrl, { method: 'HEAD' })
42+
.then(function (response) {
43+
if (response.ok) {
44+
// Version exists, but current page doesn't - show 404 within that version
45+
// Don't redirect if already on 404 to prevent loops
46+
if (!url.pathname.endsWith("/404.html") && !url.pathname.endsWith("/404/")) {
47+
var newPath = path.slice(0, versionIndex + 1).concat(["404.html"]);
48+
url.pathname = newPath.join("/");
49+
window.location = url.toString();
50+
}
51+
} else {
52+
// Version doesn't exist - redirect to stable
53+
redirectToStable();
54+
}
55+
})
56+
.catch(function () {
57+
// Network error or version doesn't exist - redirect to stable
58+
redirectToStable();
59+
});
2160
} else {
22-
// Path is not found in the stable release and no version is provided.
23-
// Then show the versioned 404 page, so the end user would still be in the same version
24-
var versionIndex = path.findIndex((elem) => elem.match(/^(v[0-9]+|stable|next)$/))
61+
// Edge case: redirect to stable
62+
redirectToStable();
63+
}
64+
65+
function redirectToStable() {
66+
var versionIndex = path.findIndex((elem) => elem.match(/^(v[0-9]+|stable|next)$/));
2567

2668
if (versionIndex > -1) {
27-
path.splice((versionIndex + 1), path.length, "404")
69+
path[versionIndex] = "stable";
70+
path = path.slice(0, versionIndex + 1);
2871
} else {
29-
// Edge case: redirect to the stable version i.e. /cli/v28372389/
30-
path = ["cli", "stable", "404"];
72+
path = ["", path[1], "stable"];
3173
}
32-
}
3374

34-
url.pathname = path.join("/");
35-
window.location = url.toString();
75+
url.pathname = path.join("/");
76+
77+
// Prevent infinite loops
78+
if (url.toString() !== window.location.toString()) {
79+
window.location = url.toString();
80+
}
81+
}
3682
})()
3783
</script>
3884
</head>
3985

4086
<body>
41-
Redirect to <a href="https://ui5.github.io/cli/stable/">the stable docs release/</a>...
87+
Redirect to <a href="https://ui5.github.io/cli/stable/">the stable docs release</a>...
4288
</body>
4389

44-
</html>
90+
</html>

0 commit comments

Comments
 (0)