Skip to content
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

CSS asset preload error #6693

Closed
fracsi opened this issue Jan 8, 2025 · 2 comments
Closed

CSS asset preload error #6693

fracsi opened this issue Jan 8, 2025 · 2 comments
Labels
bug help wanted Issues and PRs which are looking for volunteers to complete them. status: confirmed
Milestone

Comments

@fracsi
Copy link
Contributor

fracsi commented Jan 8, 2025

Describe the bug
<link rel="preload"> only instruct the browser to preload the asset, but it does not instruct it to apply it.
See: MDN example
There should be 2 HTML link tags if you want to use HTML for preload.
Using symfony/web-link and preload twig function the Link header is added to the response HTTP headers.

To Reproduce
Add CSS assets with preload.

#[Override]
public function configureAssets(): Assets
{
    return parent::configureAssets()
        ->addCssFile(Asset::new('custom.css')->preload());
}

(OPTIONAL) Additional context
Generated HTML:

<link rel="preload" href="/custom.css">

Response header contains preload link as well (if the preload twig function is callable).

Link: </custom.css>; rel="preload"; as="style"

Possible solution
If preload twig function is available:

<link rel="stylesheet" href="{{ preload(asset(css_asset.value, css_asset.packageName), {as: 'style', nopush: css_asset.nopush}) }}" {% for attr, value in css_asset.htmlAttributes %}{{ attr }}="{{ value|e('html_attr') }}" {% endfor %}>

If preload is not available we need 2 link tags:

<link rel="preload" href="{{ asset(css_asset.value, css_asset.packageName) }}" as="style">
<link rel="stylesheet" href="{{ asset(css_asset.value, css_asset.packageName) }}" {% for attr, value in css_asset.htmlAttributes %}{{ attr }}="{{ value|e('html_attr') }}" {% endfor %}>
@javiereguiluz
Copy link
Collaborator

Yes, this looks like a bug. Can you please send a pull request to fix this? Thanks!

@javiereguiluz javiereguiluz added this to the 4.x milestone Jan 9, 2025
@javiereguiluz javiereguiluz added bug status: confirmed help wanted Issues and PRs which are looking for volunteers to complete them. labels Jan 9, 2025
javiereguiluz added a commit that referenced this issue Jan 10, 2025
This PR was merged into the 4.x branch.

Discussion
----------

Fix asset preloading

Fix for #6693

Commits
-------

1a3c3c8 Fix asset preloading
@javiereguiluz
Copy link
Collaborator

Fixed by #6697. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted Issues and PRs which are looking for volunteers to complete them. status: confirmed
Projects
None yet
Development

No branches or pull requests

2 participants