Backend
Performance
Performance
Shapeless is built on lightweight tools like Hono and Drizzle ORM to deliver high performance by default.
To further improve your app's performance, Shapeless supports dynamic router and dependency loading to reduce bundle sizes and speed up cold starts.
Dynamically Loading Routers
Use the dynamic()
helper to load routers only when needed. This keeps your initial bundle small and reduces cold start time:
// resources/shapeless.ts
import { app, dynamic } from "@shapelesss/core"
const api = app
.router()
.basePath("/api")
.use(app.defaults.cors)
.onError(app.defaults.errorHandler)
const appRouter = app.mergeRouters(api, {
users: dynamic(() => import("./routers/user-router")),
posts: dynamic(() => import("./routers/post-router")),
})
export type AppRouter = typeof appRouter
export default appRouter
Dynamic Imports Within Procedures
You can also do dynamic imports inside your procedure handlers to split code and defer loading of heavy dependencies until actually needed:
// resources/routers/user-router.ts
import { app, publicProcedure } from "../shapeless"
export const userRouter = app.router({
generateReport: publicProcedure.get(async ({ c }) => {
// Dynamically import heavy utilities only when this route is called
const { generatePDF } = await import("./utils/pdf-generator")
const { processData } = await import("./utils/data-processor")
const data = await processData(c.req.query())
const pdf = await generatePDF(data)
return c.json({ pdf })
}),
})
This approach helps keep your server bundle lean and your app fast, especially as it grows.