Backend
Performance
JSandy is built on lightweight software such as Hono and Drizzle ORM for high performance out of the box.
To further optimize your app, JSandy allows you to optimize bundle sizes and significantly reduce cold starts through built-in dynamic router and dependency loading.
Dynamically Loading Routers
Dynamically load routers using the dynamic() function to maintain high performance when scaling to many routers. This approach reduces the initial bundle size & improves cold starts:
import { j } from "./jsandy"
import { dynamic } from "@jsandy/rpc"
const api = j
.router()
.basePath("/api")
.use(j.defaults.cors)
.onError(j.defaults.errorHandler)
const appRouter = j.mergeRouters(api, {
users: dynamic(() => import("./routers/user-router")),
posts: dynamic(() => import("./routers/post-router")),
})
export type AppRouter = typeof appRouter
export default appRouterDynamic Imports in Procedures
JSandy also supports dynamic imports within procedures for code splitting at the procedure level. Just like dynamically loading routers, this approach reduces the initial bundle size and can significantly improve cold starts:
import { j, publicProcedure } from "../jsandy"
export const userRouter = j.router({
generateReport: publicProcedure.get(async ({ c }) => {
// 👇 Dynamically import heavy dependencies
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 })
})
})