FastHak is a web server written in Go designed for rapid front-end development. It uses Server Sent Events for live-reload, and automatically injects the necessary javascript files, allowing you to get straight to developing your awesome web app.

It is designed to serve on localhost using HTTPS, because modern web-apps need HTTPS to do awesome stuff. You need to provide your own certs. I recommend mkcert.

FastHak is a small binary you can drop directly in your project root. Here’s how to get it:

go install github.com/sean9999/fasthak

And here’s how to use it:

fasthak -dir=public -port=9443

or maybe:

fasthak -dir=~/Documents/MySite/build -port=9443 -pubkey=~/.private/localhost.pem -privkey=~/.private/localhost-key.pem

Which will serve your folder on https://localhost:9443.

The client-side code is available at https://localhost:9443/.hak/js/*.js. You do not need to include these files in your project. They are embedded in the server itself. But you will want to point to them from your project. Ex:

//	index.js
import { hak, registerSSE } from "/.hak/js/sse.js";

registerSSE().then(sse => {
	sse.addEventListener('fs', ev => {

			//	decode event data (base64)
			const [fsEventName, filePath] = atob(ev.data).split("\n")
		
			//	reload browser unless "log.txt" was the changed file
			if (filePath !== 'log.txt') {
				window.location.reload();
			}

		});
	});
});

You can see how easy it is to get the default “LiveReload” functionality, and how easy it would be to modify the behaviour according to your needs.

Why Server Sent Events?

SSE makes more sense than websockets, which is what traditional live-reloaders use. First off, the information does not need to be two-way. Your app has nothing to say to the server. Your server has much to say to your app. The duplex connection that websockets provide are overkill and wasted resources. Fasthak is therefore more correct and more efficient than LiveReload.

Secondly, fasthak provides filesystem events as DOM Events. You choose what you want to do with those events, which in the simplest case is to reload your browser, but could just as easily leverage Hot Module Replacement. LiveReload has some degree of this (stylesheets and images are reloaded via javascript), but it’s brittle on non-configurable. FastHak gives you total control.

Should I switch from LiveReload?

Meh, probably not, if you can’t easily see how it would improve your workflow. This is a very niche improvement, since performance optimisation rarely matters in development mode. For me, FastHak was mainly an excuse to write a server in Go. That said, I use it all the time for net new web projects, like this here blog.

What’s Next?

Due to it’s design, FastHak could easily be extended to respond to events other than fileSystem events. For example, it could provide introspection capabilities to your otherwise static HTML site, or information about the server such as load and resource usage. There is no reason FastHak could not be used in production.

Additionally, it would be useful to provide hooks for common frameworks, such as React and Vue.

Furthermore, I would like to have a fasthak init command that automatically generates the minimal scaffolding needed of your static sites, and possibly generates certs too, freeing you from having to wrestle with mkcert.

Finally, the client code should be available as a browser extension, so that it’s injected into the page but not a part of your codebase.

These are all things I’ll get to, given time.