noscript-Support für CSS ergänzt#44
noscript-Support für CSS ergänzt#44danspringer wants to merge 2 commits intoFriendsOfREDAXO:masterfrom
Conversation
Noscript für CSS ergänzt für render-blocking-assets, media="all" als default gestrichen, wegen best-practice
Noscript für CSS ergänzt für render-blocking-assets, media="all" als default gestrichen, wegen best-practice
There was a problem hiding this comment.
Pull Request Overview
This PR adds <noscript> support for CSS assets to avoid render-blocking and removes the default media="all" to follow best practices for loading CSS.
- Introduces a
noscriptflag ingetLinkTag()to wrap stylesheets in<noscript>tags with preload and onload attributes. - Changes
setCss()to require explicit media values (no default) and to accept the newnoscriptparameter. - Updates
getCss()to pass thenoscriptflag through to the link tag renderer.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/theme_assets_trait.php | Extended getLinkTag() with a noscript parameter and onload logic. |
| lib/theme_assets.php | Modified setCss() and getCss() to remove default media and support noscript. |
Comments suppressed due to low confidence (3)
lib/theme_assets_trait.php:41
- The docblock above
getLinkTag()needs to be updated to include the new@param bool $noscriptso that the purpose and default behavior of this parameter are clear.
private function getLinkTag(string $key, string $file, array $attributes, string $cache_buster = '', bool $noscript = false): string
lib/theme_assets.php:66
- This change is a breaking API update: removing the default
media='all'and adding the$noscriptparameter will affect existing callers. Consider documenting this change clearly or providing a migration path.
public function setCss(string $key, string $data, string $media = '', array $attributes = [], bool $noscript = false): theme_assets
lib/theme_assets.php:66
- Update the docblock for
setCss()to describe the new$noscriptparameter and note that themediaparameter no longer defaults to'all'.
public function setCss(string $key, string $data, string $media = '', array $attributes = [], bool $noscript = false): theme_assets
| $attributes['type'] = $attributes['type'] ?? 'text/css'; | ||
|
|
||
| return '<link'.rex_string::buildAttributes($attributes).' />'.PHP_EOL; | ||
| return '<link'.rex_string::buildAttributes($attributes).$onload.' />'.$noscript_tag.PHP_EOL; |
There was a problem hiding this comment.
[nitpick] Consider inserting a newline or space before $noscript_tag so that the <noscript> element is rendered on its own line for improved readability of the generated HTML.
| return '<link'.rex_string::buildAttributes($attributes).$onload.' />'.$noscript_tag.PHP_EOL; | |
| return '<link'.rex_string::buildAttributes($attributes).$onload.' />'.PHP_EOL.$noscript_tag.PHP_EOL; |
|
Hi, Allerdings finde ich die Anpassung so noch nicht ideal:
Soweit ich mir das auf die Schnelle anlesen konnte, gibt es ein Best-Practise-Setting, mit dem man das Thema dann standardisiert angehen kann.
|
Noscript für CSS ergänzt für render-blocking-assets,
media="all"als default gestrichen, wegen best-practice.https://developer.chrome.com/docs/lighthouse/performance/render-blocking-resources?hl=de
man kann nun wie folgt css einbinden:
$assets->setCss('plyr', '/assets/addons/plyr/vendor/plyr/dist/plyr.css','', ['rel' => 'preload', 'as' => 'style'], true);daraus wird dann:
<link rel="preload" as="style" href="/assets/addons/plyr/vendor/plyr/dist/plyr.css" type="text/css" onload="this.onload=null;this.rel='stylesheet'" /><noscript><link rel="stylesheet" href="/assets/addons/plyr/vendor/plyr/dist/plyr.css"></noscript>