-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow integrations to refresh content layer data (#11878)
* Allow integrations to refresh content layer data This reverts commit 90a862f. * Add test * Add changeset * Add wait in test * Dispose of queue * Skip if no content layer collections * Use spaces in markdown * fix: don't keep data store in node_modules during dev * Lint * Fix test * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> * Re-order tests * Wait for data store * Lint * Handle case where Vite already knows about save --------- Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
4563ffc
commit 334948c
Showing
13 changed files
with
209 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Adds a new function `refreshContent` to the `astro:server:setup` hook that allows integrations to refresh the content layer. This can be used, for example, to register a webhook endpoint during dev, or to open a socket to a CMS to listen for changes. | ||
|
||
By default, `refreshContent` will refresh all collections. You can optionally pass a `loaders` property, which is an array of loader names. If provided, only collections that use those loaders will be refreshed. For example, A CMS integration could use this property to only refresh its own collections. | ||
|
||
You can also pass a `context` object to the loaders. This can be used to pass arbitrary data, such as the webhook body, or an event from the websocket. | ||
|
||
```ts | ||
{ | ||
name: 'my-integration', | ||
hooks: { | ||
'astro:server:setup': async ({ server, refreshContent }) => { | ||
server.middlewares.use('/_refresh', async (req, res) => { | ||
if(req.method !== 'POST') { | ||
res.statusCode = 405 | ||
res.end('Method Not Allowed'); | ||
return | ||
} | ||
let body = ''; | ||
req.on('data', chunk => { | ||
body += chunk.toString(); | ||
}); | ||
req.on('end', async () => { | ||
try { | ||
const webhookBody = JSON.parse(body); | ||
await refreshContent({ | ||
context: { webhookBody }, | ||
loaders: ['my-loader'] | ||
}); | ||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify({ message: 'Content refreshed successfully' })); | ||
} catch (error) { | ||
res.writeHead(500, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify({ error: 'Failed to refresh content: ' + error.message })); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.