🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Fix: Content and Data Cannot Be in Same Collection

Astro's 'content and data cannot be in same collection' means a legacy collection mixes Markdown with JSON or YAML. Split them, or move to glob() loaders.

#Astro#Errors#Content Collections#Legacy

Astro throws “Content and data cannot be in same collection” when a legacy content collection folder (one defined without a loader) contains both content files, such as .md or .mdx, and data files, such as .json or .yaml. Legacy collections must be one type or the other. Move the data files into their own collection, or switch the collection to the glob() loader, which lets you choose exactly which files it reads.

Key Takeaways

  • The rule applies to legacy collections under src/content/ that have no loader.
  • Content means Markdown or MDX; data means JSON, YAML or similar.
  • The message names the collection; look in src/content/<name>/ for the odd file out.
  • Splitting data into a sibling folder and collection fixes it in minutes.
  • With glob(), a pattern such as '**/*.md' simply ignores other files.

The error

Content and data cannot be in same collection.
"blog" contains a mix of content and data entries. All entries must be of the same type.
Hint: Store data entries in a new collection separate from your content collection.

Why it happens

Legacy collections (the pre-Content Layer API, still available in Astro 7 through legacy.collectionsBackwardsCompat) infer each collection’s type from its files. A folder like this breaks the rule:

src/content/blog/
  first-post.md
  second-post.md
  authors.json      <- data file in a content collection

The same happens with a stray _meta.yaml, a data.json dropped in by a CMS, or a .json export saved next to posts.

Fix: split into two collections

Move the data into its own folder:

# Terminal
mkdir -p src/content/authors
git mv src/content/blog/authors.json src/content/authors/authors.json

Then define each with a loader. This also takes the project off the legacy path:

// src/content.config.ts
import { defineCollection, reference } from 'astro:content';
import { glob, file } from 'astro/loaders';
import { z } from 'astro/zod';

const authors = defineCollection({
  loader: file('src/content/authors/authors.json'),
  schema: z.object({ name: z.string() }),
});

const blog = defineCollection({
  loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
  schema: z.object({
    title: z.string(),
    author: reference('authors'),
  }),
});

export const collections = { blog, authors };

The JSON file should be an array of objects, each with a unique id. reference('authors') links each post to an author entry by ID; the collection references tip shows how to resolve it with getEntry().

Fix: limit what a loader reads

If you want to keep files together, give the glob() loader a pattern that only matches content:

// src/content.config.ts (excerpt)
const blog = defineCollection({
  loader: glob({ pattern: '**/*.md', base: './src/content/blog' }),
  schema: z.object({ title: z.string() }),
});

Files that do not match the pattern are ignored, so authors.json no longer belongs to blog. You can then load it from the same folder with file().

Once every collection has a loader, you can remove legacy.collectionsBackwardsCompat from astro.config.mjs. The legacy content config fix covers the full migration, the content layer loaders guide explains glob() and file(), and the content collections docs walk through a complete setup. The official upgrade notes describe legacy collections. Themes in our Blog & Editorial category already use loader-based collections.

FAQ

What is the difference between content and data collections in Astro?

In legacy collections, content collections hold Markdown or MDX with a body, and data collections hold JSON or YAML without one. With the Content Layer, a loader decides what each collection holds.

Can one Astro collection hold Markdown and JSON?

Not as a legacy collection. With loaders, each collection reads whatever its loader returns, so you control the mix with glob() patterns or separate collections.

Do I need legacy collections in Astro 7?

No. They exist for backwards compatibility. New projects should define every collection with a loader.