mirror of
https://github.com/goauthentik/authentik
synced 2026-05-05 06:32:15 +02:00
website: Unify Netlify redirects with Docusaurus's client-side router. (#16430)
* website: Unify Netlify redirects with Docusaurus's client-side router. * website: Flesh out client-redirects. * Potential fix for code scanning alert no. 256: Incomplete string escaping or encoding Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Teffen Ellis <592134+GirlBossRush@users.noreply.github.com> * website: Use package. * website: use permanent redirect. * Apply suggestions from code review Co-authored-by: Dewi Roberts <dewi@goauthentik.io> Signed-off-by: Teffen Ellis <592134+GirlBossRush@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Dewi Roberts <dewi@goauthentik.io> Signed-off-by: Teffen Ellis <592134+GirlBossRush@users.noreply.github.com> * website: Spelling. * website: Add link. * website: Clarify. * website: Remove doc. * website: Add redirects for API and integrations. --------- Signed-off-by: Teffen Ellis <592134+GirlBossRush@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Dewi Roberts <dewi@goauthentik.io>
This commit is contained in:
@@ -1,32 +1,19 @@
|
||||
import "./styles.css";
|
||||
|
||||
import styles from "./styles.module.css";
|
||||
|
||||
import { pluckPathnameAffixes, useRedirectEntries } from "#theme/NotFound/Content/utils.ts";
|
||||
|
||||
import { RewriteIndex } from "@goauthentik/docusaurus-theme/redirects";
|
||||
|
||||
import { Redirect } from "@docusaurus/router";
|
||||
import Translate from "@docusaurus/Translate";
|
||||
import Heading from "@theme/Heading";
|
||||
import type { Props } from "@theme/NotFound/Content";
|
||||
import clsx from "clsx";
|
||||
import React, { type ReactNode, useEffect, useState } from "react";
|
||||
|
||||
const DocsPrefix = "/docs";
|
||||
|
||||
export default function NotFoundContent({ className }: Props): ReactNode {
|
||||
const [routeURL, setRouteURL] = useState<URL>();
|
||||
|
||||
useEffect(() => {
|
||||
setRouteURL(new URL(window.location.href));
|
||||
}, []);
|
||||
|
||||
if (typeof routeURL === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (routeURL.pathname.startsWith(DocsPrefix)) {
|
||||
routeURL.pathname = routeURL.pathname.slice(DocsPrefix.length);
|
||||
const [, fullPathname = ""] = routeURL.href.split(window.location.origin);
|
||||
|
||||
return <Redirect to={fullPathname} />;
|
||||
}
|
||||
import React, { memo, useEffect, useState } from "react";
|
||||
|
||||
const NotFound: React.FC<Props> = ({ className }) => {
|
||||
return (
|
||||
<main className={clsx("container margin-vert--xl", styles.container, className)}>
|
||||
<div className="row">
|
||||
@@ -52,4 +39,91 @@ export default function NotFoundContent({ className }: Props): ReactNode {
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
};
|
||||
|
||||
interface RedirectFoundProps extends Props {
|
||||
nextURL: string;
|
||||
}
|
||||
|
||||
const RedirectFound: React.FC<RedirectFoundProps> = ({ nextURL, className }) => {
|
||||
const usingRouter = nextURL.startsWith("/");
|
||||
|
||||
useEffect(() => {
|
||||
console.log("Redirecting...", { nextURL, usingRouter });
|
||||
|
||||
if (usingRouter) return;
|
||||
|
||||
window.location.assign(nextURL);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<main
|
||||
className={clsx(
|
||||
"pending-direct container margin-vert--xl",
|
||||
styles.container,
|
||||
className,
|
||||
)}
|
||||
>
|
||||
<div className="row">
|
||||
<div className={clsx("col col--8 col--offset-2", styles.delayedReveal)}>
|
||||
<Heading as="h1" className="hero__title">
|
||||
<Translate
|
||||
id="theme.NotFound.title"
|
||||
description="The title of the redirect page"
|
||||
>
|
||||
This page has moved
|
||||
</Translate>
|
||||
</Heading>
|
||||
|
||||
<p>
|
||||
<Translate
|
||||
id="theme.NotFound.p1"
|
||||
description="The first paragraph of the redirect page"
|
||||
>
|
||||
Redirecting...
|
||||
</Translate>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<a href={nextURL}>{nextURL}</a>
|
||||
</p>
|
||||
{usingRouter ? <Redirect to={nextURL} /> : null}
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
};
|
||||
|
||||
const NotFoundContentRouter: React.FC<Props> = (props) => {
|
||||
const [routeURL, setRouteURL] = useState<URL>();
|
||||
const redirects = useRedirectEntries();
|
||||
|
||||
useEffect(() => {
|
||||
setRouteURL(new URL(window.location.href));
|
||||
}, []);
|
||||
|
||||
if (typeof routeURL === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rewritesIndex = new RewriteIndex(redirects ?? []);
|
||||
const [pathname, suffix] = pluckPathnameAffixes(routeURL);
|
||||
const destination = rewritesIndex.finalDestination(pathname);
|
||||
|
||||
// Is there somewhere to redirect to that isn't the current pathname?
|
||||
if (!destination || destination === pathname) {
|
||||
console.log("No redirect found", { pathname, suffix, destination });
|
||||
|
||||
return <NotFound {...props} />;
|
||||
}
|
||||
|
||||
const nextURL = destination + suffix;
|
||||
|
||||
console.log("Redirect found", { pathname, suffix, destination, nextURL });
|
||||
|
||||
document.documentElement.dataset.redirect = "pending";
|
||||
|
||||
return <RedirectFound nextURL={nextURL} {...props} />;
|
||||
};
|
||||
|
||||
export default memo(NotFoundContentRouter);
|
||||
|
||||
Reference in New Issue
Block a user