Skip to content

The agent that grows with you

fix(website): clicking a catalog entry newer than the page's bundle opens its page

Teknium79f7de0f8273
diff --git a/website/src/components/CatalogFeatured/index.tsx b/website/src/components/CatalogFeatured/index.tsx
index 86282e0754e..7c2a0ad30dd 100644
--- a/website/src/components/CatalogFeatured/index.tsx
+++ b/website/src/components/CatalogFeatured/index.tsx
@@ -1,5 +1,5 @@
import React from "react";
-import Link from "@docusaurus/Link";
+import RouteLink from "../RouteLink";
import styles from "./styles.module.css";
@@ -23,7 +23,7 @@ export default function CatalogFeatured({
description: string;
/** Falsy entries are dropped, so callers can pass optional fields inline. */
tags: Array<string | false | null | undefined>;
- /** Internal docs route (Link) or an absolute URL (new tab). */
+ /** Internal docs route (RouteLink) or an absolute URL (new tab). */
href: string;
cta: string;
}) {
@@ -53,9 +53,9 @@ export default function CatalogFeatured({
{cta} ↗
</a>
) : (
- <Link className={styles.cta} to={href}>
+ <RouteLink className={styles.cta} to={href}>
{cta} →
- </Link>
+ </RouteLink>
)}
</div>
</section>
diff --git a/website/src/components/RouteLink.tsx b/website/src/components/RouteLink.tsx
new file mode 100644
index 00000000000..4c45511ec2f
--- /dev/null
+++ b/website/src/components/RouteLink.tsx
@@ -0,0 +1,36 @@
+import React from "react";
+import Link from "@docusaurus/Link";
+import useBaseUrl from "@docusaurus/useBaseUrl";
+import routes from "@generated/routes";
+
+/**
+ * Whether this page's bundle has a client route for `to` (a docs path without the baseUrl).
+ *
+ * Catalog listings render from live JSON, but the route table is baked into the bundle, and an edge
+ * can serve that bundle from an older deploy. An entry newer than the bundle has no client route,
+ * so router navigation would render "Page Not Found" even though the server has the page.
+ */
+export function useKnownRoute(to: string): boolean {
+ return knownPaths().has(useBaseUrl(to));
+}
+
+let paths: Set<string> | undefined;
+
+function knownPaths(): Set<string> {
+ if (!paths) {
+ paths = new Set();
+ const walk = (list: typeof routes) =>
+ list.forEach((r) => {
+ paths!.add(r.path);
+ if (r.routes) walk(r.routes as typeof routes);
+ });
+ walk(routes);
+ }
+ return paths;
+}
+
+/** A router `Link` when the bundle knows the route; otherwise a plain link the server answers. */
+export default function RouteLink({ to, ...rest }: React.ComponentProps<typeof Link> & { to: string }) {
+ const href = useBaseUrl(to);
+ return useKnownRoute(to) ? <Link to={to} {...rest} /> : <a href={href} {...rest} />;
+}
diff --git a/website/src/pages/plugins/index.tsx b/website/src/pages/plugins/index.tsx
index 067b2201544..67ea1a34393 100644
--- a/website/src/pages/plugins/index.tsx
+++ b/website/src/pages/plugins/index.tsx
@@ -2,6 +2,7 @@ import React, { useState, useMemo, useCallback, useRef, useEffect } from "react"
import Layout from "@theme/Layout";
import Link from "@docusaurus/Link";
import { useHistory } from "@docusaurus/router";
+import RouteLink, { useKnownRoute } from "../../components/RouteLink";
import useBaseUrl from "@docusaurus/useBaseUrl";
import styles from "./styles.module.css";
@@ -96,12 +97,15 @@ function PluginCard({
const pagePath = pluginPagePath(plugin.name);
const history = useHistory();
const pageHref = useBaseUrl(pagePath); // <Link> adds baseUrl itself; history.push does not
+ const routed = useKnownRoute(pagePath);
// A card IS the link to the plugin's own page: nothing expands or collapses in place. Inside
// the Desktop picker iframe an in-frame navigation would leave the host's embed, so the page
// opens in a new tab there instead.
const onCardClick = onPick
? () => window.open(new URL(pageHref, window.location.href).toString(), "_blank", "noopener,noreferrer")
- : () => history.push(pageHref);
+ : routed
+ ? () => history.push(pageHref)
+ : () => window.location.assign(pageHref);
return (
<div
@@ -141,9 +145,9 @@ function PluginCard({
<span className={styles.cardIcon} title={category.label}>{category.icon}</span>
<div className={styles.cardTitleGroup}>
<h3 className={styles.cardTitle} title={plugin.name}>
- <Link className={styles.cardTitleLink} to={pagePath} onClick={(e) => e.stopPropagation()}>
+ <RouteLink className={styles.cardTitleLink} to={pagePath} onClick={(e) => e.stopPropagation()}>
{highlightMatch(plugin.name, query)}
- </Link>
+ </RouteLink>
</h3>
{typeof plugin.stars === "number" && (
<a
@@ -313,9 +317,9 @@ function PluginCard({
<CopyButton text={plugin.installCommand} />
</div>
<div className={styles.cardLinks}>
- <Link className={styles.docsLink} to={pagePath} onClick={(e) => e.stopPropagation()}>
+ <RouteLink className={styles.docsLink} to={pagePath} onClick={(e) => e.stopPropagation()}>
Plugin page →
- </Link>
+ </RouteLink>
<a
className={styles.docsLink}
href={plugin.repo}