H
Hono2mo ago
Andre

Unexpected value of absolutePath(c) when used within a .route()

Hi. Is it expected for route() to reset the basePath context? I am on the latest version of @hono/node-server ("@hono/node-server": "^1.19.0") and stumbled uppon this behavior which I did not expect:
const AuthApp = new Hono().basePath("/auth/");

AuthApp.get("/before", (c) => {
console.log("routePath: " + routePath(c)); // logs "routePath: /auth/before"
console.log("baseRoutePath: " + baseRoutePath(c)); // logs "baseRoutePath: /auth/"
console.log("basePath: " + basePath(c)); // logs "basePath: /auth/"
return c.redirect(basePath(c) + "after"); // this redirects to "/auth/after"
});
AuthApp.get("/after", (c) => {
return c.text("after");
});
// serves AuthApp
const AuthApp = new Hono().basePath("/auth/");

AuthApp.get("/before", (c) => {
console.log("routePath: " + routePath(c)); // logs "routePath: /auth/before"
console.log("baseRoutePath: " + baseRoutePath(c)); // logs "baseRoutePath: /auth/"
console.log("basePath: " + basePath(c)); // logs "basePath: /auth/"
return c.redirect(basePath(c) + "after"); // this redirects to "/auth/after"
});
AuthApp.get("/after", (c) => {
return c.text("after");
});
// serves AuthApp
const AuthApp = new Hono().basePath("/auth/");

AuthApp.get("/before", (c) => {
console.log("routePath: " + routePath(c)); // logs "routePath: /auth/before"
console.log("baseRoutePath: " + baseRoutePath(c)); // logs "baseRoutePath: /" ???
console.log("basePath: " + basePath(c)); // logs "basePath: /" ???
return c.redirect(basePath(c) + "after"); // this redirects to "/after"
});
AuthApp.get("/after", (c) => {
return c.text("after");
});

const AuthAppUnderApp = new Hono().route("/", AuthApp); // CHANGE MADE
// serves AuthAppUnderApp
const AuthApp = new Hono().basePath("/auth/");

AuthApp.get("/before", (c) => {
console.log("routePath: " + routePath(c)); // logs "routePath: /auth/before"
console.log("baseRoutePath: " + baseRoutePath(c)); // logs "baseRoutePath: /" ???
console.log("basePath: " + basePath(c)); // logs "basePath: /" ???
return c.redirect(basePath(c) + "after"); // this redirects to "/after"
});
AuthApp.get("/after", (c) => {
return c.text("after");
});

const AuthAppUnderApp = new Hono().route("/", AuthApp); // CHANGE MADE
// serves AuthAppUnderApp
3 Replies
blair
blair2mo ago
what you're looking for is new Hono().route("/auth", AuthApp)
Andre
AndreOP2mo ago
Oh, okay So I suppose the behavior of the second example is indeed intended?

Did you find this page helpful?