From 5682e9ce57ab57150c32adb4021997a145f3956b Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 10 Sep 2024 11:59:55 +0100 Subject: [PATCH 1/2] fix: add refresh context to schema for loader args --- .changeset/cuddly-shoes-press.md | 5 +++++ packages/astro/src/content/utils.ts | 1 + packages/astro/test/content-layer.test.js | 4 +++- .../astro/test/fixtures/content-layer/src/content/config.ts | 4 +++- 4 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 .changeset/cuddly-shoes-press.md diff --git a/.changeset/cuddly-shoes-press.md b/.changeset/cuddly-shoes-press.md new file mode 100644 index 000000000000..65f9fe7ef4bf --- /dev/null +++ b/.changeset/cuddly-shoes-press.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where the refresh context data was not passed correctly to content layer loaders diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index 65e4551df335..1dd1a457fdd7 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -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(), diff --git a/packages/astro/test/content-layer.test.js b/packages/astro/test/content-layer.test.js index 2692c8913b87..e51c55c1693f 100644 --- a/packages/astro/test/content-layer.test.js +++ b/packages/astro/test/content-layer.test.js @@ -290,16 +290,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 () => { 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 a12a36e3092f..79412da6606f 100644 --- a/packages/astro/test/fixtures/content-layer/src/content/config.ts +++ b/packages/astro/test/fixtures/content-layer/src/content/config.ts @@ -123,7 +123,7 @@ 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({ @@ -131,6 +131,7 @@ const increment = defineCollection({ data: { lastValue: lastValue + 1, lastUpdated: new Date(), + refreshContextData }, }); }, @@ -139,6 +140,7 @@ const increment = defineCollection({ z.object({ lastValue: z.number(), lastUpdated: z.date(), + refreshContextData: z.record(z.unknown()), }), }, }); From b5a0043f5ca572b3c6bf5e1b7b58c51c621d0702 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 10 Sep 2024 12:25:21 +0100 Subject: [PATCH 2/2] fix negative match test --- packages/astro/test/content-layer.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/astro/test/content-layer.test.js b/packages/astro/test/content-layer.test.js index e51c55c1693f..6b833085df86 100644 --- a/packages/astro/test/content-layer.test.js +++ b/packages/astro/test/content-layer.test.js @@ -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 () => {