Skip to content

Commit 9d44eb9

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: Tweaks [Assets] Add doc for strict mode strategy
2 parents 0b3e62c + 3e4cd5e commit 9d44eb9

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

components/asset.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ In those cases, use the
167167
echo $package->getUrl('css/app.css');
168168
// result: build/css/app.b916426ea1d10021f3f17ce8031f93c2.css
169169

170+
If you request an asset that is *not found* in the ``rev-manifest.json`` file,
171+
the original - *unmodified* - asset path will be returned. The ``$strictMode``
172+
argument helps debug issues because it throws an exception when the asset is not
173+
listed in the manifest::
174+
175+
use Symfony\Component\Asset\Package;
176+
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
177+
178+
// The value of $strictMode can be specific per environment "true" for debugging and "false" for stability.
179+
$strictMode = true;
180+
// assumes the JSON file above is called "rev-manifest.json"
181+
$package = new Package(new JsonManifestVersionStrategy(__DIR__.'/rev-manifest.json', null, $strictMode));
182+
183+
echo $package->getUrl('not-found.css');
184+
// error:
185+
186+
.. versionadded:: 5.4
187+
188+
The ``$strictMode`` option was introduced in Symfony 5.4.
189+
170190
If your JSON file is not on your local filesystem but is accessible over HTTP,
171191
use the :class:`Symfony\\Component\\Asset\\VersionStrategy\\RemoteJsonManifestVersionStrategy`
172192
with the :doc:`HttpClient component </http_client>`::

reference/configuration/framework.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,7 @@ Each package can configure the following options:
17981798
* :ref:`version <reference-framework-assets-version>`
17991799
* :ref:`version_format <reference-assets-version-format>`
18001800
* :ref:`json_manifest_path <reference-assets-json-manifest-path>`
1801+
* :ref:`strict_mode <reference-assets-strict-mode>`
18011802

18021803
.. _reference-framework-assets-version:
18031804
.. _ref-framework-assets-version:
@@ -2041,6 +2042,8 @@ package:
20412042
foo_package:
20422043
# this package uses its own manifest (the default file is ignored)
20432044
json_manifest_path: "%kernel.project_dir%/public/build/a_different_manifest.json"
2045+
# Throws an exception when an asset is not found in the manifest
2046+
strict_mode: %kernel.debug%
20442047
bar_package:
20452048
# this package uses the global manifest (the default file is used)
20462049
base_path: '/images'
@@ -2061,9 +2064,10 @@ package:
20612064
<!-- you can use absolute URLs too and Symfony will download them automatically -->
20622065
<!-- <framework:assets json-manifest-path="https://cdn.example.com/manifest.json"> -->
20632066
<!-- this package uses its own manifest (the default file is ignored) -->
2067+
<!-- Throws an exception when an asset is not found in the manifest -->
20642068
<framework:package
20652069
name="foo_package"
2066-
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json"/>
2070+
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json" strict-mode="%kernel.debug%"/>
20672071
<!-- this package uses the global manifest (the default file is used) -->
20682072
<framework:package
20692073
name="bar_package"
@@ -2087,7 +2091,9 @@ package:
20872091
// 'json_manifest_path' => 'https://cdn.example.com/manifest.json',
20882092
$framework->assets()->package('foo_package')
20892093
// this package uses its own manifest (the default file is ignored)
2090-
->jsonManifestPath('%kernel.project_dir%/public/build/a_different_manifest.json');
2094+
->jsonManifestPath('%kernel.project_dir%/public/build/a_different_manifest.json')
2095+
// Throws an exception when an asset is not found in the manifest
2096+
->setStrictMode('%kernel.debug%');
20912097
20922098
$framework->assets()->package('bar_package')
20932099
// this package uses the global manifest (the default file is used)
@@ -2104,11 +2110,27 @@ package:
21042110

21052111
If you request an asset that is *not found* in the ``manifest.json`` file, the original -
21062112
*unmodified* - asset path will be returned.
2113+
Since Symfony 5.4, you can set ``strict_mode`` to ``true`` to get an exception when an asset is *not found*.
21072114

21082115
.. note::
21092116

21102117
If an URL is set, the JSON manifest is downloaded on each request using the `http_client`_.
21112118

2119+
.. _reference-assets-strict-mode:
2120+
2121+
strict_mode
2122+
...........
2123+
2124+
**type**: ``boolean`` **default**: ``false``
2125+
2126+
.. versionadded:: 5.4
2127+
2128+
The ``strict_mode`` option was introduced in Symfony 5.4.
2129+
2130+
When enabled, the strict mode asserts that all requested assets are in the
2131+
manifest file. This option is useful to detect typos or missing assets, the
2132+
recommended value is ``%kernel.debug%``.
2133+
21122134
translator
21132135
~~~~~~~~~~
21142136

0 commit comments

Comments
 (0)