Skip to content

Commit 7bd30d5

Browse files
committed
Support rebuild event from dev server
1 parent 0d00b03 commit 7bd30d5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

server.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const DEFAULT_OPTIONS = {
3434
pathPrefix: "/", // May be overridden by Eleventy, adds a virtual base directory to your project
3535
watch: [], // Globs to pass to separate dev server chokidar for watching
3636
aliases: {}, // Aliasing feature
37+
rebuildUrl: null, // POST URL to trigger rebuild
38+
rebuildUrlToken: "", // Secret token in x-11ty-rebuild-token header
3739
indexFileName: "index.html", // Allow custom index file name
3840
useCache: false, // Use a cache for file contents
3941
headers: {}, // Set default response headers
@@ -117,6 +119,10 @@ class EleventyDevServer {
117119
this.options.pathPrefix = this.cleanupPathPrefix(this.options.pathPrefix);
118120
}
119121

122+
setEventBus(_eventBus) {
123+
this.eventBus = _eventBus;
124+
}
125+
120126
get watcher() {
121127
if(this.#watcher) {
122128
return this.#watcher;
@@ -481,6 +487,18 @@ class EleventyDevServer {
481487
return res.end("");
482488
}
483489

490+
if (this.options.rebuildUrl && req.url === this.options.rebuildUrl && req.method === 'POST') {
491+
const token = req.headers['x-11ty-rebuild-token'];
492+
if (token !== this.options.rebuildUrlToken) {
493+
res.writeHead(403, { 'Content-Type': 'text/plain' });
494+
return res.end('Forbidden');
495+
}
496+
497+
this.eventBus.emit('eleventyDevServer.rebuild');
498+
res.writeHead(200);
499+
return res.end();
500+
}
501+
484502
for(let urlPatternString in this.options.onRequest) {
485503
let fn = this.options.onRequest[urlPatternString];
486504
let fullPath = this.getServerPath(urlPatternString);

0 commit comments

Comments
 (0)