Go Tip Of The Week: Removal Of Response Headers For Errors Can Break On-The-Fly Encoding In Go 1.23

The Go 1.23 pre-relase notes describe a change that might affect Web server code. Among the changes to net/http, a particular change can cause code to not function anymore. Here is the excerpt:

ServeContent, ServeFile, and ServeFileFS now remove the Cache-Control, Content-Encoding, Etag, and Last-Modified headers when serving an error. These headers usually apply to the non-error content, but not to the text of errors.

Middleware which wraps a ResponseWriter and applies on-the-fly encoding, such as Content-Encoding: gzip, will not function after this change. The previous behavior of ServeContent, ServeFile, and ServeFileFS may be restored by setting GODEBUG=httpservecontentkeepheaders=1.

Note that middleware which changes the size of the served content (such as by compressing it) already does not function properly when ServeContent handles a Range request. On-the-fly compression should use the Transfer-Encoding header instead of Content-Encoding.

TL;DR: Several HTTP headers will not be included in an error response anymore. Middleware that does on-the-fly encoding and thus would need to set the Content-Encoding header, will break due to this change.