8000 [reference] Rewrite assets_version_format docs and add tip for path p… · web-dev/symfony-docs@46de9ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 46de9ef

Browse files
committed
[reference] Rewrite assets_version_format docs and add tip for path prefixing
Versioned path prefixes are useful for CDN's that do not support cache-busting via query strings. This seemed small enough to add as a tip, although it might be better expanded into a cookbook article (and this tip reduced to a "see cookbook" link). Also added a note about required escaping of percentage signs in the option value.
1 parent 0b21c4d commit 46de9ef

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

reference/configuration/framework.rst

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,37 @@ option.
170170
assets_version_format
171171
.....................
172172

173-
**type**: ``string`` **default**: ``%s?%s``
173+
**type**: ``string`` **default**: ``%%s?%%s``
174174

175-
This option relates to the `assets_version`_ option and controls exactly
176-
how the query string is constructed. For example, if ``assets_version_format``
177-
is set to ``%s?version=%s`` and ``assets_version`` is set to ``5``, rendered
178-
assets would look like ``/images/logo.png?version=5``.
175+
This specifies a `sprintf()`_ pattern that will be used with the `assets_version`_
176+
option to construct an asset's path. By default, the pattern adds the asset's
177+
version as a query string. For example, if ``assets_version_format`` is set to
178+
``%%s?version=%%s`` and ``assets_version`` is set to ``5``, the asset's path
179+
would be ``/images/logo.png?version=5``.
180+
181+
.. note::
182+
183+
All percentage signs (``%``) in the format string must be doubled to escape
184+
the character. Without escaping, values might inadvertently be interpretted
185+
as :ref:`_book-service-container-parameters`.
186+
187+
.. tip::
188+
189+
Some CDN's do not support cache-busting via query strings, so injecting the
190+
version into the actual file path is necessary. Thankfully, ``assets_version_format``
191+
is not limited to producing versioned query strings.
192+
193+
The pattern receives the asset's original path and version as its first and
194+
second parameters, respectively. Since the asset's path is one parameter, we
195+
cannot modify it in-place (e.g. ``/images/logo-v5.png``); however, we can
196+
prefix the asset's path using a pattern of ``version-%%2$s/%%1$s``, which
197+
would result in the path ``version-5/images/logo.png``.
198+
199+
URL rewrite rules could then be used to disregard the version prefix before
200+
serving the asset. Alternatively, you could copy assets to the appropriate
201+
version path as part of your deployment process and forgo any URL rewriting.
202+
The latter option is useful if you would like older asset versions to remain
203+
accessible at their original URL.
179204

180205
Full Default Configuration
181206
--------------------------
@@ -277,5 +302,4 @@ Full Default Configuration
277302
file_cache_dir: %kernel.cache_dir%/annotations
278303
debug: true
279304
280-
281-
305+
.. _sprintf(): http://php.net/manual/en/function.sprintf.php

0 commit comments

Comments
 (0)
0