Last active
September 7, 2025 15:24
-
-
Save klarstrup/5b63772d9fddb5a24fa05b99c8ed63e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Router } from "express"; | |
| export function LazyRouter( | |
| loader: () => Promise<{ default: Router }>, | |
| { preload }: { preload?: boolean } = {} | |
| ) { | |
| const loadRouter = () => (router ||= loader().then((m) => m.default)); | |
| let router: Promise<Router> | null = preload ? loadRouter() : null; | |
| return Router().use((req, res, next) => | |
| loadRouter() | |
| .then((r) => r(req, res, next)) | |
| .catch(next) | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment