Table Of Contents
It’s been pretty useful to try making web servers with as little framework usage as possible. With Node, you can get pretty far just using the included http
package.
Every front-end dev setup comes included with a live-reload function - the page reloading in reaction to file changes. It’s pretty useful and in theory not terribly complicated to make from scratch.
I was wondering what the simplest implementation of this would look like. Here’s what I came up with.
#Minimal Live Reload
First, we set up our listeners/subscribers - pages that are waiting for reload signals.
When a reload signal is received, we tell all listeners to reload.
On pages that we want to reload, we set up a SSE listener to some path and wait for a signal. On receiving the signal, we trigger a reload.
I’m using watchexec to listen for file changes and chose the path /~_
for the reload signal because it looks strange enough to be unused.
#The Full Code
Putting it all together, we can live reload a local index.html
file with the following code.
A technique more common these days is ”Hot Module Replacement”. This requires your code to have an understanding of where updates happened to reload a component of the page without reloading the page itself.
For applications that don’t use a customized framework, adding in something like this would be considerably more complicated. You can also just use Vite.
# end note