#Fetch Astro md for blogging
4 messages · Page 1 of 1 (latest)
Hi @errant kite. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
For an Astro blog, the recommended way to fetch .md files for blog content is to use Astro's built-in functionality rather than creating a separate backend web service. Astro provides several methods to work with local Markdown files directly within your project. Here are the main approaches:
-
Using
import.meta.glob():
This is a simple way to fetch multiple Markdown files at once. You can use it to dynamically import all your blog post files.Astro Documentation provides an example:
--- const allPosts = Object.values(import.meta.glob('./posts/*.md', { eager: true })); --- -
Content Collections:
For more advanced use cases, Astro recommends using Content Collections. This feature allows you to organize your Markdown content into collections, providing benefits like type safety and validation.Astro Documentation shows how to use
getCollection():--- import { getCollection } from "astro:content"; const allPosts = await getCollection("blog"); --- -
Direct imports:
For individual files, you can also use direct imports.
Both import.meta.glob() and Content Collections allow you to work with your Markdown files directly within your Astro project, without the need for a separate backend service. The choice between these methods depends on your specific needs and the complexity of your project.