Skip to content

Commit be8d9ff

Browse files
committed
improve version matching
1 parent 54d95dc commit be8d9ff

1 file changed

Lines changed: 126 additions & 9 deletions

File tree

404.html

Lines changed: 126 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,93 @@
1111
<script>
1212
window.onload=function(){
1313
var pathname = window.location.pathname;
14+
15+
var parseVersionParts=function(value){
16+
if(!value)return null;
17+
var normalized=value;
18+
if(normalized.charAt(0)==="v")normalized=normalized.substring(1);
19+
if(!/^\d+(\.\d+){1,2}$/.test(normalized))return null;
20+
21+
var tokens=normalized.split(".");
22+
var parts=[];
23+
for(var i=0;i<tokens.length;i++){
24+
parts.push(parseInt(tokens[i],10));
25+
}
26+
return parts;
27+
};
28+
29+
var compareVersions=function(left,right){
30+
for(var i=0;i<Math.max(left.length,right.length);i++){
31+
var leftValue=i<left.length?left[i]:0;
32+
var rightValue=i<right.length?right[i]:0;
33+
if(leftValue!==rightValue)return leftValue-rightValue;
34+
}
35+
return 0;
36+
};
37+
38+
var findStableRedirect=function(targetPath, callback){
39+
var match=targetPath.match(/^\/([^/]+)(\/.*)?$/);
40+
if(!match){
41+
callback(null);
42+
return;
43+
}
44+
45+
var requestedTag=match[1];
46+
var suffix=match[2]||"";
47+
var requestedParts=parseVersionParts(requestedTag);
48+
if(!requestedParts){
49+
callback(null);
50+
return;
51+
}
52+
53+
var req = new XMLHttpRequest();
54+
req.onreadystatechange = function() {
55+
if (this.readyState !== 4) {
56+
return;
57+
}
58+
if (this.status !== 200) {
59+
callback(null);
60+
return;
61+
}
62+
63+
var lines=this.responseText.split(/\r?\n/);
64+
var matches=[];
65+
for(var i=0;i<lines.length;i++){
66+
var line=lines[i].replace(/^\s+|\s+$/g, "");
67+
var stableMatch=line.match(/^v?(\d+\.\d+\.\d+)-stable$/);
68+
if(!stableMatch)continue;
69+
70+
var candidateParts=parseVersionParts(stableMatch[1]);
71+
if(!candidateParts)continue;
72+
73+
var isMatch=false;
74+
if(requestedParts.length===2){
75+
isMatch=candidateParts[0]===requestedParts[0] && candidateParts[1]===requestedParts[1];
76+
}else if(requestedParts.length===3){
77+
isMatch=compareVersions(candidateParts,requestedParts)===0;
78+
}
79+
80+
if(isMatch){
81+
matches.push({
82+
name: "v"+stableMatch[1]+"-stable",
83+
parts: candidateParts
84+
});
85+
}
86+
}
87+
88+
if(matches.length===0){
89+
callback(null);
90+
return;
91+
}
92+
93+
matches.sort(function(left,right){
94+
return compareVersions(right.parts,left.parts);
95+
});
96+
callback("/"+matches[0].name+suffix);
97+
};
98+
req.open("GET", "/index.txt", true);
99+
req.send();
100+
};
14101

15102
var redirect=function(cnt,redir){
16103
document.getElementById("spinner").style.display="block";
@@ -38,7 +125,19 @@
38125
if(time<0)time=0;
39126
timer.innerHTML=Math.floor(time);
40127
},1000);
41-
}
128+
};
129+
130+
var redirectVersion=function(redir){
131+
var cnt=document.querySelector("#txVersion");
132+
cnt.style.display="block";
133+
document.querySelector("#tx").style.display="none";
134+
135+
var link=cnt.querySelector("#redir a");
136+
link.href=redir;
137+
link.innerHTML=redir.substring(1);
138+
139+
redirect(cnt,redir);
140+
};
42141

43142
var redir=null;
44143
if(!pathname.startsWith("/v3.x")){
@@ -62,13 +161,20 @@
62161
req.send();
63162

64163
}else{ // If the missing page is not inside /v3.x (the old javadoc) then we setup a redirect
65-
// Migration: Redirect v3.2.4-stable to old javadoc
66-
if(pathname.startsWith("/v3.2.4-stable"))pathname=pathname.substring("/v3.2.4-stable".length);
67-
//
68-
redir="/v3.x"+pathname;
69-
var cnt=document.querySelector("#tx");
70-
cnt.style.display="block";
71-
redirect(cnt,redir);
164+
findStableRedirect(pathname,function(versionRedir){
165+
if(versionRedir){
166+
redirectVersion(versionRedir);
167+
return;
168+
}
169+
170+
// Migration: Redirect v3.2.4-stable to old javadoc
171+
if(pathname.startsWith("/v3.2.4-stable"))pathname=pathname.substring("/v3.2.4-stable".length);
172+
//
173+
redir="/v3.x"+pathname;
174+
var cnt=document.querySelector("#tx");
175+
cnt.style.display="block";
176+
redirect(cnt,redir);
177+
});
72178
}
73179
}
74180

@@ -90,7 +196,13 @@
90196
width:100%;
91197
text-align:center;
92198
}
93-
#tx h1,#txLatest h1{
199+
#txVersion{
200+
font-size:1.5em;
201+
width:100%;
202+
text-align:center;
203+
display:none;
204+
}
205+
#tx h1,#txLatest h1,#txVersion h1{
94206
font-size:2em;
95207

96208
}
@@ -146,5 +258,10 @@ <h1>You are being redirected to the latest javadoc.</h1>
146258
<p>You can select a different version from <a href="/">here</a>.</p>
147259
<p id="redir">You will be automatically redirected to the <a href="/">latest javadoc</a> in <span id="time">1</span> seconds.
148260
</div>
261+
<div id="txVersion">
262+
<h1>You are being redirected to the closest stable javadoc.</h1>
263+
<p>You can select a different version from <a href="/">here</a>.</p>
264+
<p id="redir">You will be automatically redirected to <a href="/">this stable javadoc</a> in <span id="time">1</span> seconds.
265+
</div>
149266
</body>
150267
</html>

0 commit comments

Comments
 (0)