Skip to content

Commit

Permalink
Merge pull request #12 from dddwa/feature/expose-mdx-route
Browse files Browse the repository at this point in the history
Expose MDX content through app
  • Loading branch information
JakeGinnivan authored Nov 3, 2024
2 parents f23c23e + bb218be commit 1c96199
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
44 changes: 44 additions & 0 deletions website/app/routes/_layout.app-content.$.tsx
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions website/remix-routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ declare module "remix-routes" {
query: ExportedQuery<import('app/routes/app-config').SearchParams>,
};

"/app-content/*": {
params: {
"*": string | number;
} ,
query: ExportedQuery<import('app/routes/_layout.app-content.$').SearchParams>,
};

"/blog": {
params: never,
query: ExportedQuery<import('app/routes/_layout.blog._index').SearchParams>,
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 1c96199

Please sign in to comment.