Skip to content

Support rebuild event from dev server #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const DEFAULT_OPTIONS = {
pathPrefix: "/", // May be overridden by Eleventy, adds a virtual base directory to your project
watch: [], // Globs to pass to separate dev server chokidar for watching
aliases: {}, // Aliasing feature
rebuildUrl: null, // POST URL to trigger rebuild
rebuildUrlToken: "", // Secret token in x-11ty-rebuild-token header
indexFileName: "index.html", // Allow custom index file name
useCache: false, // Use a cache for file contents
headers: {}, // Set default response headers
Expand Down Expand Up @@ -79,7 +81,7 @@ class EleventyDevServer {
}

constructor(name, dir, options = {}) {
debug("Creating new Dev Server instance.")
debug("Creating new Dev Server instance.");
this.name = name;
this.normalizeOptions(options);

Expand Down Expand Up @@ -117,6 +119,10 @@ class EleventyDevServer {
this.options.pathPrefix = this.cleanupPathPrefix(this.options.pathPrefix);
}

setEventBus(_eventBus) {
this.eventBus = _eventBus;
}

get watcher() {
if(this.#watcher) {
return this.#watcher;
Expand Down Expand Up @@ -481,6 +487,18 @@ class EleventyDevServer {
return res.end("");
}

if (this.options.rebuildUrl && req.url === this.options.rebuildUrl && req.method === 'POST') {
const token = req.headers['x-11ty-rebuild-token'];
if (token !== this.options.rebuildUrlToken) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
return res.end('Forbidden');
}

this.eventBus.emit('eleventyDevServer.rebuild');
res.writeHead(200);
return res.end();
}

for(let urlPatternString in this.options.onRequest) {
let fn = this.options.onRequest[urlPatternString];
let fullPath = this.getServerPath(urlPatternString);
Expand Down