Move content collection config outside src/content/
#551
Replies: 4 comments 4 replies
-
Here are a few initial ideas for where collection config could live:
// astro.config.mjs
import { defineConfig, defineCollection, z /** could come from astro:content too? */ } from 'astro/config';
export default defineConfig({
collections: { blog }
});
const blog = defineCollection({
schema: z.object({
title: z.string(),
}),
}); With this second option, users could also continue storing collections in a separate file to ease migration. They would just need to import this file into the // my-collections.mjs
import { defineCollection, z } from 'astro/config';
export default defineConfig({
collections: { blog }
});
const blog = defineCollection({
schema: z.object({
title: z.string(),
}),
});
export const collections = { blog };
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { collections } from 'my-collections.mjs';
export default defineConfig({
collections,
}); Open questions:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for kicking this off Ben. One of the goals here is a little unclear to me:
Why is this a goal? In the motivation section you talk about data collections and I think that's part of the reason for this goal. Is the reason that we should be able to define schemas for content and data in the same place? If so could you make that a goal as well? |
Beta Was this translation helpful? Give feedback.
-
Update: the fact that my "run |
Beta Was this translation helpful? Give feedback.
-
In v5, the new default location for the content layer config is |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Summary
Introduce a way to configure content collections from your project root, instead of a file within
src/content/
.Background & Motivation
Today, content collections are configured from a
config.ts
file nested insrc/content/
. This is intuitive, though it has some flaws from tech debt and user perspectives:src
, developers (understandably) expect Vite aliases and exotic file imports (ex. importing a const from a.jsx
file) to work. This config is loaded at build time rather than runtime to ensure collection schema errors are resolved before you deploy your site. To support all of Vite's features while also loading separately from the rest of your build, we've needed a complex flow to spin up and spin down a separate Vite server. This has caused multiple p4-important bug fixes ([Content collections] Load content config with full Vite setup astro#6092, [Content collections] Surface content config errors in overlay astro#6170, Fix: stop executingastro:server:setup
twice astro#6693) to handle properly.src/content/
directory. This closes the door to movingcontent/
outside ofsrc
, which theme explorations like Astro Starbook (a documentation starter) could benefit from. It also makes introducing new concepts like asrc/data/
directory feel unnatural.Each of these concerns could be addressed by moving the config outside of the
src/content/
directory.Goals
src/data/
directory for data content collectionsBeta Was this translation helpful? Give feedback.
All reactions