11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.12k stars 495 forks source link

Ability to perform data refresh without restarting server #2569

Open ashur opened 2 years ago

ashur commented 2 years ago

Summary

Gatsby has a handy development feature for refreshing data without having to stop and restart the local server.

To facilitate this, Gatsby exposes an environment variable called ENABLE_GATSBY_REFRESH_ENDPOINT.

If set to true, this will expose a /__refresh webhook that can receive POST requests to refresh the sourced content. This exposed webhook can be triggered whenever remote data changes.

Paired with a simple bookmarklet that POSTs to the endpoint, the feature allows developers to re-fetch data with a single click without leaving the browser — ex., to test content changes in a remote CMS.

Feature Request

It would be really helpful for clearing locally cached data if Eleventy added a hook for doing something similar.

For example, Eleventy configuration could accept a new event type that allows developers to define functions for refreshing data:

eleventyConfig.on('eleventy.refreshData', async () => {
  // Define what should happen to trigger data refresh
  // (ex., delete a folder on disk)
});

Ideally, these would only called if the feature is enabled (ex., by an environment variable as in Gatsby). After the refresh function has finished successfully, Eleventy would perform another build automatically, triggering a browser reload if applicable.

Alternatives

Some possible approaches that don't require native support from Eleventy:

Snapstromegon commented 2 years ago

While I strongly agree that this functionality would be useful, I don't see the benefit of exposing such an event (@ashur if you have a usecase that can only be solved with such an event, could you maybe explain it in a little more detail?).

I would personally prefer a solution where it is possible for plugins to register routes and handlers with EleventyServe (my first thought was towards serverless functions) and to provide plugins also with a way to retrigger builds. That way you could start with something simpel like "rebuild every X seconds" and build as complex logic as you want.

That way you can have one API change and provide for way more usecases. Some other interesting ones that come to mind are:

For this solution we'd need to

Considerations

Doing this will take more work than what is proposed by @ashur, but will most likely result in a more flexible API covering this and many more usecases. This will also move functions currently only possible for Eleventy into the realm of plugins at the expense of expanding the API.