GoogleChromeLabs / prerender-loader

📰 Painless universal pre-rendering for Webpack.
https://npm.im/prerender-loader
Apache License 2.0
1.9k stars 50 forks source link

Incorrect hash for dynamic CSS files not using contenthash, #17

Closed isidrok closed 6 years ago

isidrok commented 6 years ago

Hi, when using prerender-loader in conjunction with mini-css-extract-plugin there is an error loading CSS imported by dynamic-chunks when the hashing strategy is different from contenthash for that css file.

In that case, prerender-loader outputs a link pointing to a CSS file with a different hash.

Note that this doesn't apply to JS files nor to CSS files imported by the main (entrypoint) chunk.

A full reproduction is in https://github.com/isidrok/prerender-loader-reprod, look at the readme for more information.

The original discussion is in GoogleChromeLabs/critters#7

developit commented 6 years ago

This should be fixed now as long as you upgrade to the latest (0.4.4) mini-css-extract-plugin!

developit commented 6 years ago

Closing since you gave it a thumbs up - let me know if you're still hitting issues!

isidrok commented 6 years ago

Just tried with the new version of mini-css-extract-plugin and neither critters nor prerender-loader output any error but the referenced css file is still incorrect for dynamically imported modules.

If I remove prerender-loader and just use Critters the following error is thrown: TypeError: Cannot read property 'textContent' of undefined.

The css file referenced in the bundle under the comment mini-css-extract-plugin CSS loading is correct though.

After looking at the mini-css-extract-plugin source it seems they are handling hashes on the hashForChunk and contentHash hooks, maybe that happens after prerender-loader does its work, will check later.

Edit: after debugging the reproduction repo I saw that mini-css-extract-plugin hooks are running twice, once before prerender-loader and once again inside (it is beign added to the new compiler plugins), and it seems different assets (with different names) are being generated on each compilation which seems normal since using hash is supposed to create a different hash for each build?

Anyways I'm using contenthash and it works just fine so it may be enough with having this issue as reference...

isidrok commented 6 years ago

Summarizing the previous comment:

My guess is that since prerender-loader is creating a new compilation, when using a hash different from contenthash it changes during that specific build and doesn't reference the assets of the main compilation.

developit commented 5 years ago

That makes sense. Perhaps it'd be worth having the plugin warn when [hash] is used.