Skip to content

Commit ea9cccb

Browse files
committed
404: be more helpful about outdated versions
People used to link to specific versions of manual pages (as reported in #1927). Instead of showing a plain 404 page, try to be slightly more helpful by telling the reader that this version no longer exists, providing a link to the newest version. Then, also try to figure out what the most closely-matching page is that _is_ available, and if one is found, show that one, too. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 8861571 commit ea9cccb

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

content/404.html

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,49 @@
77
<img src="{{< relurl "images/[email protected]" >}}" alt="404" width="456" height="149" />
88
</p>
99
<h1>That page doesn't exist.</h1>
10-
<p>
10+
<p id='explanation'>
1111
We recently redesigned the site and older URLs may now lead to missing pages. We apologize for the inconvenience.
1212
</p>
13+
<script>
14+
/* Be more helpful for outdated manual versions */
15+
const match = window.location.pathname.match(/^(.*\/docs\/([^/]*))\/([0-9.]*)$/)
16+
if (match) {
17+
const [, path, command, version] = match
18+
19+
const el = document.querySelector('#explanation')
20+
el.innerHTML = `Version ${version} of <a href="${
21+
path}${window.location.search}${window.location.hash
22+
}">the <code>${command}</code> manual page</a> is no longer available.`
23+
24+
const url = window.location.href.substring(0, window.location.href.length - version.length - 1);
25+
(async () => {
26+
// fetch the newest version to obtain the list of versions
27+
const result = await fetch(url)
28+
29+
const div = document.createElement('div')
30+
div.innerHTML = await result.text()
31+
const versions = Array.from(
32+
div
33+
.querySelector('#previous-versions-dropdown')
34+
.querySelectorAll('.version')
35+
).map(e => e.innerHTML)
36+
37+
const versionCompare = (a, b) => {
38+
b = String(b).split('.')
39+
for (const p of String(a).split('.')) {
40+
const q = b.shift()
41+
if (isNaN(q)) return +1
42+
if (p != q) return p - q
43+
}
44+
return b.length > 0 ? -1 : 0
45+
}
46+
47+
let i = -1
48+
while (i + 1 < versions.length && versionCompare(version, versions[i + 1]) < 0) i++
49+
if (versions[i]) el.innerHTML += `<br />The most closesely matching page describes version <a href="${
50+
path}/${versions[i]}${window.location.search}${window.location.hash
51+
}">${versions[i]}</a>.`
52+
})().catch(console.error)
53+
}
54+
</script>
1355
</div>

0 commit comments

Comments
 (0)