Skip to content

Commit

Permalink
fix: add refresh context to schema for loader args (#11960)
Browse files Browse the repository at this point in the history
* fix: add refresh context to schema for loader args

* fix negative match test
  • Loading branch information
ascorbic authored Sep 10, 2024
1 parent f13c357 commit 4410130
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-shoes-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where the refresh context data was not passed correctly to content layer loaders
1 change: 1 addition & 0 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const collectionConfigParser = z.union([
parseData: z.any(),
generateDigest: z.function(z.tuple([z.any()], z.string())),
watcher: z.any().optional(),
refreshContextData: z.record(z.unknown()).optional(),
}),
],
z.unknown(),
Expand Down
9 changes: 7 additions & 2 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ describe('Content Layer', () => {
assert.ok(json.hasOwnProperty('probes'));
assert.ok(Array.isArray(json.probes));
assert.equal(json.probes.length, 5);
assert.equal(json.probes.at(-1).id, 'philae-lander', 'Voyager probes should not be included');
assert.ok(
json.probes.every(({ id }) => !id.startsWith('voyager')),
'Voyager probes should not be included',
);
});

it('Returns data entry by id', async () => {
Expand Down Expand Up @@ -290,16 +293,18 @@ describe('Content Layer', () => {
const rawJsonResponse = await fixture.fetch('/collections.json');
const initialJson = devalue.parse(await rawJsonResponse.text());
assert.equal(initialJson.increment.data.lastValue, 1);
const now = new Date().toISOString();

const refreshResponse = await fixture.fetch('/_refresh', {
method: 'POST',
body: JSON.stringify({}),
body: JSON.stringify({ now }),
});
const refreshData = await refreshResponse.json();
assert.equal(refreshData.message, 'Content refreshed successfully');
const updatedJsonResponse = await fixture.fetch('/collections.json');
const updated = devalue.parse(await updatedJsonResponse.text());
assert.equal(updated.increment.data.lastValue, 2);
assert.deepEqual(updated.increment.data.refreshContextData, { webhookBody: { now } });
});

it('updates collection when data file is changed', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ const images = defineCollection({
const increment = defineCollection({
loader: {
name: 'increment-loader',
load: async ({ store }) => {
load: async ({ store, refreshContextData }) => {
const entry = store.get<{ lastValue: number }>('value');
const lastValue = entry?.data.lastValue ?? 0;
store.set({
id: 'value',
data: {
lastValue: lastValue + 1,
lastUpdated: new Date(),
refreshContextData
},
});
},
Expand All @@ -139,6 +140,7 @@ const increment = defineCollection({
z.object({
lastValue: z.number(),
lastUpdated: z.date(),
refreshContextData: z.record(z.unknown()),
}),
},
});
Expand Down

0 comments on commit 4410130

Please sign in to comment.