Cutelyst
2.13.0
|
Deliver static files compressed on the fly or precompressed. More...
#include <Cutelyst/Plugins/StaticCompressed/StaticCompressed>
Public Member Functions | |
StaticCompressed (Application *parent) | |
virtual | ~StaticCompressed () override |
void | setDirs (const QStringList &dirs) |
void | setIncludePaths (const QStringList &paths) |
virtual bool | setup (Application *app) override |
![]() | |
Plugin (Application *parent) | |
The StaticCompressed plugin for Cutelyst can be used to deliver specific static files like CSS and JavaScript files compressed. It has built in support for gzip and DEFLATE compression format and can be extended by external libraries to support the Brotli compression algorithm and to use Zopfli for gzip compression. Beside compressing the raw data on the fly and store the result in a cache directory, it supports pre-compressed files distinguished by file extension in the static source directories. The plugin uses the Accept-Encoding HTTP request header to determine the compression methods supported by the user agent. If you do not need this, use the StaticSimple plugin to deliver your static files.
Support for gzip and DEFLATE compression format is built in by using the qCompress() function. To enable suport for Brotli, build with -DPLUGIN_STATICCOMPRESSED_BROTLI
:BOOL=ON and have the libbrotlienc development and header files available. To use Zopfli for the gzip compression, build with
-DPLUGIN_STATICCOMPRESSED_ZOPFLI
:BOOL=ON and have the libzopfli development and header files available. Also set the configuration key
use_zopfli
to true
. Be aware that Zopfli gives better compression rate than default gzip but is also much slower. So Zopfli is disabled by default even if it is enabled at compilation time.
Static files of the configured mime_types
or with the configured suffixes
can be compressed on the fly into a format that is accepted by the requesting user agent. The compressed data is saved into files in the cache_diretory
specified in the configuration. The cache file name will be the MD5 hash sum of the original local file path together with the file extension indicating the compression format (.br for Brotli, .gz for gzip/Zopfli and .deflate for DEFLATE). If the modification time of the original file is newer than the modification time of the cached compressed file, the file will be compressed again. It is safe to clean the content of the cache directory - the files will than be recompressed on the next request. On the fly compression can be disabled by setting on_the_fly_compression
to false
in the configuration file.
Beside the cached on the fly compression it is also possible to deliver pre-comrpessed static files that are saved in the same place as the original files are. The StaticCompressed plugin will try to find a compressed file at the same path as the original file appended by an extension indicating the compression method. So if you have for example boostrap.min.css and bootstrap.min.css.gz in your static files directory, the plugin will deliver the compressed variant if the requesting user agent supports the gzip encoding. The delivery of pre-compressed files can be disabled by setting check_pre_compressed
to false
in the configuration file.
The plugin offers some configuration options that can be set in the Cutelyst application configuration file in the Cutelyst_StaticCompressed_Plugin
section.
cache_directory
- string value, sets the directory where on the fly compressed data is saved (default: QStandardPaths::CacheLocation + /compressed-static) mime_types
- string value, comma separated list of MIME types that should be compressed (default: text/css,application/javascript) suffixes
- string value, comma separted list of file suffixes/extensions that should be compressed (default: js.map,css.map,min.js.map,min.css.map) check_pre_compressed
- boolean value, enables or disables the check for pre compressed files (default: true) on_the_fly_compression
- boolean value, enables or disables the compression on the fly (default: true) zlib_compression_level
- integer value, compression level for built in zlib based compression between 0 and 9, with 9 corresponding to the greatest compression (default: 9) brotli_quality_level
- integer value, quality level for optional Brotli compression between 0 and 11, with 11 corresponding to the greates compression (default: 11) use_zopfli
- boolean value, enables the optional use of Zopfli for the gzip compression if available zopfli_iterations
- integer value, number of iterations used for Zopfli compression, more gives more compression but is slower (default: 15)-DPLUGIN_STATICCOMPRESSED
:BOOL=ON
- enables the build of the plugin (default: off
) -DPLUGIN_STATICCOMPRESSED_ZOPFLI
:BOOL=ON
- enables the Zopfli support for gzip, libzopfli development and header files have to be present (default: off
) -DPLUGIN_STATICCOMPRESSED_BROTLI
:BOOL=ON
- enables the Brotli support, libbrotlienc development and header files have to be present (default: off
)Since Cutelyst 2.0.0 you can check if CUTELYST_STATICCOMPRESSED_WITH_ZOPFLI
and/or CUTELYST_STATICCOMPRESSED_WITH_BROTLI
are defined if you need to know that the plugin supports that compressions.
Definition at line 138 of file staticcompressed.h.
|
explicit |
Constructs a new StaticCompressed object with the given parent.
Definition at line 49 of file staticcompressed.cpp.
|
overridevirtual |
Deconstructs the StaticCompressed object.
Definition at line 56 of file staticcompressed.cpp.
void StaticCompressed::setDirs | ( | const QStringList & | dirs | ) |
Sets a list of top-level directories beneath your 'root' directory that should always be served in static mode.
Definition at line 70 of file staticcompressed.cpp.
void StaticCompressed::setIncludePaths | ( | const QStringList & | paths | ) |
Sets a list of directories in which to search for your static files. The directories will be searched in order and will return the first file found. Note that your root directory is not automatically added to the search path when you specify an include_path.
Definition at line 61 of file staticcompressed.cpp.
|
overridevirtual |
Configures the plugin by reading the Cutelyst_StaticCompressed_Plugin
section from the Cutelyst application configuration file and connects to the Application::beforePrepareAction() signal. Returns false
if the cache directory can not be created if it not exists.
Reimplemented from Cutelyst::Plugin.
Definition at line 76 of file staticcompressed.cpp.
References Cutelyst::Application::beforePrepareAction(), Cutelyst::Engine::config(), and Cutelyst::Application::engine().