Skip to content

Commit 973798b

Browse files
committed
Add doc for strict JsonManifest strategy
1 parent 14ff17d commit 973798b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

components/asset.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,31 @@ In those cases, use the
160160
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
161161

162162
// assumes the JSON file above is called "rev-manifest.json"
163-
$package = new Package(new JsonManifestVersionStrategy(__DIR__.'/rev-manifest.json'));
163+
$package = new Package(new JsonManifestVersionStrategy(__DIR__.'/rev-manifest.json'), $strictMode);
164164

165165
echo $package->getUrl('css/app.css');
166166
// result: build/css/app.b916426ea1d10021f3f17ce8031f93c2.css
167167

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

reference/configuration/framework.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Configuration
4141
* `base_urls`_
4242
* `json_manifest_path`_
4343
* `packages`_
44+
* `strict_mode`_
4445
* `version_format`_
4546
* `version_strategy`_
4647
* `version`_
@@ -1933,6 +1934,7 @@ Each package can configure the following options:
19331934
* :ref:`version <reference-framework-assets-version>`
19341935
* :ref:`version_format <reference-assets-version-format>`
19351936
* :ref:`json_manifest_path <reference-assets-json-manifest-path>`
1937+
* :ref:`strict_mode <reference-assets-strict-mode>`
19361938

19371939
.. _reference-framework-assets-version:
19381940
.. _ref-framework-assets-version:
@@ -2175,6 +2177,8 @@ package:
21752177
foo_package:
21762178
# this package uses its own manifest (the default file is ignored)
21772179
json_manifest_path: "%kernel.project_dir%/public/build/a_different_manifest.json"
2180+
# for debug, an exception is thrown when an asset is not found in the manifest
2181+
strict_mode: "%kernel.debug%"
21782182
bar_package:
21792183
# this package uses the global manifest (the default file is used)
21802184
base_path: '/images'
@@ -2195,9 +2199,11 @@ package:
21952199
<!-- you can use absolute URLs too and Symfony will download them automatically -->
21962200
<!-- <framework:assets json-manifest-path="https://cdn.example.com/manifest.json"> -->
21972201
<!-- this package uses its own manifest (the default file is ignored) -->
2202+
<!-- for debug, an exception is thrown when an asset is not found in the manifest -->
21982203
<framework:package
21992204
name="foo_package"
2200-
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json"/>
2205+
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json"
2206+
strict-mode="true"/>
22012207
<!-- this package uses the global manifest (the default file is used) -->
22022208
<framework:package
22032209
name="bar_package"
@@ -2219,6 +2225,8 @@ package:
22192225
'foo_package' => [
22202226
// this package uses its own manifest (the default file is ignored)
22212227
'json_manifest_path' => '%kernel.project_dir%/public/build/a_different_manifest.json',
2228+
// for debug, an exception is thrown when an asset is not found in the manifest
2229+
'strict_mode' => '%kernel.debug%',
22222230
],
22232231
'bar_package' => [
22242232
// this package uses the global manifest (the default file is used)
@@ -2243,11 +2251,21 @@ package:
22432251

22442252
If you request an asset that is *not found* in the ``manifest.json`` file, the original -
22452253
*unmodified* - asset path will be returned.
2254+
Set `strict_mode` to `true` to get an exception when an asset is *not found*.
22462255

22472256
.. note::
22482257

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

2260+
strict_mode
2261+
...........
2262+
2263+
**type**: ``boolean`` **default**: ``false``
2264+
2265+
.. versionadded:: 5.2
2266+
2267+
This option was introduced in Symfony 5.2.
2268+
22512269
translator
22522270
~~~~~~~~~~
22532271

0 commit comments

Comments
 (0)