forked from git/git-scm.com
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
1 changed file
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,64 @@ | |
<img src="{{< relurl "images/[email protected]" >}}" alt="404" width="456" height="149" /> | ||
</p> | ||
<h1>That page doesn't exist.</h1> | ||
<p> | ||
<p id='explanation'> | ||
We recently redesigned the site and older URLs may now lead to missing pages. We apologize for the inconvenience. | ||
</p> | ||
<script> | ||
/* Be more helpful for outdated manual versions */ | ||
let match = window.location.pathname.match(/^(.*\/docs\/([^/]*))\/([0-9.]*)$/) | ||
if (match) { | ||
const [, path, command, version] = match | ||
|
||
const el = document.querySelector('#explanation') | ||
el.innerHTML = `Version ${version} of <a href="${ | ||
path}${window.location.search}${window.location.hash | ||
}">the <code>${command}</code> manual page</a> is no longer available.` | ||
|
||
const url = window.location.href.substring(0, window.location.href.length - version.length - 1) | ||
(async () => { | ||
// fetch the newest version to obtain the list of versions | ||
const result = await fetch(url) | ||
if (result.status < 200 || result.status >= 300) { | ||
el.innerHTML = `The page <code>${command}</code> does not exist in <a href="${ | ||
path.substring(0, path.length - command.length - 1) | ||
}>the documentation</a>.` | ||
return | ||
} | ||
|
||
const div = document.createElement('div') | ||
div.innerHTML = await result.text() | ||
const versions = Array.from( | ||
div | ||
.querySelector('#previous-versions-dropdown') | ||
.querySelectorAll('.version') | ||
).map(e => e.innerHTML) | ||
|
||
const versionCompare = (a, b) => { | ||
b = String(b).split('.') | ||
for (const p of String(a).split('.')) { | ||
const q = b.shift() | ||
if (isNaN(q)) return +1 | ||
if (p != q) return p - q | ||
} | ||
return b.length > 0 ? -1 : 0 | ||
} | ||
|
||
let i = -1 | ||
while (i + 1 < versions.length && versionCompare(version, versions[i + 1]) < 0) i++ | ||
if (versions[i]) el.innerHTML += `<br />The most closesely matching page describes version <a href="${ | ||
path}/${versions[i]}${window.location.search}${window.location.hash | ||
}">${versions[i]}</a>.` | ||
})().catch(console.error) | ||
} | ||
|
||
match = window.location.pathname.match(/^(.*\/book\/([^/]*))(\/.*)$/) | ||
if (match) { | ||
const [, path, rest] = match | ||
|
||
const el = document.querySelector('#explanation') | ||
el.innerHTML = `This book page was not found. <a href="${ | ||
path}${window.location.search}${window.location.hash | ||
}">The book's front page is here</a>.` | ||
</script> | ||
</div> |