I tried tackling this yesterday and found it surprisingly difficult:
serveFile, which is used by FileServer, doesn't expose an actual error. Instead it has a NotFound handler that it calls when it can't find a file on the file system, which you cannot get around using. See http://golang.org/src/net/http/fs.go#L356 . As of right now, changing this in the standard library is currently a TODO.
I tried implementing my own ResponseWriter type that intercepted WriteHeader to call a handler that handles 404s. However, at that point NotFound had already been called and the response had been written to, so this doesn't seem to be a valid option.
Other options I can think of
What do other people think?
I thought this would be simple when I jotted down the issue. Maybe @bradfitz could shed some light on this. I think he's the main person behind the http package in Go.
I know @spf13 would hate to increase the binary size with Martini just to get some proper 404 handling.
I've considered adding in httprouter though. Especially if we add a rest API.聽
聽
https://github.com/julienschmidt/httprouter
Definitely in favor of the simplest, smallest solution for this. httprouter looks very nice :+1:
I just tackled this and the solution I came up with was an enhanced/stipped down http.FileServer handler.
https://github.com/rustyoz/hugo/commit/4a4cebfccbb0663afa6547daf513d1506c8bc97a
which uses this
https://github.com/rustyoz/rustyplate/blob/master/rustyplate.go
A better solution to what I created would be to substitute the standard http package with https://github.com/Masterminds/go-fileserver
@rustyoz I agree, but I still feels that the FileServer devs should have made this extensible in the first place. When they sober up from GopherCon I will post an issue; but that will be Go 1.6 at best.
I guess this discussion can hold until we decide what to do about a REST api.
Anyone resolved this issue?
This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.
It's better to keep this issue open (or fix it before closing) for more convenient localhost development.
Related then, adding something in documentation would be a sufficient measure.
In https://gohugo.io/templates/404/ at the very end (in Automatic Loading), something like:
Hugo's built in server will not automatically load your custom 404.html file, but you can test the appearance of your custom not-found page by navigating your browser to /404.html.
@solutionroute Done! :)
Cool, thanks. I'll try submitting doc PRs myself.
Cheers
Mike
Most helpful comment
Anyone resolved this issue?