Skip to content

Commit 6abd848

Browse files
committed
add rough 404 page in astro to present user with a quick search option
1 parent a41dc9c commit 6abd848

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

src/pages/404.astro

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
import Layout from "../layouts/BaseLayout.astro";
3+
4+
/**
5+
* Returns the last (non-empty) segment from the slash-separated path, or ""
6+
*
7+
* @param path string to extract the last part of
8+
* @returns the last non-empty segment or an empty string
9+
*
10+
* @example
11+
* an input of `/reference/p5/basematerialshader/` will return `basematerialshader`
12+
*/
13+
function getLastPathElementOrEmptyString(path: string): string {
14+
return path.split("/").filter(Boolean).pop() ?? "";
15+
}
16+
17+
18+
const currentPath = Astro.url.pathname;
19+
const finalTerm = getLastPathElementOrEmptyString(currentPath);
20+
---
21+
22+
<Layout title="404: Page Not Found">
23+
<main class="error-container">
24+
<p>Sorry, we couldn't find your exact page <code>{currentPath}</code>.</p>
25+
26+
<div class="actions">
27+
<a
28+
href={`https://beta.p5js.org/search/?term=${finalTerm}`}
29+
class="btn secondary"
30+
>
31+
Search here for <code>{finalTerm}</code>
32+
</a>
33+
or
34+
<a href="/" class="btn">go home</a>
35+
</div>
36+
</main>
37+
</Layout>
38+
<style>
39+
.error-container {
40+
display: flex;
41+
flex-direction:column;
42+
align-items: flex-start;
43+
padding: 2rem 1rem;
44+
gap: 2rem;
45+
}
46+
.actions {
47+
display: flex;
48+
flex-direction: column;
49+
gap: 1rem;
50+
justify-content: center;
51+
}
52+
</style>

0 commit comments

Comments
 (0)