Skip to content
← Public packages

@kentcdodds/photo-portfolio

A fast, photo-first portfolio site you update by chatting with your agent: galleries, about, contact, and one-step rollback.

FollowFork

Files

seo.ts

65 lines · 1.9 KB · TypeScript
import type { Action } from 'remix/router'
import { canonicalUrl, loadSite } from '../data/site.ts'
import { routes } from '../routes.ts'

function xmlEscape(value: string) {
	return value
		.replace(/&/g, '&')
		.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;')
}

/** sitemap.xml for the live site, using the configured siteUrl when set. */
export const sitemap = {
	async handler(context) {
		const site = await loadSite(context)
		const { galleries, updatedAt } = site.content
		const entries: Array<{ loc: string; lastmod: string }> = [
			{ loc: canonicalUrl(site, routes.home.href()), lastmod: updatedAt },
			{ loc: canonicalUrl(site, routes.galleries.href()), lastmod: updatedAt },
			...galleries.map((gallery) => ({
				loc: canonicalUrl(site, routes.gallery.href({ slug: gallery.slug })),
				lastmod: gallery.updatedAt,
			})),
			{ loc: canonicalUrl(site, routes.about.href()), lastmod: updatedAt },
			{
				loc: canonicalUrl(site, routes.contact.index.href()),
				lastmod: updatedAt,
			},
		]
		const body = [
			'<?xml version="1.0" encoding="UTF-8"?>',
			'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
			...entries.map(
				(e) =>
					`  <url><loc>${xmlEscape(e.loc)}</loc><lastmod>${e.lastmod.slice(0, 10)}</lastmod></url>`,
			),
			'</urlset>',
			'',
		].join('\n')
		return new Response(body, {
			headers: {
				'Content-Type': 'application/xml; charset=utf-8',
				'Cache-Control': 'public, max-age=3600',
			},
		})
	},
} satisfies Action<typeof routes.sitemap>

export const robots = {
	async handler(context) {
		const site = await loadSite(context)
		const body = [
			'User-agent: *',
			'Allow: /',
			`Sitemap: ${canonicalUrl(site, routes.sitemap.href())}`,
			'',
		].join('\n')
		return new Response(body, {
			headers: {
				'Content-Type': 'text/plain; charset=utf-8',
				'Cache-Control': 'public, max-age=3600',
			},
		})
	},
} satisfies Action<typeof routes.robots>