You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactors legacy `content` and `data` collections to use the Content Layer API `glob()` loader for better performance and to support backwards compatibility. Also introduces the `legacy.collections` flag for projects that are unable to update to the new behavior immediately.
6
+
7
+
:warning:**BREAKING CHANGE FOR LEGACY CONTENT COLLECTIONS**:warning:
8
+
9
+
By default, collections that use the old types (`content` or `data`) and do not define a `loader` are now implemented under the hood using the Content Layer API's built-in `glob()` loader, with extra backward-compatibility handling.
10
+
11
+
In order to achieve backwards compatibility with existing `content` collections, the following have been implemented:
12
+
13
+
- a `glob` loader collection is defined, with patterns that match the previous handling (matches `src/content/<collection name>/**/*.md` and other content extensions depending on installed integrations, with underscore-prefixed files and folders ignored)
14
+
- When used in the runtime, the entries have an ID based on the filename in the same format as legacy collections
15
+
- A `slug` field is added with the same format as before
16
+
- A `render()` method is added to the entry, so they can be called using `entry.render()`
17
+
-`getEntryBySlug` is supported
18
+
19
+
In order to achieve backwards compatibility with existing `data` collections, the following have been implemented:
20
+
21
+
- a `glob` loader collection is defined, with patterns that match the previous handling (matches `src/content/<collection name>/**/*{.json,.yaml}` and other data extensions, with underscore-prefixed files and folders ignored)
22
+
- Entries have an ID that is not slugified
23
+
-`getDataEntryById` is supported
24
+
25
+
While this backwards compatibility implementation is able to emulate most of the features of legacy collections, **there are some differences and limitations that may cause breaking changes to existing collections**:
26
+
27
+
- In previous versions of Astro, collections would be generated for all folders in `src/content/`, even if they were not defined in `src/content/config.ts`. This behavior is now deprecated, and collections should always be defined in `src/content/config.ts`. For existing collections, these can just be empty declarations (e.g. `const blog = defineCollection({})`) and Astro will implicitly define your legacy collection for you in a way that is compatible with the new loading behavior.
28
+
- The special `layout` field is not supported in Markdown collection entries. This property is intended only for standalone page files located in `src/pages/` and not likely to be in your collection entries. However, if you were using this property, you must now create dynamic routes that include your page styling.
29
+
- Sort order of generated collections is non-deterministic and platform-dependent. This means that if you are calling `getCollection()`, the order in which entries are returned may be different than before. If you need a specific order, you should sort the collection entries yourself.
30
+
-`image().refine()` is not supported. If you need to validate the properties of an image you will need to do this at runtime in your page or component.
31
+
- the `key` argument of `getEntry(collection, key)` is typed as `string`, rather than having types for every entry.
32
+
33
+
A new legacy configuration flag `legacy.collections` is added for users that want to keep their current legacy (content and data) collections behavior (available in Astro v2 - v4), or who are not yet ready to update their projects:
34
+
35
+
```js
36
+
// astro.config.mjs
37
+
import { defineConfig } from'astro/config';
38
+
39
+
exportdefaultdefineConfig({
40
+
legacy: {
41
+
collections:true
42
+
}
43
+
});
44
+
```
45
+
46
+
When set, no changes to your existing collections are necessary, and the restrictions on storing both new and old collections continue to exist: legacy collections (only) must continue to remain in `src/content/`, while new collections using a loader from the Content Layer API are forbidden in that folder.
@@ -103,6 +114,17 @@ export function glob(globOptions: GlobOptions): Loader {
103
114
});
104
115
105
116
constid=generateId({ entry, base, data });
117
+
letlegacyId: string|undefined;
118
+
119
+
if(isLegacy){
120
+
constentryURL=newURL(encodeURI(entry),base);
121
+
constlegacyOptions=getContentEntryIdAndSlug({
122
+
entry: entryURL,
123
+
contentDir: base,
124
+
collection: '',
125
+
});
126
+
legacyId=legacyOptions.id;
127
+
}
106
128
untouchedEntries.delete(id);
107
129
108
130
constexistingEntry=store.get(id);
@@ -132,6 +154,12 @@ export function glob(globOptions: GlobOptions): Loader {
132
154
filePath,
133
155
});
134
156
if(entryType.getRenderFunction){
157
+
if(isLegacy&&data.layout){
158
+
logger.error(
159
+
`The Markdown "layout" field is not supported in content collections in Astro 5. Ignoring layout for ${JSON.stringify(entry)}. Enable "legacy.collections" if you need to use the layout field.`,
0 commit comments