Public
HTTP
iamseeley-multiroutepreact.web.val.run
June 26, 2024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @title Multi-Route Website with Preact
* @description Create a personal portfolio website with multiple pages using Preact and server-side rendering.
*/
/** @jsxImportSource npm:preact */
import { h } from 'npm:preact';
import renderToString from 'npm:preact-render-to-string';
import { websiteStyles } from "https://esm.town/v/iamseeley/websiteStyles";
import { personalData } from "https://esm.town/v/iamseeley/personalData";
// Navigation component
const Navigation = ({ currentPath }) => (
<nav>
{[
{ path: '/', text: 'Home' },
{ path: '/about', text: 'About' },
{ path: '/projects', text: 'Projects' }
].map(link => (
<a
key={link.path}
href={link.path}
class={currentPath === link.path ? 'active' : ''}
>
{link.text}
</a>
))}
</nav>
);
// Page components
const HomePage = () => (
<div>
<Navigation currentPath="/" />
<main>
<h2>Welcome to My Portfolio</h2>
<p>{personalData.summary}</p>
<p><strong>This website is rendered server-side with Preact.</strong></p>
<p>Check out the <a href='https://www.val.town/v/iamseeley/multiroutePreact'>source</a> and start building your own site!</p>
</main>
</div>
);
const AboutPage = () => (
<div>
<Navigation currentPath="/about" />
<main>
<h2>About Me</h2>
<p>{personalData.about}</p>
</main>
</div>
);
const ProjectsPage = () => (
<div>
<Navigation currentPath="/projects" />
<main>
<h2>My Projects</h2>
{personalData.projects.map((project, index) => (
<a key={index} href={project.url} target="_blank" class="projectLink">
<div class="project">
<h3>{project.name}</h3>
<p>{project.description}</p>
</div>
</a>
))}
</main>
</div>
);
const NotFoundPage = () => (
<div>
<Navigation currentPath="" />
<main>
<h2>404 Not Found</h2>
<p>Sorry, the page you're looking for doesn't exist.</p>
<a href="/">Return to Home</a>
</main>
</div>
);
// Main handler function
export default function (req: Request): Response {
const url = new URL(req.url);
let component;
switch (url.pathname) {
case '/':
component = <HomePage />;
break;
case '/about':
component = <AboutPage />;
break;
case '/projects':
component = <ProjectsPage />;
break;
default:
component = <NotFoundPage />;
}
const appContent = renderToString(component);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 26, 2024