Server
PluginServerAPI — register HTTP middleware and server lifecycle hooks for Boltdocs plugins without writing a Vite plugin.
ctx.server — PluginServerAPI
Registers HTTP middleware and server lifecycle hooks without needing
to write a Vite plugin. This is the formal API for what plugins like
@bdocs/plugin-ask-ai used to do manually with
vitePlugins: [{ configureServer }].
ts
// Register a Connect-style middleware (runs on every request)
ctx.server.use((req, res, next) => {
const start = Date.now()
res.on('finish', () => {
ctx.logger.info(`${req.method} ${req.url} — ${Date.now() - start}ms`)
})
next()
})
// Register a middleware scoped to a specific path prefix
ctx.server.useAt('/api/my-plugin', (req, res, next) => {
if (req.method !== 'POST') {
res.statusCode = 405
res.end('Method Not Allowed')
return
}
res.end(JSON.stringify({ ok: true }))
})
// Lifecycle callbacks
ctx.server.onStart(() => {
ctx.logger.info('My plugin server started')
})
ctx.server.onEnd(() => {
ctx.logger.info('My plugin server shutting down')
})
Middleware registered via ctx.server.use() runs on both the
dev server and the preview server. The onStart callback
fires once per process when the server initializes; onEnd
fires on shutdown or restart (use it for cleanup).
See also
- PluginContext — base context object
- Middleware — transform middleware pipeline
- HMR — file watching and custom dev server events
Last updated on October 18, 2026