Skip to content

Commit 46cea13

Browse files
committed
feat(404): implement custom 404 page for missing text-detail routes
- add 404.astro using existing layout structure - update [slug].astro to redirect when page is undefined - fix search link to respect trailingSlash: "always"
1 parent 5f3f82d commit 46cea13

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/pages/404.astro

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
import BaseLayout from "@layouts/BaseLayout.astro";
3+
4+
const rawPath = Astro.url.pathname;
5+
6+
const cleanPath =
7+
rawPath !== "/" ? rawPath.replace(/\/$/, "") : rawPath;
8+
9+
const searchText =
10+
cleanPath.split("/").filter(Boolean).pop() || "";
11+
---
12+
<BaseLayout title="404: Page Not Found">
13+
<section class="flex flex-col gap-8 items-start max-w-none prose px-2 py-8">
14+
15+
<p>
16+
Sorry, we couldn't find your exact page <code>{cleanPath}</code>.
17+
</p>
18+
19+
<div class="flex flex-col gap-4">
20+
<a class="btn" href={`/search/?term=${searchText}`}>
21+
Search here for <code>{searchText}</code>
22+
</a>
23+
24+
<span>or</span>
25+
26+
<a class="btn secondary" href="/">
27+
go home
28+
</a>
29+
</div>
30+
31+
</section>
32+
</BaseLayout>

src/pages/[slug].astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export const getStaticPaths = async () => {
2626
};
2727
2828
const { page } = Astro.props;
29+
if (!page) {
30+
return Astro.redirect("/404");
31+
}
2932
---
30-
3133
<TextDetailLayout page={page} />

0 commit comments

Comments
 (0)