HMR Support
Enable Hot Module Replacement for your Girouette routes
Girouette supports Hot Module Replacement (HMR) during development, it means that any changes in your code unrelated to Girouette (like controller logic or services) will automatically invalidate your files without restarting the server.
HMR support is automatically configured when you install Girouette using node ace add @adonisjs-community/girouette. The indexControllers() hook is registered during installation. See the Installation guide for details.
Any changes to Girouette configuration, route definitions, or the indexControllers() hook options will still trigger a full-reload.
Usage
Start the Development Server
Run your AdonisJS development server with the --hmr flag:
node ace serve --hmrThe --hmr flag enables Hot Module Replacement for your application.
Make Changes to Your Controllers
Edit any controller's logic and save it:
import { Get } from '@adonisjs-community/girouette'
export default class UsersController {
@Get('/users')
async index() {
return { message: 'Hello from Users!' }
return { message: 'HMR is working!' }
}
}Verify HMR is Working
Check your terminal. You should see:
invalidated app/controllers/users_controller.tsYour changes should be reflected immediately in your application without restarting the server.
Customizing Controller Options
You can customize the controller scanning behavior:
import { indexControllers } from '@adonisjs-community/girouette'
export default defineConfig({
hooks: {
init: [
indexControllers({
source: './app/controllers', // Source directory
glob: ['**/*_controller.ts'], // File patterns to match
importAlias: '#controllers', // Import alias to use
}),
],
},
})How It Works
When HMR is enabled:
- Initial Scan: The
indexControllers()hook scans your controllers directory during initialization - Code Generation: A
start/routes.girouette.tsfile forwards your controllers and hmr path to Girouette - File Watcher: When you modify a controller, AdonisJS detects the change and triggers HMR
- Auto Reload: If your edits affect routing, a full-reload is triggered; otherwise, only the changed module is invalidated
- Instant Update: Changes are reflected immediately in your application
Troubleshooting
Routes Not Updating
If your routes aren't updating automatically:
-
Check the hook is registered:
adonisrc.ts hooks: { init: [ indexControllers(), ], } -
Verify the preload is added:
adonisrc.ts preloads: [ () => import('#start/routes'), () => import('#start/kernel'), () => import('#start/routes.girouette'), ] -
Check the hot-hook boundaries in your
package.json"hotHook": { "boundaries": [ "./app/**/*_controller.ts", // for nested controllers "./app/middleware/*.ts" ] }, } -
Verify HMR is enabled:
node ace serve --hmr -
Check file naming: Controllers must end with
_controller.tsby default (configurable via thegloboption inindexControllers())
Changes Require Full Restart
Some changes still triggers a full-reload:
- Changes to
adonisrc.ts(hooks, preloads, providers) or config-related files - Changes to files booted on startup, like
start/kernel.tsor evenmodels/user.ts(imported byapp_provider.ts) - Changes to middleware definitions in
start/kernel.ts - Changes to the
indexControllers()hook options
Next Steps
- Basic Routing - Learn how to define routes
- Route Groups - Organize your routes