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

Chunk css is inserted before bundle on production, inconsistent with dev environment #1106

Closed
Inviz opened this issue Apr 2, 2020 · 11 comments · Fixed by #1185
Closed

Chunk css is inserted before bundle on production, inconsistent with dev environment #1106

Inviz opened this issue Apr 2, 2020 · 11 comments · Fixed by #1185

Comments

@Inviz
Copy link

Inviz commented Apr 2, 2020

Steps to reproduce

  1. Use some css in the bundle
  2. Create a lazy loading chunk with some css in it
  3. Navigate to the page that loads the chunk

Expected Behavior

In production environment, I expect both csses to be in the same order as in dev environment - chunk after bundle.

Actual Behavior

  1. In development <style> element of the chunk will be injected after the bundle css
  2. In production <link> element of the chunk will be inserted before the bundle css (chunk in head, bundle before </body>)

Screen Shot 2020-04-02 at 14 37 51

Conversation in Slack with prateekbh

ohh i guess i know whats happening
bundle is placed by. us at the end of the body
where as chunks are inserted by webpack
at runtime in headtag

@JoviDeCroock
Copy link
Member

I’ll transfer this issue to preact-cli

@JoviDeCroock JoviDeCroock transferred this issue from preactjs/preact Apr 2, 2020
@Inviz
Copy link
Author

Inviz commented Apr 2, 2020

Thanks!

The reason stylesheet order matters is that it's a tie-breaker in case multiple selectors with the same specificity affect the same properties of the same element. Stylesheets coming later overpower the ones coming earlier. Right now i'm seeing different visual results between production and development.

@developit
Copy link
Member

Ah this makes sense. We need to adjust where we are injecting the prerendered chunks. Maybe we could just set inject:'body' here?

@Inviz
Copy link
Author

Inviz commented Apr 7, 2020

@developit I tried

  helpers.getPluginsByName(config, 'HtmlWebpackPlugin')[0].plugin.options.inject = 'body';

but this didn't seem to do anything

@prateekbh
Copy link
Member

I posted an update here:
webpack-contrib/mini-css-extract-plugin#212

May be for now we can just make them consistent? would that help?

@Inviz
Copy link
Author

Inviz commented Apr 9, 2020

Yeah that would certainly help for me. If possible, that chunks should be inserted after the bundle.

@Inviz
Copy link
Author

Inviz commented Apr 9, 2020

I personally don't mind either all of them will be in head, or all of them be in body. thank you

@developit
Copy link
Member

developit commented May 8, 2020

This is actually caused by Critters: it inlines your critical CSS into a style tag in <head>, then moved the <link> tag to the end of the document so it loads without blocking JS.

Critters has a preload:"swap" option that does this in-place - that might work better for you?

One note: the production output ordering is more intentional here than development ordering. During development there's no prerendering and CSS is injected dynamically, so the ordering is inconsistent.

@prateekbh
Copy link
Member

@developit do you think our currrent choice of preload strategyy is good or should we switch to swap since more people might encounter this?

@developit
Copy link
Member

I avoided swap as the default here because it requires JavaScript, but I think 100% of CLI apps would fail without JS enabled anyway..

@prateekbh
Copy link
Member

makes sense, also this will make everything a lot more intuitive i agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants