Skip to content

Commit

Permalink
avoid 404s when switching version
Browse files Browse the repository at this point in the history
  • Loading branch information
bates64 committed Sep 27, 2023
1 parent a1bb6b4 commit d41a2e1
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/components/ProductVersionSwitcher.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ const { product } = Astro.props;
const select = document.getElementById(
"version-switcher",
) as HTMLSelectElement;
select.addEventListener("change", () => {
location.pathname = location.pathname.replace(
/\/cheerpj\d/,
`/${select.value}`,
);
select.addEventListener("change", async () => {
let href = location.pathname.replace(/\/cheerpj\d/, `/${select.value}`);

// If the page doesn't exist, redirect to the homepage
const req = await fetch(href, { method: "HEAD" });
if (req.status === 404) {
href = `/${select.value}`;
}

location.href = href;
});
</script>

0 comments on commit d41a2e1

Please sign in to comment.