@@ -59,13 +59,23 @@ correct prioritization and the content security policy:
59
59
60
60
<head>
61
61
<!-- ... -->
62
- <link rel="preload" href="{{ preload('/app.css', { as: 'style' }) }}">
62
+ {# note that you must add two <link> tags per asset:
63
+ one to link to it and the other one to tell the browser to preload it #}
64
+ <link rel="preload" href="{{ preload('/app.css', {as: 'style'}) }}" as="style">
65
+ <link rel="stylesheet" href="/app.css">
63
66
</head>
64
67
65
68
If you reload the page, the perceived performance will improve because the
66
69
server responded with both the HTML page and the CSS file when the browser only
67
70
requested the HTML page.
68
71
72
+ .. tip ::
73
+
74
+ When using the :doc: `AssetMapper component </frontend/asset_mapper >` to link
75
+ to assets (e.g. ``importmap('app') ``), there's no need to add the ``<link rel="preload"> ``
76
+ tag. The ``importmap() `` Twig function automatically adds the ``Link `` HTTP
77
+ header for you when the WebLink component is available.
78
+
69
79
.. note ::
70
80
71
81
You can preload an asset by wrapping it with the ``preload() `` function:
@@ -74,7 +84,8 @@ requested the HTML page.
74
84
75
85
<head>
76
86
<!-- ... -->
77
- <link rel="preload" href="{{ preload(asset('build/app.css')) }}">
87
+ <link rel="preload" href="{{ preload(asset('build/app.css')) }}" as="style">
88
+ <!-- ... -->
78
89
</head>
79
90
80
91
Additionally, according to `the Priority Hints specification `_, you can signal
@@ -84,7 +95,8 @@ the priority of the resource to download using the ``importance`` attribute:
84
95
85
96
<head>
86
97
<!-- ... -->
87
- <link rel="preload" href="{{ preload('/app.css', { as: 'style', importance: 'low' }) }}">
98
+ <link rel="preload" href="{{ preload('/app.css', {as: 'style', importance: 'low'}) }}" as="style">
99
+ <!-- ... -->
88
100
</head>
89
101
90
102
How does it work?
@@ -108,7 +120,8 @@ issuing an early separate HTTP request, use the ``nopush`` option:
108
120
109
121
<head>
110
122
<!-- ... -->
111
- <link rel="preload" href="{{ preload('/app.css', { as: 'style', nopush: true }) }}">
123
+ <link rel="preload" href="{{ preload('/app.css', {as: 'style', nopush: true}) }}" as="style">
124
+ <!-- ... -->
112
125
</head>
113
126
114
127
Resource Hints
@@ -142,7 +155,8 @@ any link implementing the `PSR-13`_ standard. For instance, any
142
155
<head>
143
156
<!-- ... -->
144
157
<link rel="alternate" href="{{ link('/index.jsonld', 'alternate') }}">
145
- <link rel="preload" href="{{ preload('/app.css', { as: 'style', nopush: true }) }}">
158
+ <link rel="preload" href="{{ preload('/app.css', {as: 'style', nopush: true}) }}" as="style">
159
+ <!-- ... -->
146
160
</head>
147
161
148
162
The previous snippet will result in this HTTP header being sent to the client:
0 commit comments