Skip to content

Commit d78510b

Browse files
committed
feature #20446 [AssetMapper] Document the feature that precompresses assets (javiereguiluz)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [AssetMapper] Document the feature that precompresses assets Fixes #20442. #SymfonyHackday Commits ------- c14a4e7 [AssetMapper] Document the feature that precompresses assets
2 parents d1108d7 + c14a4e7 commit d78510b

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

frontend/asset_mapper.rst

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,9 @@ which will automatically do most of these things for you:
656656
- **Compress your assets**: Your web server should compress (e.g. using gzip)
657657
your assets (JavaScript, CSS, images) before sending them to the browser. This
658658
is automatically enabled in Caddy and can be activated in Nginx and Apache.
659-
In Cloudflare, assets are compressed by default.
659+
In Cloudflare, assets are compressed by default. AssetMapper also supports
660+
:ref:`precompressing your web assets <performance-precompressing>` to further
661+
improve performance.
660662

661663
- **Set long-lived cache expiry**: Your web server should set a long-lived
662664
``Cache-Control`` HTTP header on your assets. Because the AssetMapper component includes a version
@@ -704,6 +706,57 @@ even though it hasn't yet seen the ``import`` statement for them.
704706
Additionally, if the :doc:`WebLink Component </web_link>` is available in your application,
705707
Symfony will add a ``Link`` header in the response to preload the CSS files.
706708

709+
.. _performance-precompressing:
710+
711+
Pre-Compressing Assets
712+
----------------------
713+
714+
Although most servers (Caddy, Nginx, Apache, FrankenPHP) and services like Cloudflare
715+
provide asset compression features, AssetMapper also allows you to compress all
716+
your assets before serving them.
717+
718+
This improves performance because you can compress assets using the highest (and
719+
slowest) compression ratios beforehand and provide those compressed assets to the
720+
server, which then returns them to the client without wasting CPU resources on
721+
compression.
722+
723+
AssetMapper supports `Brotli`_, `Zstandard`_ and `gzip`_ compression formats.
724+
Before using any of them, the server that pre-compresses assets must have
725+
installed the following PHP extensions or CLI commands:
726+
727+
* Brotli: ``brotli`` CLI command; `brotli PHP extension`_;
728+
* Zstandard: ``zstd`` CLI command; `zstd PHP extension`_;
729+
* gzip: ``gzip`` CLI command; `zlib PHP extension`_.
730+
731+
Then, update your AssetMapper configuration to define which compression to use
732+
and which file extensions should be compressed:
733+
734+
.. code-block:: yaml
735+
736+
# config/packages/asset_mapper.yaml
737+
framework:
738+
asset_mapper:
739+
# ...
740+
741+
precompress:
742+
format: 'zstandard'
743+
# if you don't define the following option, AssetMapper will compress all
744+
# the extensions considered safe (css, js, json, svg, xml, ttf, otf, wasm, etc.)
745+
extensions: ['css', 'js', 'json', 'svg', 'xml']
746+
747+
Now, when running the ``asset-map:compile`` command, all matching files will be
748+
compressed in the configured format and at the highest compression level. The
749+
compressed files are created with the same name as the original but with the
750+
``.br``, ``.zst``, or ``.gz`` extension appended. Web servers that support asset
751+
precompression will use the compressed assets automatically, so there's nothing
752+
else to configure in your server.
753+
754+
.. tip::
755+
756+
AssetMapper provides an ``assets:compress`` CLI command and a service called
757+
``asset_mapper.compressor`` that you can use anywhere in your application to
758+
compress any kind of files (e.g. files uploaded by users to your application).
759+
707760
Frequently Asked Questions
708761
--------------------------
709762

@@ -1195,3 +1248,9 @@ command as part of your CI to be warned anytime a new vulnerability is found.
11951248
.. _strict-dynamic: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#strict-dynamic
11961249
.. _kocal/biome-js-bundle: https://github.com/Kocal/BiomeJsBundle
11971250
.. _`SensioLabs Minify Bundle`: https://github.com/sensiolabs/minify-bundle
1251+
.. _`Brotli`: https://en.wikipedia.org/wiki/Brotli
1252+
.. _`Zstandard`: https://en.wikipedia.org/wiki/Zstd
1253+
.. _`gzip`: https://en.wikipedia.org/wiki/Gzip
1254+
.. _`brotli PHP extension`: https://pecl.php.net/package/brotli
1255+
.. _`zstd PHP extension`: https://pecl.php.net/package/zstd
1256+
.. _`zlib PHP extension`: https://www.php.net/manual/en/book.zlib.php

0 commit comments

Comments
 (0)