Skip to content

Commit eb3905c

Browse files
authored
Merge pull request #1257 from nbogie/nb-add-astro-404-with-case-help
Add rough custom 404 page in astro with quick search
2 parents a41dc9c + e63ed25 commit eb3905c

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

.github/workflows/beta_deploy.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Deploy beta to Netlify
1+
name: Deploy beta to Cloudflare
22

33
on:
44
# Trigger the workflow every time you push to the `main` branch
@@ -20,6 +20,12 @@ jobs:
2020
steps:
2121
- name: Checkout your repository using git
2222
uses: actions/checkout@v4
23+
- name: Setup Node.js
24+
uses: actions/setup-node@v4
25+
with:
26+
node-version: '22'
27+
- name: Install dependencies
28+
run: npm ci
2329
- name: Build website
2430
run: npm run build
2531
- name: Deploy to Cloudflare Workers

src/pages/404.astro

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
import Head from "@components/Head/index.astro";
3+
import BaseLayout from "../layouts/BaseLayout.astro";
4+
import { getCurrentLocale } from "../i18n/utils.ts";
5+
const currentLocale = getCurrentLocale(Astro.url.pathname);
6+
---
7+
8+
<Head title="404: Page Not Found" locale={currentLocale} />
9+
10+
<BaseLayout title="404: Page Not Found">
11+
<section class="flex flex-col items-start gap-8 py-8 px-2 prose max-w-none">
12+
<p>
13+
Sorry, we couldn't find your exact page
14+
<code id="display-path"></code>.
15+
16+
<div class="flex flex-col gap-4">
17+
<a id="search-link" href="/search/" class="btn">
18+
Search here for <code id="display-search-term"></code>
19+
</a>
20+
or
21+
<a href="/" class="btn secondary">go home</a>
22+
</div>
23+
</p>
24+
</section>
25+
<script is:inline>
26+
(function update404PageDetails() {
27+
function getLastPathSegment(path) {
28+
return path.split("/").filter(Boolean).pop() ?? "";
29+
}
30+
31+
// 1. Get the last meaningful segment from the URL path, excluding hash part
32+
// 2. Use that in the link href to the search
33+
// 3. Also display it and the failed path
34+
35+
//(This .pathname already strips away any hash part)
36+
const realPath = window.location.pathname;
37+
const term = getLastPathSegment(realPath);
38+
39+
const displayPathEl = document.getElementById("display-path");
40+
const searchLinkEl = document.getElementById("search-link");
41+
const displaySearchTermEl = document.getElementById("display-search-term");
42+
43+
if (displayPathEl) {
44+
displayPathEl.textContent = realPath;
45+
}
46+
if (displaySearchTermEl) {
47+
displaySearchTermEl.textContent = term;
48+
}
49+
if (searchLinkEl) {
50+
searchLinkEl.href = `/search/?term=${encodeURIComponent(term)}`;
51+
}
52+
})();
53+
</script>
54+
</BaseLayout>

wrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"nodejs_compat"
1818
],
1919
"assets": {
20-
"directory": "dist"
20+
"directory": "dist",
21+
"not_found_handling": "404-page"
2122
},
2223
"env": {
2324
"beta": {

0 commit comments

Comments
 (0)