Skip to content

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Jun 27, 2025

Resolves #1107, Resolves #1111, Resolves #1110

Currently, there are several ways to register a h3 sub-app, as a middleware, route handler or fetch handler. While they work, all are opaque as we lose information about sub-app other than a Request=>Response interface. Additionally, having multiple nested routers adds performance overhead, and we cannot access the route meta of nested apps.

This PR solves all issues by natively supporting mounting an H3 sub-app using app.mount(base, app):

  • Nested middleware will be auto-grouped with the base prefix to main app.
  • Nested routes will be prefixed and added to the main top-level router.
import { H3, serve, redirect } from "h3";

const nestedApp = new H3().get("/test", () => "/test (sub app)");

const app = new H3()
  .get("/", (event) => redirect(event, "/api/test"))
  .mount("/api", nestedApp);

With this, we encourage .mount over other methods (#1128) and simple routes info is accessable from app._routes (#1127) including new meta (#1118)

Copy link

cloudflare-workers-and-pages bot commented Jun 27, 2025

Deploying h3dev with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4e32f63
Status: ✅  Deploy successful!
Preview URL: https://7b739745.h3dev.pages.dev
Branch Preview URL: https://feat-mount-h3.h3dev.pages.dev

View logs

Copy link

codecov bot commented Jun 27, 2025

Codecov Report

Attention: Patch coverage is 96.15385% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/h3.ts 96.15% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@pi0
Copy link
Member Author

pi0 commented Jun 27, 2025

Updating docs in next PR for sub-apps.

@pi0 pi0 merged commit 331875f into main Jun 27, 2025
5 checks passed
@pi0 pi0 deleted the feat/mount-h3 branch June 27, 2025 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

List all routes, including those in nested apps Improve matchedRoute for nested apps The Nested routing is not elegant enough

1 participant