Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't strip "slug" from content layer data #12168

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-wolves-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allows "slug" as a field in content layer data
7 changes: 4 additions & 3 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
13 changes: 10 additions & 3 deletions packages/astro/test/fixtures/content-layer/src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,31 @@ 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
schema: async () =>
z.object({
lastValue: z.number(),
lastUpdated: z.date(),
refreshContextData: z.record(z.unknown()),
refreshContextData: z.record(z.unknown()).optional(),
slug: z.string().optional(),
}),
},
});
Expand Down
Loading