Skip to content

Commit ab8cf68

Browse files
sarah11918ascorbicArmandPhilippotyanthomasdev
authored
support for legacy content collections removed (#12376)
Co-authored-by: Matt Kane <[email protected]> Co-authored-by: Armand Philippot <[email protected]> Co-authored-by: Yan <[email protected]>
1 parent a409a05 commit ab8cf68

File tree

5 files changed

+207
-41
lines changed

5 files changed

+207
-41
lines changed

src/content/docs/en/guides/upgrade-to/v6.mdx

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,137 @@ The following features have now been entirely removed from the code base and can
161161

162162
Projects now containing these removed features will be unable to build, and there will no longer be any supporting documentation prompting you to remove these features.
163163

164+
165+
### Removed: legacy content collections
166+
167+
<SourcePR number="14407" title="fix: remove legacy content collections"/>
168+
169+
In Astro 5.x, it was still possible to use [the original Content Collections API first introduced in Astro v2.0](https://astro.build/blog/introducing-content-collections/), either through a `legacy` configuration flag or via built-in backwards compatibility. These methods allowed you to upgrade to Astro v5 even if you were not yet ready or able to update your existing content collections to those powered by the new Content Layer API.
170+
171+
**Astro v6.0 removes this previously deprecated Content Collections API support entirely, including the `legacy.collections` flag.** All content collections must now use [the Content Layer API introduced in Astro v5.0](https://astro.build/blog/content-layer-deep-dive/) that powers all content collections. No backwards compatibility support is available.
172+
173+
#### What should I do?
174+
175+
If you had previously enabled the legacy flag, you must remove it.
176+
177+
```ts title="astro.config.mjs" del={5}
178+
import { defineConfig } from 'astro/config';
179+
180+
export default defineConfig({
181+
legacy: {
182+
collections: true,
183+
}
184+
})
185+
```
186+
187+
Additionally, if you did not upgrade your collections for Astro v5.0, ensure that your content collections are **fully updated** for the new API.
188+
189+
Astro v5.x included some automatic backwards compatibility to allow content collections to continue to work even if they had not been updated to use the new API. Therefore, your v5 collections may contain one or more legacy features that need updating to the newer API for v6, even if your project was previously error-free.
190+
191+
If you have [content collections errors](/en/reference/error-reference/#content-collection-errors) or warnings after upgrading to v6, use the following list to help you identify and upgrade any legacy features that may exist in your code.
192+
193+
##### If you have...
194+
195+
<details>
196+
<summary>no content collections configuration file</summary>
197+
Create `src/content.config.ts` and [define your collections](/en/guides/content-collections/#defining-collections) in it.
198+
</details>
199+
200+
<details>
201+
<summary>a configuration file located at `src/content/config.ts` / ([`LegacyContentConfigError`](/en/reference/errors/legacy-content-config-error/))</summary>
202+
Rename and move this file to `src/content.config.ts`
203+
</details>
204+
205+
<details>
206+
<summary>a collection that does not define a `loader`/ ([`ContentCollectionMissingALoaderError`](/en/reference/errors/content-collection-missing-loader/))</summary>
207+
208+
Import [Astro's built-in `glob()` loader](/en/guides/content-collections/#built-in-loaders) and define the `pattern` and `base` for your collection entries:
209+
210+
```ts ins={3,6}
211+
// src/content.config.ts
212+
import { defineCollection, z } from 'astro:content';
213+
import { glob } from 'astro/loaders';
214+
215+
const blog = defineCollection({
216+
loader: glob({ pattern: '**/[^_]*.{md,mdx}', base: "./src/data/blog" }),
217+
schema: z.object({
218+
title: z.string(),
219+
description: z.string(),
220+
pubDate: z.coerce.date(),
221+
updatedDate: z.coerce.date().optional(),
222+
}),
223+
});
224+
```
225+
</details>
226+
227+
<details>
228+
<summary>a collection that defines a collection type (`type: 'content'` or `type: 'data'`) / ([`ContentCollectionInvalidTypeError`](/en/reference/errors/content-collection-invalid-type/))</summary>
229+
There are no longer different types of collections. This must be deleted from your collection definition.
230+
231+
```ts del={7}
232+
// src/content.config.ts
233+
import { defineCollection, z } from 'astro:content';
234+
import { glob } from 'astro/loaders';
235+
236+
const blog = defineCollection({
237+
// For content layer you no longer define a `type`
238+
type: 'content',
239+
loader: glob({ pattern: '**/[^_]*.{md,mdx}', base: "./src/data/blog" }),
240+
schema: z.object({
241+
title: z.string(),
242+
description: z.string(),
243+
pubDate: z.coerce.date(),
244+
updatedDate: z.coerce.date().optional(),
245+
}),
246+
});
247+
```
248+
</details>
249+
250+
<details>
251+
<summary>legacy collection querying methods `getDataEntryById()` and `getEntryBySlug()` / ([`GetEntryDeprecationError`](/en/reference/errors/get-entry-deprecation-error/))</summary>
252+
Replace both methods with [`getEntry()`](/en/reference/modules/astro-content/#getentry).
253+
254+
</details>
255+
256+
<details>
257+
<summary>legacy collection querying and rendering methods that depend on a `slug` property / ([`ContentSchemaContainsSlugError`](/en/reference/errors/content-schema-contains-slug-error/))</summary>
258+
Previously, the `id` was based on the filename, and there was a `slug` property that could be used in a URL. Now the [CollectionEntry](/en/reference/modules/astro-content/#collectionentry) `id` is a slug. If you need access to the filename (previously available as the `id`), use the `filePath` property. Replace instances of `slug` with `id`:
259+
260+
```astro ins={6} del={5} title="src/pages/[slug].astro"
261+
---
262+
export async function getStaticPaths() {
263+
const posts = await getCollection('blog');
264+
return posts.map((post) => ({
265+
params: { slug: post.slug },
266+
params: { slug: post.id },
267+
props: post,
268+
}));
269+
}
270+
---
271+
```
272+
</details>
273+
274+
<details>
275+
<summary>content rendered using `entry.render()`</summary>
276+
Collection entries no longer have a `render()` method. Instead, import the `render()` function from `astro:content` and use `render(entry)`:
277+
278+
```astro title="src/pages/index.astro" ins=", render" del={6} ins={7}
279+
---
280+
import { getEntry, render } from 'astro:content';
281+
282+
const post = await getEntry('pages', 'homepage');
283+
284+
const { Content, headings } = await post.render();
285+
const { Content, headings } = await render(post);
286+
---
287+
<Content />
288+
```
289+
290+
</details>
291+
292+
<ReadMore> See [the Astro v5 upgrade guide](/en/guides/upgrade-to/v5/#legacy-v20-content-collections-api) for previous guidance about backwards compatibility of legacy collections in Astro v5 and full step-by-step instructions for upgrading legacy collections to the new Content Layer API.</ReadMore>
293+
294+
164295
### Removed: `<ViewTransitions />` component
165296

166297
<SourcePR number="14400" title="Remove deprecated ViewTransitions component"/>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
# NOTE: This file is auto-generated from 'scripts/error-docgen.mjs'
3+
# Do not make edits to it directly, they will be overwritten.
4+
# Instead, change this file: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
5+
# Translators, please remove this note and the <DontEditWarning/> component.
6+
7+
title: Content collection invalid type.
8+
i18nReady: true
9+
githubURL: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
10+
---
11+
12+
import DontEditWarning from '~/components/DontEditWarning.astro'
13+
14+
<DontEditWarning />
15+
16+
17+
> Invalid collection type "data". Remove the type from your collection definition in your content config file.
18+
19+
### What went wrong?
20+
21+
Content collections should no longer have a type field. Remove this field from your content config file.
22+
23+
See the [Astro 6 migration guide](/en/guides/upgrade-to/v6/#removed-legacy-content-collections) for more information.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
# NOTE: This file is auto-generated from 'scripts/error-docgen.mjs'
3+
# Do not make edits to it directly, they will be overwritten.
4+
# Instead, change this file: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
5+
# Translators, please remove this note and the <DontEditWarning/> component.
6+
7+
title: Content collection missing loader.
8+
i18nReady: true
9+
githubURL: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
10+
---
11+
12+
import DontEditWarning from '~/components/DontEditWarning.astro'
13+
14+
<DontEditWarning />
15+
16+
17+
> Collections must have a loader defined. Check your collection definitions in your content config file.
18+
19+
### What went wrong?
20+
21+
A content collection is missing a loader definition. Make sure that each collection in your content config file has a loader.
22+
23+
**See Also:**
24+
- [Content collections configuration](/en/guides/content-collections/#defining-the-collection-loader)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
# NOTE: This file is auto-generated from 'scripts/error-docgen.mjs'
3+
# Do not make edits to it directly, they will be overwritten.
4+
# Instead, change this file: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
5+
# Translators, please remove this note and the <DontEditWarning/> component.
6+
7+
title: Legacy content config error.
8+
i18nReady: true
9+
githubURL: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
10+
---
11+
12+
import DontEditWarning from '~/components/DontEditWarning.astro'
13+
14+
<DontEditWarning />
15+
16+
17+
> Legacy content config file found.
18+
19+
### What went wrong?
20+
21+
A legacy content config file was found. Move the file to `src/content.config.ts` and update any collection definitions if needed.
22+
23+
See the [Astro 6 migration guide](/en/guides/upgrade-to/v6/#removed-legacy-content-collections) for more information.
24+
25+
**See Also:**
26+
- [Content collections configuration](/en/guides/content-collections/#the-collection-config-file)

src/content/docs/en/reference/legacy-flags.mdx

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,11 @@ title: Legacy flags
33
i18nReady: true
44
---
55

6+
import Since from '~/components/Since.astro'
7+
68
To help some users migrate between versions of Astro, we occasionally introduce `legacy` flags.
79

810
These flags allow you to opt in to some deprecated or otherwise outdated behavior of Astro
911
in the latest version, so that you can continue to upgrade and take advantage of new Astro releases until you are able to fully update your project code.
1012

11-
import Since from '~/components/Since.astro'
12-
13-
## Collections
14-
15-
<p>
16-
17-
**Type:** `boolean`<br />
18-
**Default:** `false`<br />
19-
<Since v="5.0.0" />
20-
</p>
21-
22-
Enable legacy behavior for content collections (as used in Astro v2 through v4)
23-
24-
```js
25-
// astro.config.mjs
26-
import { defineConfig } from 'astro/config';
27-
export default defineConfig({
28-
legacy: {
29-
collections: true
30-
}
31-
});
32-
```
33-
34-
If enabled, `data` and `content` collections (only) are handled using the legacy content collections implementation. Collections with a `loader` (only) will continue to use the Content Layer API instead. Both kinds of collections may exist in the same project, each using their respective implementations.
35-
36-
The following limitations continue to exist:
37-
38-
- Any legacy (`type: 'content'` or `type: 'data'`) collections must continue to be located in the `src/content/` directory.
39-
- These legacy collections will not be transformed to implicitly use the `glob()` loader, and will instead be handled by legacy code.
40-
- Collections using the Content Layer API (with a `loader` defined) are forbidden in `src/content/`, but may exist anywhere else in your project.
41-
42-
When you are ready to remove this flag and migrate to the new Content Layer API for your legacy collections, you must define a collection for any directories in `src/content/` that you want to continue to use as a collection. It is sufficient to declare an empty collection, and Astro will implicitly generate an appropriate definition for your legacy collections:
43-
44-
```js
45-
// src/content/config.ts
46-
import { defineCollection, z } from 'astro:content';
47-
48-
const blog = defineCollection({ })
49-
50-
export const collections = { blog };
51-
```
13+
There are currently no legacy flags available. When new legacy flags become available, instructions for removing them and upgrading to the latest features of Astro will be listed below.

0 commit comments

Comments
 (0)