mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
fix: don't strip "slug" from content layer data (#12168)
* fix: don't strip "slug" from content layer data * fix: catch fallback case
This commit is contained in:
parent
676b2c66ee
commit
1cd30852a3
4 changed files with 23 additions and 6 deletions
5
.changeset/funny-wolves-dream.md
Normal file
5
.changeset/funny-wolves-dream.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Allows "slug" as a field in content layer data
|
|
@ -167,11 +167,12 @@ export async function getEntryDataAndImages<
|
||||||
pluginContext?: PluginContext,
|
pluginContext?: PluginContext,
|
||||||
): Promise<{ data: TOutputData; imageImports: Array<string> }> {
|
): Promise<{ data: TOutputData; imageImports: Array<string> }> {
|
||||||
let data: TOutputData;
|
let data: TOutputData;
|
||||||
if (collectionConfig.type === 'data') {
|
// Legacy content collections have 'slug' removed
|
||||||
data = entry.unvalidatedData as TOutputData;
|
if (collectionConfig.type === 'content' || (collectionConfig as any)._legacy) {
|
||||||
} else {
|
|
||||||
const { slug, ...unvalidatedData } = entry.unvalidatedData;
|
const { slug, ...unvalidatedData } = entry.unvalidatedData;
|
||||||
data = unvalidatedData as TOutputData;
|
data = unvalidatedData as TOutputData;
|
||||||
|
} else {
|
||||||
|
data = entry.unvalidatedData as TOutputData;
|
||||||
}
|
}
|
||||||
|
|
||||||
let schema = collectionConfig.schema;
|
let schema = collectionConfig.schema;
|
||||||
|
|
|
@ -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 () => {
|
it('updates the store on new builds', async () => {
|
||||||
assert.equal(json.increment.data.lastValue, 1);
|
assert.equal(json.increment.data.lastValue, 1);
|
||||||
assert.equal(json.entryWithReference.data.something?.content, 'transform me');
|
assert.equal(json.entryWithReference.data.something?.content, 'transform me');
|
||||||
|
|
|
@ -198,16 +198,22 @@ const images = defineCollection({
|
||||||
const increment = defineCollection({
|
const increment = defineCollection({
|
||||||
loader: {
|
loader: {
|
||||||
name: 'increment-loader',
|
name: 'increment-loader',
|
||||||
load: async ({ store, refreshContextData }) => {
|
load: async ({ store, refreshContextData, parseData }) => {
|
||||||
const entry = store.get<{ lastValue: number }>('value');
|
const entry = store.get<{ lastValue: number }>('value');
|
||||||
const lastValue = entry?.data.lastValue ?? 0;
|
const lastValue = entry?.data.lastValue ?? 0;
|
||||||
store.set({
|
const raw = {
|
||||||
id: 'value',
|
id: 'value',
|
||||||
data: {
|
data: {
|
||||||
lastValue: lastValue + 1,
|
lastValue: lastValue + 1,
|
||||||
lastUpdated: new Date(),
|
lastUpdated: new Date(),
|
||||||
refreshContextData,
|
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
|
// Example of a loader that returns an async schema function
|
||||||
|
@ -215,7 +221,8 @@ const increment = defineCollection({
|
||||||
z.object({
|
z.object({
|
||||||
lastValue: z.number(),
|
lastValue: z.number(),
|
||||||
lastUpdated: z.date(),
|
lastUpdated: z.date(),
|
||||||
refreshContextData: z.record(z.unknown()),
|
refreshContextData: z.record(z.unknown()).optional(),
|
||||||
|
slug: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue