Serving Single-Page Applications in Go
Someone may ask what is so special about serving single-page applications? Well, if you use hash (#) based URL’s like https://yourdomain.com/#/yourroute , there is no problem because browser recognize that this is a default page with some parameters and doesn’t make another request to the server. However, if you want to use non-hash based routes, there is a problem if you try to refresh a non-default route. In this case browser’s default action is to load such URL from the server and there is no such document on the server. For example, if you load your SPA at URL https://yourdomain.com and then you click on another route, you will see in your browser’s URL field something like https://yourdomain.com/yourroute . That works fine unless you try to reload the page. In that case browser doesn’t know this is actually already loaded SPA, but it actually tries to fetch /yourroute URI from server. As there is no such URI on the server, you will usually get the famous 404 error and your application breaks. ...