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

router.ts

51 lines · 1.7 KB · TypeScript
import { packageContext } from 'kody:runtime'
import { createRouter } from 'remix/router'
import about from './controllers/about.tsx'
import contact from './controllers/contact.tsx'
import galleries from './controllers/galleries.tsx'
import gallery from './controllers/gallery.tsx'
import home from './controllers/home.tsx'
import media from './controllers/media.ts'
import photo from './controllers/photo.tsx'
import { enterPreview, exitPreview } from './controllers/preview.ts'
import { robots, sitemap } from './controllers/seo.ts'
import { loadSite } from './data/site.ts'
import { routes } from './routes.ts'
import { notFound } from './ui/not-found.tsx'

const router = createRouter({
	async defaultHandler(context) {
		return notFound(await loadSite(context))
	},
})

router.get(routes.home, home)
router.get(routes.galleries, galleries)
router.get(routes.gallery, gallery)
router.get(routes.photo, photo)
router.get(routes.about, about)
router.map(routes.contact, contact)
router.get(routes.media, media)
router.get(routes.sitemap, sitemap)
router.get(routes.robots, robots)
router.get(routes.preview, enterPreview)
router.get(routes.previewExit, exitPreview)
router.get(routes.health, () => Response.json({ ok: true }))

// The host strips the mount before forwarding; the route contract includes it, so remount.
function remountRequest(request: Request) {
	const appBasePath = String(packageContext?.appBasePath ?? '').replace(
		/\/+$/,
		'',
	)
	if (!appBasePath) return request
	const url = new URL(request.url)
	url.pathname = url.pathname === '/' ? appBasePath : appBasePath + url.pathname
	return new Request(url, request)
}

export default {
	fetch(request: Request) {
		return router.fetch(remountRequest(request))
	},
}