-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added "How to Use a Custom Version Strategy for Assets" #7141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4f530d8
Cookbook entry: Asset - Custom Version Strategy
teohhanhui 80d0ca2
Simplified the intro
javiereguiluz 40dff64
Minor updates to the doc and service config
javiereguiluz 5971ce0
Removed an unneeded file
javiereguiluz c59cff6
Moved the article to its new location
javiereguiluz ab8fa54
Fixed a rebase error
javiereguiluz f038483
Removed an unneeded change
javiereguiluz 482fda5
Minor rewords and fixes
javiereguiluz 2830ba0
Added the missing doc label
javiereguiluz a02f3b3
Added the version_strategy option to the config reference
javiereguiluz 981e82d
Misc fixes
javiereguiluz 1399967
Fixed a label name
javiereguiluz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
.. index:: | ||
single: Asset; Custom Version Strategy | ||
|
||
How to Use a Custom Version Strategy for Assets | ||
=============================================== | ||
|
||
.. versionadded:: 2.7 | ||
The Asset component was introduced in Symfony 2.7. | ||
|
||
Asset versioning is a technique that improves the performance of web | ||
applications by adding a version identifier to the URL of the static assets | ||
(CSS, JavaScript, images, etc.) When the content of the asset changes, its | ||
identifier is also modified to force the browser to download it again instead of | ||
reusing the cached asset. | ||
|
||
Symfony supports asset versioning thanks to the | ||
:ref:`version <reference-framework-assets-version>` and | ||
:ref:`version_format <reference-framework-assets-version-format>` configuration | ||
options. If your application requires a more advanced versioning, such as | ||
generating the version dynamically based on some external information, you can | ||
create your own version strategy. | ||
|
||
Creating your Own Asset Version Strategy | ||
---------------------------------------- | ||
|
||
The following example shows how to create a version strategy compatible with | ||
`gulp-buster`_. This tool defines a configuration file called ``busters.json`` | ||
which maps each asset file to its content hash: | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"js/script.js": "f9c7afd05729f10f55b689f36bb20172", | ||
"css/style.css": "91cd067f79a5839536b46c494c4272d8" | ||
} | ||
|
||
Implement VersionStrategyInterface | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Asset version strategies are PHP classes that implement the | ||
:class:`Symfony\\Component\\Asset\\VersionStrategy\\VersionStrategyInterface`. | ||
In this example, the constructor of the class takes as arguments the path to | ||
the manifest file generated by `gulp-buster`_ and the format of the generated | ||
version string:: | ||
|
||
// src/AppBundle/Asset/VersionStrategy/GulpBusterVersionStrategy.php | ||
namespace AppBundle\Asset\VersionStrategy; | ||
|
||
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; | ||
|
||
class GulpBusterVersionStrategy implements VersionStrategyInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $manifestPath; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $format; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $hashes; | ||
|
||
/** | ||
* @param string $manifestPath | ||
* @param string|null $format | ||
*/ | ||
public function __construct($manifestPath, $format = null) | ||
{ | ||
$this->manifestPath = $manifestPath; | ||
$this->format = $format ?: '%s?%s'; | ||
} | ||
|
||
public function getVersion($path) | ||
{ | ||
if (!is_array($this->hashes)) { | ||
$this->hashes = $this->loadManifest(); | ||
} | ||
|
||
return isset($this->hashes[$path]) ? $this->hashes[$path] : ''; | ||
} | ||
|
||
public function applyVersion($path) | ||
{ | ||
$version = $this->getVersion($path); | ||
|
||
if ('' === $version) { | ||
return $path; | ||
} | ||
|
||
$versionized = sprintf($this->format, ltrim($path, '/'), $version); | ||
|
||
if ($path && '/' === $path[0]) { | ||
return '/'.$versionized; | ||
} | ||
|
||
return $versionized; | ||
} | ||
|
||
private function loadManifest(array $options) | ||
{ | ||
return json_decode(file_get_contents($this->manifestPath), true); | ||
} | ||
} | ||
|
||
Register the Strategy Service | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
After creating the strategy PHP class, register it as a Symfony service. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
services: | ||
app.assets.versioning.gulp_buster: | ||
class: AppBundle\Asset\VersionStrategy\GulpBusterVersionStrategy | ||
arguments: | ||
- "%kernel.root_dir%/../busters.json" | ||
- "%%s?version=%%s" | ||
public: false | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/services.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
http://symfony.com/schema/dic/services/services-1.0.xsd" | ||
> | ||
<services> | ||
<service id="app.assets.versioning.gulp_buster" | ||
class="AppBundle\Asset\VersionStrategy\GulpBusterVersionStrategy" public="false"> | ||
<argument>%kernel.root_dir%/../busters.json</argument> | ||
<argument>%%s?version=%%s</argument> | ||
</service> | ||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/services.php | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
$definition = new Definition( | ||
'AppBundle\Asset\VersionStrategy\GulpBusterVersionStrategy', | ||
array( | ||
'%kernel.root_dir%/../busters.json', | ||
'%%s?version=%%s', | ||
) | ||
); | ||
$definition->setPublic(false); | ||
|
||
$container->setDefinition('app.assets.versioning.gulp_buster', $definition); | ||
|
||
Finally, enable the new asset versioning for all the application assets or just | ||
for some :ref:`asset package <reference-framework-assets-packages>` thanks to | ||
the :ref:`version_strategy <reference-framework-assets-version_strategy>` option: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
framework: | ||
# ... | ||
assets: | ||
version_strategy: 'app.assets.versioning.gulp_buster' | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config> | ||
<framework:assets version-strategy="app.assets.versioning.gulp_buster" /> | ||
</framework:config> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('framework', array( | ||
// ... | ||
'assets' => array( | ||
'version_strategy' => 'app.assets.versioning.gulp_buster', | ||
), | ||
)); | ||
|
||
.. _`gulp-buster`: https://www.npmjs.com/package/gulp-buster |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you have to add a label for this.