feat: hot-swap route handlers in dev mode#41
Merged
Merged
Conversation
Route handlers were captured by value in closures at startup and never
refreshed — changing an API handler required a full server restart.
Add a `routeModule` option pointing to the routes file. In dev mode,
when a dependency of that module changes the dev watcher re-bundles it
via `Bun.build({ packages: 'external' })`, reimports with cache
busting, and updates mutable handler maps that route closures read
lazily on each request. WS handlers already used lazy lookup via
`wsHandlersMap` so only API, SSE, and page serverProps/actions needed
new indirection.
Contributor
Mochi review reportTry this PRExpand instructionsgh run download -R khromov/mochi 26543966964 -n mochi-framework-pr -D /tmp/mochi-pr && bun i /tmp/mochi-pr/mochi-framework-pr.tgzDependency reportExpand reportLines of codepackages/mochi
Unchanged: packages/docs
packages/site
Unchanged: packages/demos
Unchanged: packages/minimal
Unchanged: packages/cli
Unchanged: |
Probe for ./src/routes.ts (then .js) in dev mode so route HMR works out of the box without explicit config. routeModule option remains as an override. Remove the now-redundant explicit routeModule from all three site configs.
Replace non-null assertions on HMR handler map lookups with fallbacks to the originally captured handler, and sequence the initial route module build through the reload chain to prevent a race with early file changes.
Merged
khromov
pushed a commit
that referenced
this pull request
May 29, 2026
🤖 I have created a release *beep* *boop* --- <details><summary>mochi-framework: 0.5.0</summary> ## [0.5.0](mochi-framework-v0.4.0...mochi-framework-v0.5.0) (2026-05-28) ### Features * hot-swap route handlers in dev mode ([#41](#41)) ([106a4a2](106a4a2)) * opt-in route warmup to pre-warm static pages at startup ([#64](#64)) ([9f28ede](9f28ede)) * replace highlight.js with Shiki ([#54](#54)) ([3d2dc6a](3d2dc6a)) ### Code Refactoring * replace `cookie` npm package with Bun native cookie APIs ([#60](#60)) ([4e0d953](4e0d953)) * run all tests in per-file isolation, drop .isolated.test.ts suffix ([#55](#55)) ([68cdc06](68cdc06)) * vendor debounce, drop npm dependency ([#63](#63)) ([d465fea](d465fea)) * vendor json-format-highlight, drop npm dependency ([#61](#61)) ([0b88399](0b88399)) </details> --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
serverProps/actionswere captured by value at startup and never refreshed — changing a handler required a full server restartrouteModuleoption toMochiServeOptionspointing to the routes fileBun.build({ packages: 'external' }), reimports with cache busting, and hot-swaps handler references through mutable mapswsHandlersMap— only API, SSE, and page routes needed new indirectiondevelopment && routeModuleTest plan
bun run checkspasses (lint, format, typecheck, tests)/health, edited handler to addhmr: truefield, hit again — response reflected the change without restartserverPropsHMR by editing a data-loading function