Hugo: Hugo server should try to show 404 page on HTTP 404

Created on 2 Feb 2015  路  13Comments  路  Source: gohugoio/hugo

Most helpful comment

Anyone resolved this issue?

All 13 comments

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

  • Implement our own version of http.FileServer which can allow for 404 handling. This is what Martini does.
  • Speaking of which, we could use Martini for mux and it's static file handler in server.go. How do hugo developers feel about including third party libraries like this?
  • Maybe there's some clever way to get our own version of FileSystem to return a 404.html when it can't find the file? Sounds kind of like overkill to me to address this issue, though.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digitalcraftsman picture digitalcraftsman  路  3Comments

carandraug picture carandraug  路  3Comments

crash-dive picture crash-dive  路  3Comments

nikolas picture nikolas  路  3Comments

artelse picture artelse  路  3Comments