StaticFileMiddleware serve pre-compressed file when found by file provider #38295
Labels
area-middleware
Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares
feature-static-files
Is your feature request related to a problem? Please describe.
Today, most sites have a build system in place to build, uglify, compress, remove unused code from static web assets such as css, js, or other assets. One such a build system could be webpack as an example. Furthermore, some sites might use such build systems to pre-compress some static assets to reduce the runtime load. The
StaticFileMiddleware
added byUseStaticFiles
has currently no ability to serve a pre-compressed file if present or fallback to the uncompressed file.Describe the solution you'd like
It would be nice to add an opt-in option to the
StaticFileOptions
to enable serving pre-compressed files if present or otherwise falling back to the original uncompressed file.The implementation would be straight forward I think. In the StaticFileContext.cs#L115 the file provider could be additionally queried for a compressed version of the requested file according to the
accept-encoding
request header. If a version of a pre-compressed file exists theStaticFileContext
would serve the pre-compressed file and thecontent-encoding
would be set accordingly. Thecontent-type
would be unaffected as it is retrieved byIContentTypeProvider
from the originally requested path.If there is no pre-compressed file available (i.e. the
IFileInfo.Exists
property returned by the file provider isfalse
), theStaticFileContext
would fallback and serve the normal uncompressed file.The text was updated successfully, but these errors were encountered: