Skip to content

Commit ce27cb3

Browse files
KianNHthomasgauvin
authored andcommitted
[Docs Site] Make per-product 404 pages and link to SERP/llms.txt (#23587)
* [Docs Site] Make per-product 404 pages and link to SERP/llms.txt * route to 404 via worker * update test
1 parent 92a116f commit ce27cb3

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

astro.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export default defineConfig({
161161
headingLinks: false,
162162
},
163163
routeMiddleware: "./src/plugins/starlight/route-data.ts",
164+
disable404Route: true,
164165
}),
165166
liveCode({}),
166167
icon(),

src/components/404.astro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="flex flex-col items-center justify-center md:py-28">
2+
<h1>404</h1>
3+
<p>
4+
Check the URL, try using our <a id="404-search-link" href="/search/"
5+
>search</a
6+
> or try our LLM-friendly
7+
<a href="/llms.txt"> llms.txt directory</a>.
8+
</p>
9+
</div>
10+
11+
<script>
12+
const { pathname } = window.location;
13+
14+
const anchor = document.getElementById("404-search-link");
15+
16+
if (anchor) {
17+
const pretty = pathname.replaceAll("/", " ").replaceAll("-", " ").trim();
18+
19+
anchor.setAttribute("href", `/search/?q=${encodeURIComponent(pretty)}`);
20+
}
21+
</script>

src/pages/404.astro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
import StarlightPage, {
3+
type StarlightPageProps,
4+
} from "@astrojs/starlight/components/StarlightPage.astro";
5+
import FourOhFour from "~/components/404.astro";
6+
7+
const props = {
8+
frontmatter: {
9+
title: "404 - Page Not Found",
10+
template: "splash",
11+
editUrl: false,
12+
feedback: false,
13+
},
14+
hideTitle: true,
15+
hideBreadcrumbs: true,
16+
} as StarlightPageProps;
17+
---
18+
19+
<StarlightPage {...props}>
20+
<FourOhFour />
21+
</StarlightPage>

src/pages/[product]/404.astro

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
import StarlightPage, {
3+
type StarlightPageProps,
4+
} from "@astrojs/starlight/components/StarlightPage.astro";
5+
import type { GetStaticPaths } from "astro";
6+
import { getCollection } from "astro:content";
7+
8+
import FourOhFour from "~/components/404.astro";
9+
10+
export const getStaticPaths = (async () => {
11+
const sections = await getCollection("docs", (e) => {
12+
return e.id.split("/").length === 1;
13+
});
14+
15+
return sections.map((section) => {
16+
return {
17+
params: {
18+
product: section.id,
19+
},
20+
};
21+
});
22+
}) satisfies GetStaticPaths;
23+
24+
const props = {
25+
frontmatter: {
26+
title: "404 - Page Not Found",
27+
tableOfContents: false,
28+
editUrl: false,
29+
feedback: false,
30+
},
31+
hideTitle: true,
32+
hideBreadcrumbs: true,
33+
} as StarlightPageProps;
34+
---
35+
36+
<StarlightPage {...props}>
37+
<FourOhFour />
38+
</StarlightPage>

worker/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ export default class extends WorkerEntrypoint<Env> {
9898
console.error("Unknown error", error);
9999
}
100100

101-
return this.env.ASSETS.fetch(request);
101+
const response = await this.env.ASSETS.fetch(request);
102+
103+
if (response.status === 404) {
104+
const section = new URL(response.url).pathname.split("/").at(1);
105+
106+
if (!section) return response;
107+
108+
return this.env.ASSETS.fetch(`http://fakehost/${section}/404/`);
109+
}
110+
111+
return response;
102112
}
103113
}

worker/index.worker.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("Cloudflare Docs", () => {
2424
const request = new Request("http://fakehost/non-existent");
2525
const response = await SELF.fetch(request);
2626
expect(response.status).toBe(404);
27-
expect(await response.text()).toContain("Page not found.");
27+
expect(await response.text()).toContain("Check the URL,");
2828
});
2929
});
3030

@@ -283,7 +283,7 @@ describe("Cloudflare Docs", () => {
283283
const request = new Request("http://fakehost/non-existent/index.md");
284284
const response = await SELF.fetch(request);
285285
expect(response.status).toBe(404);
286-
expect(await response.text()).toContain("Page not found.");
286+
expect(await response.text()).toContain("Check the URL,");
287287
});
288288
});
289289

0 commit comments

Comments
 (0)