-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v2: Content negotiation #2665
Comments
As discussed in https://caddy.community/t/how-to-serve-gzipped-files-automatically-in-caddy-v2/7311?u=matt it would be useful for Caddy to be able to serve pre-compressed files as well. If enabled, this would involve using the Accept-Encoding header to see if there is a matching file on disk, and if so, serve it with the proper headers. Maybe as a guest module to the file_server handler. |
What is the workaround for implementing this in the actual version v2? Suppose the following scenario: The browser requests the resource |
It'd be nice for Caddy to be able to support pre-compressed brotli Unlike gzip, brotli isn't advised for on-demand compression from what I've read, would it also be something Caddy could do if encoding was requested for static assets to compress them, but write that compressed file to disk for serving in future, or is this something that should be handled outside of Caddy? Such as a scheduled compression pass if compressible files are added to the server?(eg via user uploads) |
I think so too (see my reply above)! It just needs to be discussed, designed, and implemented... |
An important point I'm missing here is, that the decision to use compression at all should depend on the type of data to be served. It is wasted CPU cycles to compress images, web fonts (woff2) or any other content that is already compressed. To pre-compression: Next question would be how determine the order of the encodings. In #3692 I suggested a implemention of a prefered order of encodings. But this should only apply to dynamic content. The prefered order of encodings for pre compressed content may/should be different. (Brotli for pre compression because of the better compression ratio and gzip for dynamic content because of the better speed). Maybe an separate setting?
(I think as a first step pre compression should be done outside of caddy. |
I tried myself on an implementation for this. Let me know what you think. Things implemented on this branch:
What I'm unsure about:
Edit: Creating compressed versions of the files should be done outside of caddy. |
I'm not sure if I'm convinced by the I think we should change I'm trying to think whether the |
@ueffel you can open a PR for this, it'll probably be easier to discuss specific aspects of the changes. I like where you're going with this! ❤️ |
@ueffel Yeah that looks great. I might suggest renaming the
It is OK for the staticfiles package to import the encode package, if that's what you mean? Speaking of dependencies though, I do want to see if we can get away without https://github.com/gobwas/glob or any other new external dependencies for this.
Manipulate headers as necessary to serve the content properly. 👍 Create a pull request and let's review! |
Just a quick heads-up that this issue had been linked in several places (examples 1, 2) as the issue that's tracking content negotiation, especially regarding In the end, it has been closed in favor of #4045 which basically only cares about negotiating encoding (compression) for the There's the conneg plugin by @mpilhlt which doesn't look terribly mature (12 commits, last one two years ago) that's supposed to provide flexible content negotiation for Other than that, is there anything else available or planned that I didn't see, that would support this kind of content negotiation, in particular dealing with q-values? |
Except for fine-tuning some related headers recently, I haven't really done much more with content negotiation because it's a vast area and there hasn't been specific requests (in particular from sponsors). To see something more, we'll probably need specific requests and use cases to justify the effort -- since there are many ways to do it. |
Caddy 2 needs a generic, powerful way to do content negotiation.
Currently, the
http.handlers.encode
module negotiates its encoding from the Accept-Encoding header. However, for pre-compressed assets or other kinds of content negotiation, different facilities are required.Content negotiation can be based on MIME type (Accept header), encoding (Accept-Encoding header), or language (Accept-Language) header.
I propose a content negotiation request matcher:
For each negotiated variance, a placeholder would be added indicating what was negotiated, for example:
{http.matchers.content_negotiation.mime}
TODO: We would also need a way to add a Vary header to the response. Maybe a matcher is not the right solution. This might have to be a middleware... hm.
The text was updated successfully, but these errors were encountered: