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: add refresh context to schema for loader args #11960

Merged
merged 2 commits into from
Sep 10, 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/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',
);
Comment on lines +90 to +93
Copy link
Contributor Author

@ascorbic ascorbic Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated test that was flaky because the collection order was undefined

});

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
Loading