diff --git a/website/app/routes/_layout.app-content.$.tsx b/website/app/routes/_layout.app-content.$.tsx new file mode 100644 index 0000000..1447c32 --- /dev/null +++ b/website/app/routes/_layout.app-content.$.tsx @@ -0,0 +1,44 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import type { HeadersFunction, LoaderFunctionArgs } from '@remix-run/node' +import { json } from '@remix-run/node' + +import { trace } from '@opentelemetry/api' +import { CACHE_CONTROL } from '~/lib/http.server' +import { getPage } from '../lib/mdx.server' + +export async function loader({ params, request, context }: LoaderFunctionArgs) { + const contentSlug = params['*'] + if (!contentSlug) { + throw new Error('Expected contentSlug param') + } + + const requestUrl = new URL(request.url) + if (requestUrl.pathname.startsWith('/static')) { + throw new Response('Not Found', { status: 404, statusText: 'Not Found' }) + } + + const post = await getPage(contentSlug, 'page') + if (!post) { + throw new Response('Not Found', { status: 404, statusText: 'Not Found' }) + } + if (post.frontmatter.draft) { + throw new Response('Not Found', { status: 404, statusText: 'Not Found' }) + } + if (!post.frontmatter.title) { + trace.getActiveSpan()?.recordException(new Error(`Missing title in frontmatter for ${contentSlug}`)) + throw new Response('Not Found', { status: 404, statusText: 'Not Found' }) + } + + return json( + { + frontmatter: post.frontmatter, + post: post.code, + }, + { headers: { 'Cache-Control': CACHE_CONTROL.doc } }, + ) +} + +export const headers: HeadersFunction = ({ loaderHeaders }) => { + // Inherit the caching headers from the loader so we don't cache 404s + return loaderHeaders +} diff --git a/website/remix-routes.d.ts b/website/remix-routes.d.ts index 35f15d7..1c68e95 100644 --- a/website/remix-routes.d.ts +++ b/website/remix-routes.d.ts @@ -44,6 +44,13 @@ declare module "remix-routes" { query: ExportedQuery, }; + "/app-content/*": { + params: { + "*": string | number; + } , + query: ExportedQuery, + }; + "/blog": { params: never, query: ExportedQuery, @@ -77,6 +84,7 @@ declare module "remix-routes" { | 'routes/_layout.$' | 'routes/_layout.agenda.($year)' | 'routes/_layout.agenda.$year.talk.$sessionId' + | 'routes/_layout.app-content.$' | 'routes/_layout.blog._index' | 'routes/_layout.blog.$slug' | 'routes/app-config'