When using either the SSR example or the CRA example in production mode (npm start production in the SSR example) the SSR output does not contain .jssN class names, it contains the same classnames as non production mode (.Mui*).
Classnames look like .jssN
Classnames look like dev mode .Mui*
run the SSR example npm run production, view source.
or run the CRA example:
$ npm run build
$ serve ./build
inspect output
| Tech | Version |
|--------------|---------|
| Material-UI | v4.1.0 |
| React | |
| Browser | |
| TypeScript | |
| etc. | |
This is the desired output.
Ah. This has changed from 3.9 > 4 then? What was the reason?
@guybowden The motivation is explained in https://medium.com/material-ui/material-ui-v4-is-out-4b7587d1e701. It helps styled-components & SASS users. You can restore the v3 behavior with the disableGlobal class name generator option.
So.. reason I raised this issue was because in the course of upgrading a (server side rendered) app, I changed the ssr code to match the example / docs, except I also needed a custom productionPrefix to stop clashes with a second on-page MUI app.
The new SSR example does not allow for this as the new ServerStyleSheets class masks the additional complexity (yay) of creating StylesProviders etc.
I thought it was not working because initially the styles all looked like non-production. Then on closer inspection, there were some .jssN classes in there.
I have solved my issue by doing the following - it may help others.
import React from "react";
import {
ServerStyleSheets,
StylesProvider,
createGenerateClassName,
} from "@material-ui/styles";
import { SheetsRegistry } from "jss";
export default class CustomServerStyleSheets extends ServerStyleSheets {
collect(children) {
// This is needed in order to deduplicate the injection of CSS in the page.
const sheetsManager = new Map();
// This is needed in order to inject the critical CSS.
this.sheetsRegistry = new SheetsRegistry();
// A new class name generator
const generateClassName = createGenerateClassName({
productionPrefix: "MY_PRODUCT_PREFIX",
});
return (
<StylesProvider
sheetsManager={sheetsManager}
serverGenerateClassName={generateClassName}
sheetsRegistry={this.sheetsRegistry}
{...this.options}
>
{children}
</StylesProvider>
);
}
}
I can then use this class in place.
stop clashes with a second on-page MUI app
We have a seed option in the class name generator for this problem.
@guybowden You can pass options to the ServerStyleSheets to overwrite the default serverGenerateClassName prop. This should work without creating a custom class:
const sheets = new ServerStyleSheets({
serverGenerateClassName: createGenerateClassName({
disableGlobal: true,
productionPrefix: 'custom',
});,
});
Most helpful comment
@guybowden The motivation is explained in https://medium.com/material-ui/material-ui-v4-is-out-4b7587d1e701. It helps styled-components & SASS users. You can restore the v3 behavior with the disableGlobal class name generator option.