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 is different between local development build and built for prod #4518

Closed
SimenB opened this issue Mar 26, 2021 · 3 comments
Closed

CSS is different between local development build and built for prod #4518

SimenB opened this issue Mar 26, 2021 · 3 comments
Labels
bug An error in the Docusaurus core causing instability or issues with its execution closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests)

Comments

@SimenB
Copy link
Contributor

SimenB commented Mar 26, 2021

🐛 Bug Report

When running docusaurus start the CSS is different than running docusaurus build followed by docusaurus serve.

Have you read the Contributing Guidelines on issues?

Yup

To Reproduce

  1. Check out chore: remove sass from website jestjs/jest#11229
  2. Run yarn from root
  3. cd into website/
  4. Compare yarn docusaurus start and yarn docusaurus build && yarn docusaurus serve

Expected behavior

The website should look identical

Actual Behavior

The CSS looks different

Dev:

image

Prod:

image

I assume this is some bug in the ordering of css files when built vs running in dev mode.

Your Environment

Reproducible Demo

Checkout the branch from jestjs/jest#11229 (https://github.com/SimenB/jest/tree/remove-sass), instructions are above in this issue (it's just "run the website"). From that PR you can also see that the netlify preview shows the same CSS as the local docusarus build version

@SimenB SimenB added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Mar 26, 2021
@slorber
Copy link
Collaborator

slorber commented Mar 26, 2021

Hi,

As explained in DM, this is not an issue with Docusaurus itself but rather it's an issue with the site that has conflicting CSS rules that are too dependent on CSS insertion order.

CSS is code-splitted by Webpack and lazy-loaded and inserted in pages on demand.

For example when you go to https://jestjs.io/help, the following CSS files load:

image

Now when you navigate to the homepage you get new CSS files loaded on-demand:

image

This order may be different according to the navigation patterns of the user, webpack code-splitting heuristics, and you should make sure that one CSS chunk does not mess-up with another CSS chunk and that the order somehow does not matter.

That's why we use CSS-in-JS or methodologies like BEM: to avoid polluting the global CSS namespace and preventing this kind of problem from happening.

You didn't have this on Docusaurus 1 because on each navigation you "reset" the page CSS as it's not a real SPA navigation. The problem appears in Docusaurus 2 because it's an SPA navigation, but you'll have the same problem with CRA, Gatsby, Next, VuePress...

Let's continue this in DM and find a solution for the Jest website.

May also be related to #3678

@slorber slorber closed this as completed Mar 26, 2021
@SimenB
Copy link
Contributor Author

SimenB commented Mar 26, 2021

I still think it's a bug that dev and prod do not agree on a fresh navigation (or refresh) on the same page. It's not about navigation in that case - some prod optimization breaks css insertion order. The fact other frameworks may or may not have the same bug doesn't make it any less of a bug.

But of course if we fix this for Jest it doesn't really impact me anymore, so I'm happy with that resolution 😀

@slorber
Copy link
Collaborator

slorber commented Mar 26, 2021

It also affects other projects of the React ecosystem.

facebook/create-react-app#7190
gatsbyjs/gatsby#9733
gatsbyjs/gatsby#9733

If you have any solution to fix this problem let me know, but I don't have any apart advising you to scope your styles and make them independent or insertion order and specificity issues.

All this is why we invented CSS in JS:
https://speakerdeck.com/vjeux/react-css-in-js

image

Here the problem is "non-deterministic resolution". It's not something new and it affects many sites for many years already.

@Josh-Cena Josh-Cena added closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests) and removed status: needs triage This issue has not been triaged by maintainers labels Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests)
Projects
None yet
Development

No branches or pull requests

3 participants