Skip to content

Commit

Permalink
fix blog not found
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtsam committed Jan 8, 2025
1 parent d7a09bd commit dfc068a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
28 changes: 27 additions & 1 deletion app/routes/_site+/blog_.$slug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
} from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
import { serverOnly$ } from 'vite-env-only/macros'
import {
GeneralErrorBoundary,
generalNotFoundHandler,
} from '#app/components/error-boundary.tsx'
import { getPostInfos, getPostMeta } from '#app/utils/mdx/blog.server'
import { bundleMDX } from '#app/utils/mdx/compile-mdx.server'
import { useMdxComponent } from '#app/utils/mdx/mdx'
Expand Down Expand Up @@ -44,8 +48,20 @@ export const loader = async ({ params }: LoaderFunctionArgs) => {
const slug = params.slug
const timing = new ServerTiming()

timing.time('get post mdx bundle', 'Get post mdx bundle')
timing.time('get post mdx entry', 'Get post mdx entry')
const mdxEntry = getMdxEntry('blog', slug)
if (!mdxEntry) {
timing.timeEnd('get post mdx entry')
throw new Response('Not found', {
status: 404,
headers: {
'Server-Timing': timing.toString(),
},
})
}
timing.timeEnd('get post mdx entry')

timing.time('get post mdx bundle', 'Get post mdx bundle')
const mdxBundle = await getMdxBundleSource(mdxEntry)
timing.timeEnd('get post mdx bundle')

Expand Down Expand Up @@ -83,3 +99,13 @@ export default function BlogPost() {
</div>
)
}

export function ErrorBoundary() {
return (
<GeneralErrorBoundary
statusHandlers={{
404: generalNotFoundHandler,
}}
/>
)
}
5 changes: 5 additions & 0 deletions app/utils/mdx/blog.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export async function getPostInfos(): Promise<Array<PostInfo>> {

try {
const entry = getMdxEntry('blog', slug)
if (!entry) {
throw new Error(
`No file named ${slug} with valid extension found in the blog directory`,
)
}
const file = await readFile(entry.mdxPath, 'utf-8')
const meta = getPostMeta(file)

Expand Down
20 changes: 17 additions & 3 deletions app/utils/mdx/mdx.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,25 @@ type Entry = {
const mdxDirPath = resolve(rootPath, 'mdx')
const VALID_EXTENSION = ['md', 'mdx']

export function getMdxEntry(category: string, name: string): Entry {
export function getMdxEntry(category: string, name: string): Entry | null {
const maybeDirPath = resolve(mdxDirPath, category, name)
if (existsSync(maybeDirPath)) {
const dirPath = maybeDirPath
const mdxPath = getFilePathInDirectoryByName(
dirPath,
'index',
VALID_EXTENSION,
)

if (!mdxPath) {
return null
}

return {
name,
isFile: false,
bundlePath: dirPath,
mdxPath: getFilePathInDirectoryByName(dirPath, 'index', VALID_EXTENSION),
mdxPath,
}
}

Expand All @@ -37,6 +47,10 @@ export function getMdxEntry(category: string, name: string): Entry {
VALID_EXTENSION,
)

if (!filePath) {
return null
}

return {
name,
isFile: true,
Expand Down Expand Up @@ -76,7 +90,7 @@ async function getFilesInDirectory(

const fileSets = await Promise.all(
dir.map((dirent) =>
getFilesInDirectory(resolve(dirent.path, dirent.name), rootPath),
getFilesInDirectory(resolve(dirent.parentPath, dirent.name), rootPath),
),
)

Expand Down
4 changes: 1 addition & 3 deletions app/utils/path.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ export function getFilePathInDirectoryByName(
}
}

throw new Error(
`No file named ${name} with valid extension found in directory => ${dirPath}`,
)
return null
}

0 comments on commit dfc068a

Please sign in to comment.