feat: support lazily loading routes/handlers (#3131)

We can improve cold start times by only loading route modules when the particular pattern matches. This PR introduces the concept of lazy middlewares, routes and handlers to do that.
This commit is contained in:
Marvin Hagemeister
2025-07-25 14:33:28 +02:00
committed by GitHub
parent 097fb6b27c
commit 3ad3e6ecf3
24 changed files with 529 additions and 133 deletions
+18
View File
@@ -1,4 +1,5 @@
import type { RouteHandler } from "./handlers.ts";
import type { Method } from "./router.ts";
import type { RouteComponent } from "./segments.ts";
export interface RouteConfig {
@@ -29,6 +30,12 @@ export interface RouteConfig {
* Default: `false`
*/
skipAppWrapper?: boolean;
/**
* Which method to use when loading this route lazily. This is only used
* for lazy routes.
* Default: `ALL`
*/
methods?: "ALL" | Method[];
}
export interface LayoutConfig {
@@ -51,6 +58,17 @@ export interface Route<State> {
handler?: RouteHandler<unknown, State>;
}
/**
* Lazily construct items like middlewares, handlers, etc by wrapping them
* in a function.
*/
export type Lazy<T> = () => Promise<T>;
/**
* Return T or lazy version of T
*/
export type MaybeLazy<T> = T | Lazy<T>;
// TODO: Uncomment once JSR supports global types
// declare global {
// namespace preact.createElement.JSX {