Skip to content

Instantly share code, notes, and snippets.

@frozzare
Last active April 12, 2018 11:49
Show Gist options
  • Save frozzare/8918dc0f60acaed688c69d23b1741328 to your computer and use it in GitHub Desktop.
Save frozzare/8918dc0f60acaed688c69d23b1741328 to your computer and use it in GitHub Desktop.
package main
// reactWrapper fix issues with dynamic routes created
// in `react-router` so we don't get a 404 page.
//
// usage: http.Handle("/", reactWrapper(http.FileServer("path/to/public")))
func reactWrapper(fs http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
staticIndex := strings.Index(req.URL.Path, "/static/")
if staticIndex == -1 {
fsHandler := http.StripPrefix(req.URL.Path, fs)
fsHandler.ServeHTTP(w, req)
} else {
fs.ServeHTTP(w, req)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment