Blog db the lazy way

When I wanted to start this small blog I wanted to avoid using databases to keep things simple and cost effective. I already use React/Nextjs so I went to the nextjs docs.

Markdown

Nextjs can support mdx files so thats what I setup my project to use. The convenience with markdown is its easy to write and verify versatile. When there is a custom component that needs to be used you can use html notation to embed your custom component.

Storing posts

Instead of relying on a database I am saving the posts in a content folder within ./src/content/md. Then I created a couple of server utils to get all post ids and the post content.

/* ./src/server/getPostIds.ts */ import fs from 'fs' export const getPostIds = () =>{ const files = fs.readdirSync(process.cwd() + '/src/content/md') return files.map(x=> x.replace('.md', '')) }
/* ./src/server/getPost.ts */ import fs from 'fs' export const getPost = (postId: string) => { const name = postId + '.md' try { const path = `${process.cwd()}/src/content/md/${postId}.md` const content = fs.readFileSync(path, 'utf-8') return { name, content } } catch (error) { return { name, content: '' } } }

With those two utils I am able to get all of the posts to render a preview in /posts and render the content within /posts/posts-id.

Happy coding folks! 👋

Comments 🔐
Join the discussion