Skip to content

Commit 155dcb4

Browse files
committed
fix not found
1 parent e8e10b5 commit 155dcb4

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

site/src/routes/package/$name/index.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createMemo, onMount } from "solid-js";
2-
import { createFileRoute } from "@tanstack/solid-router";
2+
import { createFileRoute, notFound } from "@tanstack/solid-router";
33
import { fetchPackageData } from "~/api.js";
4+
import NotFound from "~/components/NotFound.jsx";
45
import { PRIMITIVE_PAGE_PADDING_TOP } from "~/components/Header/Header.jsx";
56
import InfoBar from "~/components/Primitives/InfoBar.jsx";
67
import { H2 } from "~/components/prose.jsx";
@@ -13,9 +14,26 @@ import { createPrimitiveNameTooltips } from "./-components/primitive-name-toolti
1314

1415
export const Route = createFileRoute("/package/$name/")({
1516
component: PackagePage,
16-
loader: async ({ params }) => fetchPackageData(params.name),
17-
head: ({ params }) => ({
18-
meta: [{ title: `${kebabCaseToCapitalized(params.name)} — Solid Primitives` }],
17+
// Handle unknown package names (e.g. /package/wefwe) with the same 404 page
18+
// as any other missing route. We throw `notFound()` from the loader when the
19+
// dynamic JSON import fails, and a route-level `notFoundComponent` catches
20+
// it so TanStack doesn't need to walk up to the (absent) root handler.
21+
notFoundComponent: NotFound,
22+
loader: async ({ params }) => {
23+
try {
24+
return await fetchPackageData(params.name);
25+
} catch {
26+
throw notFound();
27+
}
28+
},
29+
head: ({ params, loaderData }) => ({
30+
meta: [
31+
{
32+
title: loaderData
33+
? `${kebabCaseToCapitalized(params.name)} — Solid Primitives`
34+
: "Not Found — Solid Primitives",
35+
},
36+
],
1937
}),
2038
});
2139

site/src/routes/playground/$name.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { type Component, Suspense, lazy } from "solid-js";
22
import { Dynamic } from "solid-js/web";
3-
import { createFileRoute } from "@tanstack/solid-router";
3+
import { createFileRoute, notFound } from "@tanstack/solid-router";
44
import { HEADER_HEIGHT } from "~/components/Header/Header.jsx";
5+
import NotFound from "~/components/NotFound.jsx";
56
import { ClientOnly } from "~/primitives/client-only.js";
67
import { kebabCaseToCapitalized } from "~/utils.js";
78
import "./-playground.scss";
@@ -24,8 +25,23 @@ const modules = Object.entries(
2425

2526
export const Route = createFileRoute("/playground/$name")({
2627
component: PlaygroundPage,
27-
head: ({ params }) => ({
28-
meta: [{ title: `${kebabCaseToCapitalized(params.name)} — Playground` }],
28+
// Render the 404 page for unknown package names (e.g. /playground/wefwe)
29+
// instead of an empty playground. The loader checks the glob-generated
30+
// `modules` map and throws `notFound()` when there's no match.
31+
notFoundComponent: NotFound,
32+
loader: ({ params }) => {
33+
if (!(params.name in modules)) throw notFound();
34+
return null;
35+
},
36+
head: ({ params, loaderData }) => ({
37+
meta: [
38+
{
39+
title:
40+
loaderData === null
41+
? `${kebabCaseToCapitalized(params.name)} — Playground`
42+
: "Not Found — Solid Primitives",
43+
},
44+
],
2945
}),
3046
});
3147

0 commit comments

Comments
 (0)