Skip to content

Commit 6617dc0

Browse files
committed
feature #21478 [Asset] Add support for preloading with links and HTTP/2 push (dunglas)
This PR was squashed before being merged into the 3.3-dev branch (closes #21478). Discussion ---------- [Asset] Add support for preloading with links and HTTP/2 push | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | todo Allows compatible clients to preload mandatory assets like scripts, stylesheets or images according to [the "preload" working draft of the W3C](https://www.w3.org/TR/preload/). Thanks to this PR, Symfony will automatically adds `Link` HTTP headers with a `preload` relation for mandatory assets. If an intermediate proxy supports HTTP/2 push, it will convert preload headers. For instance [Cloudflare supports this feature](https://blog.cloudflare.com/using-http-2-server-push-with-php/). It dramatically increases pages speed and make the web greener because only one TCP connection is used to fetch all mandatory assets (decrease servers and devices loads, improve battery lives). Usage: Updated version: ```html <html> <body> Hello <script src="{{ preload(asset('/scripts/foo.js'), 'script') }}"></script> </body> </html> ``` ~~First proposal:~~ ```html <html> <body> Hello <script src="{{ preloaded_asset('/scripts/foo.js', 'script') }}"></script> </body> </html> ``` - [x] Add tests Commits ------- 7bab21700d [Asset] Add support for preloading with links and HTTP/2 push
2 parents 09a3ce6 + 7e753cb commit 6617dc0

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Resources/config/assets.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,13 @@
3838

3939
<service id="assets.empty_version_strategy" class="Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy" public="false" />
4040

41+
<service id="assets.preload_manager" class="Symfony\Component\Asset\Preload\PreloadManager" public="false" />
42+
43+
<service id="asset.preload_listener" class="Symfony\Component\Asset\EventListener\PreloadListener">
44+
<argument type="service" id="assets.preload_manager" />
45+
46+
<tag name="kernel.event_subscriber" />
47+
</service>
48+
4149
</services>
4250
</container>

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ public function testAssetsDefaultVersionStrategyAsService()
376376
$this->assertEquals('assets.custom_version_strategy', (string) $defaultPackage->getArgument(1));
377377
}
378378

379+
public function testAssetHasPreloadListener()
380+
{
381+
$container = $this->createContainerFromFile('assets');
382+
$this->assertTrue($container->hasDefinition('asset.preload_listener'));
383+
}
384+
379385
public function testTranslator()
380386
{
381387
$container = $this->createContainerFromFile('full');

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"conflict": {
6161
"phpdocumentor/reflection-docblock": "<3.0",
6262
"phpdocumentor/type-resolver": "<0.2.0",
63+
"symfony/asset": "<3.3",
6364
"symfony/console": "<3.3",
6465
"symfony/serializer": "<3.3",
6566
"symfony/form": "<3.3",

0 commit comments

Comments
 (0)