summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matt Kane <m@mk.gg> 2024-10-09 12:42:29 +0100
committerGravatar GitHub <noreply@github.com> 2024-10-09 12:42:29 +0100
commit1cd30852a3bdae1847ad4e835e503598ca5fdf5c (patch)
treec09c42f0f19d43636614b14be53128df5550d9bb
parent676b2c66ee5d991bfb8ea85ca466be690cf0e5b9 (diff)
downloadastro-1cd30852a3bdae1847ad4e835e503598ca5fdf5c.tar.gz
astro-1cd30852a3bdae1847ad4e835e503598ca5fdf5c.tar.zst
astro-1cd30852a3bdae1847ad4e835e503598ca5fdf5c.zip
fix: don't strip "slug" from content layer data (#12168)
* fix: don't strip "slug" from content layer data * fix: catch fallback case
-rw-r--r--.changeset/funny-wolves-dream.md5
-rw-r--r--packages/astro/src/content/utils.ts7
-rw-r--r--packages/astro/test/content-layer.test.js4
-rw-r--r--packages/astro/test/fixtures/content-layer/src/content/config.ts13
4 files changed, 23 insertions, 6 deletions
diff --git a/.changeset/funny-wolves-dream.md b/.changeset/funny-wolves-dream.md
new file mode 100644
index 000000000..3023efebd
--- /dev/null
+++ b/.changeset/funny-wolves-dream.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Allows "slug" as a field in content layer data
diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts
index 05c712be5..f67a7ad63 100644
--- a/packages/astro/src/content/utils.ts
+++ b/packages/astro/src/content/utils.ts
@@ -167,11 +167,12 @@ export async function getEntryDataAndImages<
pluginContext?: PluginContext,
): Promise<{ data: TOutputData; imageImports: Array<string> }> {
let data: TOutputData;
- if (collectionConfig.type === 'data') {
- data = entry.unvalidatedData as TOutputData;
- } else {
+ // Legacy content collections have 'slug' removed
+ if (collectionConfig.type === 'content' || (collectionConfig as any)._legacy) {
const { slug, ...unvalidatedData } = entry.unvalidatedData;
data = unvalidatedData as TOutputData;
+ } else {
+ data = entry.unvalidatedData as TOutputData;
}
let schema = collectionConfig.schema;
diff --git a/packages/astro/test/content-layer.test.js b/packages/astro/test/content-layer.test.js
index a46da9a78..5d89e82d8 100644
--- a/packages/astro/test/content-layer.test.js
+++ b/packages/astro/test/content-layer.test.js
@@ -260,6 +260,10 @@ describe('Content Layer', () => {
});
});
+ it('allows "slug" as a field', async () => {
+ assert.equal(json.increment.data.slug, 'slimy');
+ });
+
it('updates the store on new builds', async () => {
assert.equal(json.increment.data.lastValue, 1);
assert.equal(json.entryWithReference.data.something?.content, 'transform me');
diff --git a/packages/astro/test/fixtures/content-layer/src/content/config.ts b/packages/astro/test/fixtures/content-layer/src/content/config.ts
index 776c44f68..5a1f2ae1d 100644
--- a/packages/astro/test/fixtures/content-layer/src/content/config.ts
+++ b/packages/astro/test/fixtures/content-layer/src/content/config.ts
@@ -198,16 +198,22 @@ const images = defineCollection({
const increment = defineCollection({
loader: {
name: 'increment-loader',
- load: async ({ store, refreshContextData }) => {
+ load: async ({ store, refreshContextData, parseData }) => {
const entry = store.get<{ lastValue: number }>('value');
const lastValue = entry?.data.lastValue ?? 0;
- store.set({
+ const raw = {
id: 'value',
data: {
lastValue: lastValue + 1,
lastUpdated: new Date(),
refreshContextData,
+ slug: 'slimy'
},
+ }
+ const parsed = await parseData(raw)
+ store.set({
+ id: raw.id,
+ data: parsed,
});
},
// Example of a loader that returns an async schema function
@@ -215,7 +221,8 @@ const increment = defineCollection({
z.object({
lastValue: z.number(),
lastUpdated: z.date(),
- refreshContextData: z.record(z.unknown()),
+ refreshContextData: z.record(z.unknown()).optional(),
+ slug: z.string().optional(),
}),
},
});