Skip to content

Commit

Permalink
Adds support for pathPrefix to onRequest feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Apr 19, 2024
1 parent 610a4f7 commit 2e41ce7
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,11 @@ class EleventyDevServer {
async eleventyDevServerMiddleware(req, res, next) {
for(let urlPatternString in this.options.onRequest) {
let fn = this.options.onRequest[urlPatternString];
let p = new URLPattern({ pathname: urlPatternString });
let fullPath = this.getServerPath(urlPatternString);
let p = new URLPattern({ pathname: fullPath });

let fullUrl = this.getServerUrl("localhost", req.url);
// request url should already include pathprefix.
let fullUrl = this.getServerUrlRaw("localhost", req.url);
let match = p.exec(fullUrl);

let u = new URL(fullUrl);
Expand Down Expand Up @@ -687,17 +689,25 @@ class EleventyDevServer {
});
}

getServerUrl(host, pathname = "") {
getServerPath(pathname) {
// duplicate slashes
if(this.options.pathPrefix.endsWith("/") && pathname.startsWith("/")) {
pathname = pathname.slice(1);
}
return `${this.options.pathPrefix}${pathname}`;
}

getServerUrlRaw(host, pathname = "", isRaw = true) {
if(!this._server || !this._serverProtocol) {
throw new Error("Access to `serverUrl` property not yet available.");
}

let { port } = this._server.address();
// duplicate slashes
if(this.options.pathPrefix.endsWith("/") && pathname.startsWith("/")) {
pathname = pathname.slice(1);
}
return `${this._serverProtocol}//${host}:${port}${this.options.pathPrefix}${pathname}`;
return `${this._serverProtocol}//${host}:${port}${isRaw ? pathname : this.getServerPath(pathname)}`;
}

getServerUrl(host, pathname = "") {
return this.getServerUrlRaw(host, pathname, false);
}

async getPort() {
Expand Down

0 comments on commit 2e41ce7

Please sign in to comment.