Skip to content

Commit eed8289

Browse files
committed
add sitemap
1 parent 313266d commit eed8289

File tree

6 files changed

+98
-5
lines changed

6 files changed

+98
-5
lines changed

frontend/.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Backend API URL
2-
PUBLIC_BACKEND_API_URL=http://localhost:3000
2+
PUBLIC_BACKEND_API_URL=http://localhost:3000
3+
4+
# Public site URL
5+
PUBLIC_SITE_URL=http://localhost:5173

frontend/src/lib/utils/site.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { env } from '$env/dynamic/public';
2+
3+
const DEFAULT_SITE_URL = 'https://rustytime.shymike.dev';
4+
5+
export function getSiteUrl(): string {
6+
const siteUrl = env.PUBLIC_SITE_URL ?? DEFAULT_SITE_URL;
7+
8+
return siteUrl.replace(/\/+$/, '');
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { getSiteUrl } from '$lib/utils/site';
2+
import type { RequestHandler } from './$types';
3+
4+
const SITE_URL = getSiteUrl();
5+
const CACHE_CONTROL_HEADER = 'max-age=0, s-maxage=3600';
6+
const BODY = `User-agent: *\nDisallow:\n\nSitemap: ${SITE_URL}/sitemap.xml\n`;
7+
8+
export const prerender = true;
9+
10+
export const GET: RequestHandler = async () => {
11+
return new Response(BODY, {
12+
headers: {
13+
'Content-Type': 'text/plain; charset=utf-8',
14+
'Cache-Control': CACHE_CONTROL_HEADER
15+
}
16+
});
17+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { getSiteUrl } from '$lib/utils/site';
2+
import type { RequestHandler } from './$types';
3+
4+
type SitemapEntry = {
5+
path: string;
6+
changefreq?: string;
7+
priority?: string;
8+
lastmod?: string;
9+
};
10+
11+
const SITE_URL = getSiteUrl();
12+
const CACHE_CONTROL_HEADER = 'max-age=0, s-maxage=3600';
13+
const DEFAULT_CHANGE_FREQUENCY = 'daily';
14+
const DEFAULT_PRIORITY = '0.5';
15+
16+
const PAGE_ENTRIES: Record<string, Omit<SitemapEntry, 'path'>> = {
17+
'/': {
18+
changefreq: 'daily',
19+
priority: '1.0'
20+
}
21+
};
22+
23+
const generatedAt = new Date().toISOString();
24+
const sitemapXml = createSitemap(makeEntries(PAGE_ENTRIES, generatedAt));
25+
26+
export const prerender = true;
27+
28+
export const GET: RequestHandler = async () => {
29+
return new Response(sitemapXml, {
30+
headers: {
31+
'Content-Type': 'application/xml',
32+
'Cache-Control': CACHE_CONTROL_HEADER
33+
}
34+
});
35+
};
36+
37+
function makeEntries(
38+
pages: Record<string, Omit<SitemapEntry, 'path'>>,
39+
generatedAtIso: string
40+
): SitemapEntry[] {
41+
return Object.entries(pages)
42+
.map(([path, config]) => ({
43+
path,
44+
changefreq: config.changefreq ?? DEFAULT_CHANGE_FREQUENCY,
45+
priority: config.priority ?? DEFAULT_PRIORITY,
46+
lastmod: config.lastmod ?? generatedAtIso
47+
}))
48+
.sort((a, b) => {
49+
if (a.path === '/') return -1;
50+
if (b.path === '/') return 1;
51+
return a.path.localeCompare(b.path);
52+
});
53+
}
54+
55+
function createSitemap(entries: SitemapEntry[]): string {
56+
const urls = entries.map(formatUrlEntry).join('\n');
57+
58+
return `<?xml version="1.0" encoding="UTF-8" ?>\n<urlset\n xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"\n xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"\n xmlns:xhtml="https://www.w3.org/1999/xhtml"\n xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"\n xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"\n xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"\n>\n${urls}\n</urlset>`;
59+
}
60+
61+
function formatUrlEntry({ path, changefreq, priority, lastmod }: SitemapEntry): string {
62+
const loc = path === '/' ? SITE_URL : `${SITE_URL}${path}`;
63+
64+
return ` <url>\n <loc>${loc}</loc>\n <lastmod>${lastmod}</lastmod>\n <changefreq>${changefreq}</changefreq>\n <priority>${priority}</priority>\n </url>`;
65+
}
66+

frontend/static/robots.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

frontend/wrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"vars": {
2222
"ENVIRONMENT": "production",
23-
"PUBLIC_BACKEND_API_URL": "https://api-rustytime.shymike.dev"
23+
"PUBLIC_BACKEND_API_URL": "https://api-rustytime.shymike.dev",
24+
"PUBLIC_SITE_URL": "https://rustytime.shymike.dev"
2425
},
2526
"observability": {
2627
"logs": {

0 commit comments

Comments
 (0)