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, '>')
}
/** 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 = [
'',
'',
...entries.map(
(e) =>
` ${xmlEscape(e.loc)}${e.lastmod.slice(0, 10)}`,
),
'',
'',
].join('\n')
return new Response(body, {
headers: {
'Content-Type': 'application/xml; charset=utf-8',
'Cache-Control': 'public, max-age=3600',
},
})
},
} satisfies Action
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