@@ -55,13 +55,14 @@ To do that, first install the WebLink component:
55
55
$ composer require symfony/web-link
56
56
57
57
Now, update the template to use the ``preload() `` Twig function provided by
58
- WebLink:
58
+ WebLink. The `"as" attribute `_ is mandatory because browsers need it to apply
59
+ correct prioritization and the content security policy:
59
60
60
61
.. code-block :: html+twig
61
62
62
63
<head>
63
64
<!-- ... -->
64
- <link rel="stylesheet" href="{{ preload('/app.css') }}">
65
+ <link rel="stylesheet" href="{{ preload('/app.css', { as: 'style' } ) }}">
65
66
</head>
66
67
67
68
If you reload the page, the perceived performance will improve because the
@@ -75,7 +76,7 @@ the priority of the resource to download using the ``importance`` attribute:
75
76
76
77
<head>
77
78
<!-- ... -->
78
- <link rel="stylesheet" href="{{ preload('/app.css', { importance: 'low' }) }}">
79
+ <link rel="stylesheet" href="{{ preload('/app.css', { as: 'style', importance: 'low' }) }}">
79
80
</head>
80
81
81
82
.. tip ::
@@ -88,8 +89,7 @@ How does it work?
88
89
89
90
The WebLink component manages the ``Link `` HTTP headers added to the response.
90
91
When using the ``preload() `` function in the previous example, the following
91
- header was added to the response: ``Link </app.css>; rel="preload" ``
92
-
92
+ header was added to the response: ``Link </app.css>; rel="preload"; as="style" ``
93
93
According to `the Preload specification `_, when an HTTP/2 server detects that
94
94
the original (HTTP 1.x) response contains this HTTP header, it will
95
95
automatically trigger a push for the related file in the same HTTP/2 connection.
@@ -105,7 +105,7 @@ issuing an early separate HTTP request, use the ``nopush`` option:
105
105
106
106
<head>
107
107
<!-- ... -->
108
- <link rel="stylesheet" href="{{ preload('/app.css', { nopush: true }) }}">
108
+ <link rel="stylesheet" href="{{ preload('/app.css', { as: 'style', nopush: true }) }}">
109
109
</head>
110
110
111
111
Resource Hints
@@ -139,7 +139,7 @@ any link implementing the `PSR-13`_ standard. For instance, any
139
139
<head>
140
140
<!-- ... -->
141
141
<link rel="alternate" href="{{ link('/index.jsonld', 'alternate') }}">
142
- <link rel="stylesheet" href="{{ preload('/app.css', { nopush: true }) }}">
142
+ <link rel="stylesheet" href="{{ preload('/app.css', { as: 'style', nopush: true }) }}">
143
143
</head>
144
144
145
145
The previous snippet will result in this HTTP header being sent to the client:
@@ -164,7 +164,7 @@ You can also add links to the HTTP response directly from controllers and servic
164
164
165
165
// alternative if you don't want to use the addLink() shortcut
166
166
$linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
167
- $request->attributes->set('_links', $linkProvider->withLink(new Link('preload', '/app.css')));
167
+ $request->attributes->set('_links', $linkProvider->withLink(new Link('preload', '/app.css', ['as' : 'style'] )));
168
168
169
169
return $this->render('...');
170
170
}
@@ -179,6 +179,7 @@ You can also add links to the HTTP response directly from controllers and servic
179
179
.. _`Resource Hints` : https://www.w3.org/TR/resource-hints/
180
180
.. _`Docker installer and runtime for Symfony` : https://github.com/dunglas/symfony-docker
181
181
.. _`preload` : https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
182
+ .. _`"as" attribute` : https://w3c.github.io/preload/#as-attribute
182
183
.. _`the Priority Hints specification` : https://wicg.github.io/priority-hints/
183
184
.. _`the Preload specification` : https://www.w3.org/TR/preload/#server-push-(http/2)
184
185
.. _`Cloudflare` : https://blog.cloudflare.com/announcing-support-for-http-2-server-push-2/
0 commit comments