React Admin displays very messed up

前端 未结 2 1515
不知归路
不知归路 2021-01-12 18:10

My RA project renders fine when deploying locally, however, when I copy my build directory to an S3 bucket for deployment, it renders all messed up. Sometimes it works, but

2条回答
  •  無奈伤痛
    2021-01-12 18:14

    There is an issue with the production build if you have a different version of @material-ui in your dependencies. And it will be fixed in the next version.

    Here is the full issue: https://github.com/marmelab/react-admin/issues/1782

    Meanwhile, you can write a workaround (also available in the issue): write your own class generator for JSS.

    const escapeRegex = /([[\].#*$><+~=|^:(),"'`\s])/g;
    let classCounter = 0;
    
    // Heavily inspired of Material UI:
    // @see https://github.com/mui-org/material-ui/blob/9cf73828e523451de456ba3dbf2ab15f87cf8504/src/styles/createGenerateClassName.js
    // The issue with the MUI function is that is create a new index for each
    // new `withStyles`, so we handle have to write our own counter
    export const generateClassName = (rule, styleSheet) => {
        classCounter += 1;
    
        if (process.env.NODE_ENV === 'production') {
            return `c${classCounter}`;
        }
    
        if (styleSheet && styleSheet.options.classNamePrefix) {
            let prefix = styleSheet.options.classNamePrefix;
            // Sanitize the string as will be used to prefix the generated class name.
            prefix = prefix.replace(escapeRegex, '-');
    
            if (prefix.match(/^Mui/)) {
                return `${prefix}-${rule.key}`;
            }
    
            return `${prefix}-${rule.key}-${classCounter}`;
        }
    
        return `${rule.key}-${classCounter}`;
    };
    

    And wrap the admin with a JSSProvider:

    import JssProvider from 'react-jss/lib/JssProvider';
    
    export default () => (
        
            
        
    );
    

    It should fix your CSS issue, if it's related to JSS classes minification and Material-UI.

提交回复
热议问题