Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

🌐 Personal Website with Server-Side Active Navigation

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 Personal Website with Server-Side Active Navigation
* @description Create a personal portfolio website with multiple pages and server-side active navigation using an HTTP Val.
*/
import { websiteStyles } from "https://esm.town/v/iamseeley/websiteStyles";
import { personalData } from "https://esm.town/v/iamseeley/personalData";
import { generateTrackingHtml } from 'https://esm.town/v/iamseeley/counterTown';
export default async function (req: Request): Promise<Response> {
const url = new URL(req.url);
const path = url.pathname;
// Function to create navigation links with active state
function createNavLinks(currentPath: string): string {
const links = [
{ path: '/', text: 'Home' },
{ path: '/about', text: 'About' },
{ path: '/projects', text: 'Projects' }
];
return links.map(link =>
`<a href="${link.path}" class="${currentPath === link.path ? 'active' : ''}">${link.text}</a>`
).join('');
}
const trackingHtmlResponse = generateTrackingHtml(req);
const trackingHtml = await trackingHtmlResponse.text();
// Common HTML structure
function createHTML(title: string, content: string, req: Request): string {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${personalData.name} | HTML as String</title>
<style>
${websiteStyles}
</style>
</head>
<body>
<header>
<h1>${personalData.name}</h1>
<p>${personalData.label}</p>
</header>
<nav>
${createNavLinks(path)}
</nav>
<main>
${content}
</main>
<footer>
<ul>
${personalData.profiles.map((profile, index) => `
<li key={${index}}>
<a href='${profile.url}' target="_blank" rel="noopener noreferrer">
${profile.network}
</a>
</li>
`).join('')}
</ul>
</footer>
${trackingHtml}
</body>
</html>
`;
}
// Router function to handle different routes
function router(path: string, req: Request): Response {
switch (path) {
case '/':
return homePage(req);
case '/about':
return aboutPage(req);
case '/projects':
return projectsPage(req);
default:
return notFoundPage(req);
}
}
// Home page
function homePage(req: Request): Response {
const content = `
<h2>Welcome to My Portfolio</h2>
<p>${personalData.summary}</p>
<p><strong>This website is rendered server-side with pure HTML.</strong></p>
<p>Check out the <a href='https://www.val.town/v/iamseeley/multirouteHTML'>source</a> and start building your own site!</p>
`;
return new Response(createHTML("Home", content, req), {
headers: { "Content-Type": "text/html" },
});
}
// About page
function aboutPage(req: Request): Response {
iamseeley-multiroutehtml.web.val.run
September 9, 2024