Open jods4 opened 2 months ago
The process is definitely worth simplifying, maybe even fixed. Without the current implementation, there was no invalidation at all and things were even worse... The info you provided here is really helpful, thank you. I will definitely take a look
I would like to suggest a few improvements to HMR.
I observe
updateRoutes
is only invoked when adding or removing page.In
updatePage
there's a comment:That's not accurate. Vite will notice the module has changed and invalidate it, but it will not actively push it through HMR to clients. As
auto-routes
is typically not referenced by pages or components, only by the module that create routes, it might not be reloaded for a long time, if ever. It also looks that this "late update" is not considered HMR and just creates duplicate modules in the front-end, which led me to all sorts of confusing issues. I would suggest to trigger HMR when the routes are modifiedIs this really the right place to do it though? Consider:
extendRoutes
andbeforeWriteFiles
can do a lot of shenanigans that may have different dependencies and are basically impossible to predict.For these reasons I would suggest a different approach altogether: after the
auto-routes
virtual file has been rebuilt, compute a hash. If the hash is different from previous generation, send HMR. This covers all possible changes (add/remove/update/extensibility changes) and never sends unnecessary HMR to front-end.@posva Maybe this process is worth simplifying? The obvious idea (to me) is a custom message
hot.on('auto-routes:afterUpdate', routes => ..)
but the bummer is that these messages can only be sent by server. Alternativelyauto-routes
could export aonRouteUpdated(routes => ..)
function to register listeners that are called byaccept
?