diff options
author | 2024-09-10 13:18:18 +0100 | |
---|---|---|
committer | 2024-09-10 13:18:18 +0100 | |
commit | 4410130df722eae494caaa46b17c8eeb6223f160 (patch) | |
tree | 036081b66b953860d3d6e17b349783ec3f52ff17 | |
parent | f13c3577530e841a9a78bfb2b47448fad3f9bbc6 (diff) | |
download | astro-4410130df722eae494caaa46b17c8eeb6223f160.tar.gz astro-4410130df722eae494caaa46b17c8eeb6223f160.tar.zst astro-4410130df722eae494caaa46b17c8eeb6223f160.zip |
fix: add refresh context to schema for loader args (#11960)
* fix: add refresh context to schema for loader args
* fix negative match test
-rw-r--r-- | .changeset/cuddly-shoes-press.md | 5 | ||||
-rw-r--r-- | packages/astro/src/content/utils.ts | 1 | ||||
-rw-r--r-- | packages/astro/test/content-layer.test.js | 9 | ||||
-rw-r--r-- | packages/astro/test/fixtures/content-layer/src/content/config.ts | 4 |
4 files changed, 16 insertions, 3 deletions
diff --git a/.changeset/cuddly-shoes-press.md b/.changeset/cuddly-shoes-press.md new file mode 100644 index 000000000..65f9fe7ef --- /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 65e4551df..1dd1a457f 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 2692c8913..6b833085d 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 () => { @@ -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 () => { 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 a12a36e30..79412da66 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()), }), }, }); |