Live reloads for Publish

Oct 19, 2021

Publish is a great tool to use when writing your own blog – I used it to write this blog. I think Publish is really great, and I love being able to use Swift for my blog posts. But when I was starting up, setting up my own blog, I really missed the possibility of using live reloads. I'm sure this is probably something that will come eventually be baked into the publish CLI, but at the time of writing it is not included.

When running the local webserver with publish run you still have to go into the browser and manually reload the page each time you've rebuilt the output directory (⌘+R). But there are a lot of ways you can solve this. A colleague of mine, who is a proficient web developer, tipped me on a node package called live-server. It does exactly what you expect it to do. To run it you first have to make sure you have Node installed. Then you'll have to run

cd Output
npx live-server

from the root of your publish project, and that's it. There's no dependency added to you project, just a simple one-liner to get your web-server up and running. If you're curious to how this actually works you can inspect the resulting html to see that it injects some javascript at the end of the html <body>, which sets up a socket to a locally running web server.