Skip to content

Commit c745051

Browse files
committed
Merge branch 'more-helpful-404'
This topic branch makes the 404 page a bit more helpful by detecting e.g. when readers try to look at old manual page versions that are no longer served. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 5f8bd9f + 53ba929 commit c745051

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

content/404.html

+58-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,64 @@
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+
let 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+
if (result.status < 200 || result.status >= 300) {
29+
el.innerHTML = `The page <code>${command}</code> does not exist in <a href="${
30+
path.substring(0, path.length - command.length - 1)
31+
}>the documentation</a>.`
32+
return
33+
}
34+
35+
const div = document.createElement('div')
36+
div.innerHTML = await result.text()
37+
const versions = Array.from(
38+
div
39+
.querySelector('#previous-versions-dropdown')
40+
.querySelectorAll('.version')
41+
).map(e => e.innerHTML)
42+
43+
const versionCompare = (a, b) => {
44+
b = String(b).split('.')
45+
for (const p of String(a).split('.')) {
46+
const q = b.shift()
47+
if (isNaN(q)) return +1
48+
if (p != q) return p - q
49+
}
50+
return b.length > 0 ? -1 : 0
51+
}
52+
53+
let i = -1
54+
while (i + 1 < versions.length && versionCompare(version, versions[i + 1]) < 0) i++
55+
if (versions[i]) el.innerHTML += `<br />The most closesely matching page describes version <a href="${
56+
path}/${versions[i]}${window.location.search}${window.location.hash
57+
}">${versions[i]}</a>.`
58+
})().catch(console.error)
59+
}
60+
61+
match = window.location.pathname.match(/^(.*\/book\/([^/]*))(\/.*)$/)
62+
if (match) {
63+
const [, path, rest] = match
64+
65+
const el = document.querySelector('#explanation')
66+
el.innerHTML = `This book page was not found. <a href="${
67+
path}${window.location.search}${window.location.hash
68+
}">The book's front page is here</a>.`
69+
</script>
1370
</div>

0 commit comments

Comments
 (0)