-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[AssetMapper] Automatically preload CSS files if WebLink available #51829
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,13 @@ | |
|
||
namespace Symfony\Component\AssetMapper\ImportMap; | ||
|
||
use Psr\Link\EvolvableLinkProviderInterface; | ||
use Symfony\Component\Asset\Packages; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener; | ||
use Symfony\Component\WebLink\GenericLinkProvider; | ||
use Symfony\Component\WebLink\Link; | ||
|
||
/** | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
|
@@ -27,6 +33,7 @@ public function __construct( | |
private readonly string $charset = 'UTF-8', | ||
private readonly string|false $polyfillUrl = ImportMapManager::POLYFILL_URL, | ||
private readonly array $scriptAttributes = [], | ||
private readonly ?RequestStack $requestStack = null, | ||
) { | ||
} | ||
|
||
|
@@ -68,6 +75,10 @@ public function render(string|array $entryPoint, array $attributes = []): string | |
$output .= "\n<link rel=\"stylesheet\" href=\"$url\">"; | ||
} | ||
|
||
if (class_exists(AddLinkHeaderListener::class) && $request = $this->requestStack?->getCurrentRequest()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same logic as in |
||
$this->addWebLinkPreloads($request, $cssLinks); | ||
} | ||
|
||
$scriptAttributes = $this->createAttributesString($attributes); | ||
$importMapJson = json_encode(['imports' => $importMap], \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG); | ||
$output .= <<<HTML | ||
|
@@ -131,4 +142,25 @@ private function createAttributesString(array $attributes): string | |
|
||
return $attributeString; | ||
} | ||
|
||
private function addWebLinkPreloads(Request $request, array $cssLinks): void | ||
{ | ||
$cssPreloadLinks = array_map(fn ($url) => new Link('preload', $url), $cssLinks); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, but shouldn't the nopush attribute be better set here? So that the css files are not sent again and again, even if they are already in client cache. From https://developer.chrome.com/blog/early-hints/:
In current Chrome versions it wouldn't make any difference, because Server Push is already disabled there. But afaik other browsers still support Server Push in current versions. - $cssPreloadLinks = array_map(fn ($url) => new Link('preload', $url), $cssLinks);
+ $cssPreloadLinks = array_map(fn ($url) => (new Link('preload', $url))->withAttribute('nopush', true), $cssLinks); |
||
|
||
if (null === $linkProvider = $request->attributes->get('_links')) { | ||
$request->attributes->set('_links', new GenericLinkProvider($cssPreloadLinks)); | ||
|
||
return; | ||
} | ||
|
||
if (!$linkProvider instanceof EvolvableLinkProviderInterface) { | ||
return; | ||
} | ||
|
||
foreach ($cssPreloadLinks as $link) { | ||
$linkProvider = $linkProvider->withLink($link); | ||
} | ||
|
||
$request->attributes->set('_links', $linkProvider); | ||
} | ||
} |
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.
Injecting the request stack here feels a bit odd, but it's the best way I can think of to handle this.