Skip to content

Commit

Permalink
feat: Referencing doc pages with latest alias (edgexfoundry#831)
Browse files Browse the repository at this point in the history
Signed-off-by: Farshid Tavakolizadeh <[email protected]>

Co-authored-by: Mengyi Wang <[email protected]>
  • Loading branch information
farshidtz and MonicaisHer authored Aug 12, 2022
1 parent 4a176c6 commit 6249b0d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
67 changes: 67 additions & 0 deletions docs/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<!--
This is a special page used by Github Pages when a requested page is not found.
The script is misused for redirecting requests that use the "latest" alias to
the latest version of a page.
-->
<html>
<head>
<title>404 Not Found</title>
<script>
// redirectToLatest redirects the user agent to the latest version of a
// page by rewriting the path.
//
// Examples:
// https://docs.edgexfoundry.org/latest is redirected to:
// https://docs.edgexfoundry.org/2.2
//
// https://docs.edgexfoundry.org/latest/getting-started/ is redirected to:
// https://docs.edgexfoundry.org/2.2/getting-started/
function redirectToLatest()
{
let req = new XMLHttpRequest();
req.open("GET", "/versions.json", false);
req.send();
if(req.status === 200){
let versions = JSON.parse(req.responseText);

// find the version by alias
let res = versions.find(it => {
return it["aliases"].find(alias => {
return alias == "latest";
});
});

if (res && res.version) {
// rewrite the path and replace the alias with version
let pathParts = window.location.pathname.split("/");
pathParts[1] = res.version;
window.location.pathname = pathParts.join("/");
} else {
alert("Unable to find version with 'latest' alias.");
return;
}
} else {
alert("Error querying the versions file.");
return;
}
}

// When the 'latest' alias is in the path, redirect to the actual page
// instead of showing the 404 page.
if(window.location.pathname.split("/")[1]=="latest"){
redirectToLatest();
}
</script>
</head>
<body>
<h1>404 Not Found</h1>
<p>
The requested page was not found.
</p>
<p>
To visit the latest documentation, please visit
<a href="https://docs.edgexfoundry.org">https://docs.edgexfoundry.org</a>.
</p>
</body>
</html>
8 changes: 3 additions & 5 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Redirecting</title>
<script>
window.location.replace("2.2");
</script>
<meta http-equiv="refresh" content="0; url=/latest">
</head>
<body>
Redirecting to latest...
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion docs/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{ "version": "1.3", "title": "1.3-Hanoi", "aliases": [] },
{ "version": "2.0", "title": "2.0-Ireland", "aliases": [] },
{ "version": "2.1", "title": "2.1-Jakarta", "aliases": [] },
{ "version": "2.2", "title": "2.2-Kamakura", "aliases": [] },
{ "version": "2.2", "title": "2.2-Kamakura", "aliases": ["latest"] },
{ "version": "2.3", "title": "2.3-Levski", "aliases": [] }
]

0 comments on commit 6249b0d

Please sign in to comment.