prefer-content-collections
Prefer Content Collections over Astro.glob() or import.meta.glob() for Markdown and MDX files.
astro-doctor/prefer-content-collections
Why it matters
Astro.glob() and import.meta.glob() return untyped content objects. Content Collections provide TypeScript types via the schema you define, build-time validation that catches typos and missing fields before deploy, and caching for better performance. They are the recommended approach for structured content.
See the difference
Incorrect
Using Astro.glob()
---
// No types, no validation, runs every request
const posts = await Astro.glob('../content/blog/*.md')
---
<ul>
{posts.map(post => (
<li>{post.frontmatter.title}</li>
))}
</ul>Correct
Using Content Collections
---
import { getCollection } from 'astro:content'
// Typed, validated at build time, cached
const posts = await getCollection('blog', ({ data }) => !data.draft)
---
<ul>
{posts.map(post => (
<li>{post.data.title}</li>
))}
</ul>Configure this check
Override the default severity in your ESLint config:
import astroDoctorPlugin from '@santi020k/eslint-plugin-astro-doctor'
export default [
astroDoctorPlugin.configs.recommended,
{
rules: {
'astro-doctor/prefer-content-collections': 'error',
},
},
]Or set the severity for CLI scans in doctor.config.ts:
export default {
rules: {
'astro-doctor/prefer-content-collections': 'error',
},
}Use 'error', 'warn', or 'off'. The configuration guide covers presets and per-file overrides.
Keep exploring best practices
- prefer-class-listUse class:list directive for conditional or dynamic class names.
- no-process-envDisallow process.env in Astro files — use import.meta.env instead.
- no-legacy-astro-7-experimental-flagsMigrate experimental configuration fields removed or stabilized in Astro 7.
- prefer-env-schemaDefine an Astro env schema for variables documented in .env.example.
- prefer-pnpmUse pnpm consistently and remove competing package-manager lockfiles.
- require-client-router-script-lifecycleInitialize scripts on astro:page-load when using ClientRouter.
- require-content-configAdd a content config when src/content contains entries.
- require-fetch-default-exportRequire a default export from Astro 7 advanced-routing entrypoints.