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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user