Skip to content

[WebLink] All mentions to preload must have an "as" attribute #11615

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions web_link.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ WebLink:

<head>
<!-- ... -->
<link rel="stylesheet" href="{{ preload('/app.css') }}">
<link rel="stylesheet" href="{{ preload('/app.css', { as: 'style' }) }}">
</head>

If you reload the page, the perceived performance will improve because the
Expand All @@ -75,7 +75,7 @@ the priority of the resource to download using the ``importance`` attribute:

<head>
<!-- ... -->
<link rel="stylesheet" href="{{ preload('/app.css', { importance: 'low' }) }}">
<link rel="stylesheet" href="{{ preload('/app.css', { as: 'style', importance: 'low' }) }}">
</head>

.. tip::
Expand All @@ -88,8 +88,7 @@ How does it work?

The WebLink component manages the ``Link`` HTTP headers added to the response.
When using the ``preload()`` function in the previous example, the following
header was added to the response: ``Link </app.css>; rel="preload"``

header was added to the response: ``Link </app.css>; rel="preload"; as="style"``
According to `the Preload specification`_, when an HTTP/2 server detects that
the original (HTTP 1.x) response contains this HTTP header, it will
automatically trigger a push for the related file in the same HTTP/2 connection.
Expand All @@ -105,7 +104,7 @@ issuing an early separate HTTP request, use the ``nopush`` option:

<head>
<!-- ... -->
<link rel="stylesheet" href="{{ preload('/app.css', { nopush: true }) }}">
<link rel="stylesheet" href="{{ preload('/app.css', { as: 'style', nopush: true }) }}">
</head>

Resource Hints
Expand Down Expand Up @@ -139,7 +138,7 @@ any link implementing the `PSR-13`_ standard. For instance, any
<head>
<!-- ... -->
<link rel="alternate" href="{{ link('/index.jsonld', 'alternate') }}">
<link rel="stylesheet" href="{{ preload('/app.css', { nopush: true }) }}">
<link rel="stylesheet" href="{{ preload('/app.css', { as: 'style', nopush: true }) }}">
</head>

The previous snippet will result in this HTTP header being sent to the client:
Expand All @@ -164,7 +163,7 @@ You can also add links to the HTTP response directly from controllers and servic

// alternative if you don't want to use the addLink() shortcut
$linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
$request->attributes->set('_links', $linkProvider->withLink(new Link('preload', '/app.css')));
$request->attributes->set('_links', $linkProvider->withLink(new Link('preload', '/app.css', ['as' : 'style'])));

return $this->render('...');
}
Expand Down