-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
PreloadAssetsEventListener does not use integrity hashes #225
Comments
Hey @weaverryan 👋 Could you possibly spare some time to take a look at this? I've just found the related PR #161 which has been open for well over a year, signifying the same issue with a proposed fix, but is has never been merged. |
@weaverryan @jrushlow please take a look at this. |
2 similar comments
@weaverryan @jrushlow please take a look at this. |
@weaverryan @jrushlow please take a look at this. |
Thank you for doing all this research, I just ran into this issue as well @ToshY. |
@ping-localhost No problem. I'm just slightly disappointed that PR #161 was created over 2 years ago and is still just sitting there collection dust. 🤷♂️ |
Problem
I've been scratching my head for a while on why I'm getting warnings in my Chrome console denoting the following:
A preload for 'https://example.com/build/850.b58e0264.css' is found, but is not used due to an integrity mismatch.
This happens when I enable integrity hashes in my
webpack.config.js
and havepreload: true
in thewebpack_encore.yaml
.Looking at the
Link
response header I see the following:Initially I didn't think much of it (as I don't experience with this at all), but after a bit of researching I landed at the Asset Preloading and Resource Hints with HTTP/2 and WebLink documentation. Here I've found the following section:
After following the link I saw this:
So basically "integrity metadata" is part of the (so-called) "preload entry".
Reproduction
./config/webpack-encore.yaml
./webpack.config.js
Very basic (truncated) config example, as it only concerns the option
.enableIntegrityHashes()
Cause
After finding out that the missing integrity in the
Link
response header might be the cause of the issue, I set out in trying to debug it 🕵️I've found that the files involved are
PreLoadAssetsEventListener
andTagRenderer
.PreLoadAssetsEventListener
In
PreLoadAssetsEventListener.php#L50-L68
this part is apparently creating the preload links.There is currently nothing here that adds the
integrity
to the link, regardless where is should get this info from (we shall see later it can be retrieved from theTagRenderer
).TagRenderer
The
TagRenderer
has 2 methods thare are directly called from inside thePreLoadAssetsEventListener
, which aregetRenderedScripts()
andgetRenderedStyles()
.webpack-encore-bundle/src/Asset/TagRenderer.php
Line 124 in 75cb918
webpack-encore-bundle/src/Asset/TagRenderer.php
Line 129 in 75cb918
Looking at both methods they will return either the script or styles that are available from
$this->renderedFiles
. However, both thescripts
andstyles
keys contain simple string arrays of hrefs, as they were added to these arrays in TagRenderer.php#L80 and TagRenderer.php#L118 respectively.The
integrity
for both is available as can be seen in TagRenderer.php#L62 and TagRenderer.php#L100, but can/is not being used anywhere else.Possible solution
After finding out all of the above, I've started trying to think of way on how to "fix" this without making breaking changes 🙃
TagRenderer::getAttributes()
In order to be able to get and use the integrity inside the
PreLoadAssetsEventListener
, we need to find a way to have theTagRenderer
return the complete attributes result (which contains the integrity hash).To do this I've thought of adding a private property
$attributes
with a public methodgetAttributes()
(similar to the existinggetRenderedScripts()
andgetRenderedStyles()
methods). At the end of the loop it will append to the array of$this->attributes
in a similar it way as it does for$this->renderedFiles
, however now for all attributes and not just thesrc
orhref
.TagRenderer
PreLoadAssetsEventListener
After adding
TagRenderer::getAttributes()
it can now be used in thePreLoadAssetsEventListener
.It boils down to adding the following snippet in both foreach statements:
PreLoadAssetsEventListener
Result
After applying the above solution I see the following
Link
header in my response.There are no longer warnings in the Chrome console regarding preload integrity mismatch.
TLDR
As I really would like this to be fixed, I can create a (draft) PR for this if that's okay. I'm not sure if the above solution is a proper way to do this in a technical sense, but that's what I came up with for now. 🙂
The text was updated successfully, but these errors were encountered: