eleventy-dev-server icon indicating copy to clipboard operation
eleventy-dev-server copied to clipboard

Support rebuild event from dev server

Open antgel opened this issue 1 year ago • 0 comments

This feature adds the ability to define a rebuildUrl in the config. When eleventy-dev-server is running and a POST request is made to that URL, 11ty rebuilds the site.

Usage:

  eleventyConfig.setServerOptions({
    rebuildUrl: "/rebuild/",
    rebuildUrlToken: "foobar",
  });

...

$ curl -vsSo/dev/null -X POST http://localhost:8080/rebuild/
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /rebuild/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 403 Forbidden
< Content-Type: text/plain
< Date: Mon, 30 Oct 2023 11:43:04 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked
< 
{ [19 bytes data]
* Connection #0 to host localhost left intact

$ curl -vsSo/dev/null -X POST -H 'x-11ty-rebuild-token: foobar' http://localhost:8080/rebuild/
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /rebuild/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
> x-11ty-rebuild-token: foobar
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Mon, 30 Oct 2023 11:43:20 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked
< 
{ [5 bytes data]
* Connection #0 to host localhost left intact

Rationale: I'm working on a cool 11ty / Strapi project. In the cloud, when content is updated on Strapi, Strapi can send a webhook to whichever service is hosting 11ty, to rebuild the 11ty site. However this doesn't work for local development, as discussed at https://github.com/11ty/eleventy/issues/604, https://github.com/11ty/eleventy/issues/691, https://github.com/11ty/eleventy/issues/1198, https://github.com/11ty/eleventy/issues/1358, and elsewhere.

There have been suggested workarounds that generally involve the 11ty user writing an(other) HTTP server to accept the request and dumping a file somewhere to trigger a rebuild. But it is clunky and we already have an HTTP server. I reused existing EventBus functionality; really most of my time was spent reading and understanding the code.

I hope this is useful; it's my first 11ty contribution so feel free to let me know how it may be improved. :)

Corresponding 11ty PR: https://github.com/11ty/eleventy/pull/3085

Please review the commits separately, one is mere formatting.

antgel avatar Oct 30 '23 01:10 antgel