-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dddwa/feature/expose-mdx-route
Expose MDX content through app
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters