Skip to content

Commit a46e0a6

Browse files
committed
Add doc for strict JsonManifest strategy
1 parent 9b334b0 commit a46e0a6

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`_
@@ -1926,6 +1927,7 @@ Each package can configure the following options:
19261927
* :ref:`version <reference-framework-assets-version>`
19271928
* :ref:`version_format <reference-assets-version-format>`
19281929
* :ref:`json_manifest_path <reference-assets-json-manifest-path>`
1930+
* :ref:`strict_mode <reference-assets-strict-mode>`
19291931

19301932
.. _reference-framework-assets-version:
19311933
.. _ref-framework-assets-version:
@@ -2168,6 +2170,8 @@ package:
21682170
foo_package:
21692171
# this package uses its own manifest (the default file is ignored)
21702172
json_manifest_path: "%kernel.project_dir%/public/build/a_different_manifest.json"
2173+
# for debug, an exception is thrown when an asset is not found in the manifest
2174+
strict_mode: "%kernel.debug%"
21712175
bar_package:
21722176
# this package uses the global manifest (the default file is used)
21732177
base_path: '/images'
@@ -2188,9 +2192,11 @@ package:
21882192
<!-- you can use absolute URLs too and Symfony will download them automatically -->
21892193
<!-- <framework:assets json-manifest-path="https://cdn.example.com/manifest.json"> -->
21902194
<!-- this package uses its own manifest (the default file is ignored) -->
2195+
<!-- for debug, an exception is thrown when an asset is not found in the manifest -->
21912196
<framework:package
21922197
name="foo_package"
2193-
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json"/>
2198+
json-manifest-path="%kernel.project_dir%/public/build/a_different_manifest.json"
2199+
strict-mode="true"/>
21942200
<!-- this package uses the global manifest (the default file is used) -->
21952201
<framework:package
21962202
name="bar_package"
@@ -2212,6 +2218,8 @@ package:
22122218
'foo_package' => [
22132219
// this package uses its own manifest (the default file is ignored)
22142220
'json_manifest_path' => '%kernel.project_dir%/public/build/a_different_manifest.json',
2221+
// for debug, an exception is thrown when an asset is not found in the manifest
2222+
'strict_mode' => '%kernel.debug%',
22152223
],
22162224
'bar_package' => [
22172225
// this package uses the global manifest (the default file is used)
@@ -2236,11 +2244,21 @@ package:
22362244

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

22402249
.. note::
22412250

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

2253+
strict_mode
2254+
...........
2255+
2256+
**type**: ``boolean`` **default**: ``false``
2257+
2258+
.. versionadded:: 5.2
2259+
2260+
This option was introduced in Symfony 5.2.
2261+
22442262
translator
22452263
~~~~~~~~~~
22462264

0 commit comments

Comments
 (0)