-
Notifications
You must be signed in to change notification settings - Fork 393
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
material-ui / jss / dynamic imports #99
Comments
I do not fully understand problem. I will need to investigate. Thanks for the tip |
Yes I was able to reproduce. Strange thing. I wonder how JSS works with SSR. UPD: oh my
render(<Button />, document.getElementById('app'), () => {
// We don't need the static css any more once we have launched our application.
const ssStyles = document.getElementById('server-side-styles')
ssStyles.parentNode.removeChild(ssStyles)
}) Another fixbasically JSS doesn't support rehydration import React from 'react';
import { hydrate, render } from 'react-dom';
import { loadComponents } from 'loadable-components';
import { getState } from 'loadable-components/snap';
import Index from './pages/index';
const app = <Index />;
const rootElement = document.getElementById('root');
loadComponents().then(() => {
render(app, rootElement, () => {
Array.from(document.querySelectorAll('[data-jss-snap]')).forEach(elem =>
elem.parentNode.removeChild(elem),
);
});
});
window.snapSaveState = () => {
Array.from(document.querySelectorAll('[data-jss]')).forEach(elem =>
elem.setAttribute('data-jss-snap', ''),
);
return getState();
}; |
Thank you @aheissenberger |
perfect, this solution was amazing , thanks a lot |
thanks @aheissenberger and @stereobooster! Very helpful! |
@aheissenberger I had today same problem with React.lazy() and JSS from Material-UI, without SSR. Your solution seems to be helpful. Do you think it is 1 to 1 related? I think so, but I'm not 100% sure. |
…box is big and blue#101. Create own name generator for css-classes to dodge conflicts in prerenderes versions. Otherwise we have totally unexspected behavior with our styling. stereobooster/react-snap#99
…box is big and blue#101. Create own name generator for css-classes to dodge conflicts in prerenderes versions. Otherwise we have totally unexspected behavior with our styling. stereobooster/react-snap#99
Another solution
|
A much easier solution for For people who are using For |
@matthewkwong2 |
If you want to do pre-rendering on a large scale project, then react-snap is not the right tool. It is more or less an experiment and prove of concept and have a lot of imperfection. Instead, you should use a proper solution like Gatsby. |
Yeah that makes sense but the project is already a long way ahead
…On Wed, May 5, 2021, 7:57 PM Matthew Kwong ***@***.***> wrote:
<NoSsr/>.
@matthewkwong2 <https://github.com/matthewkwong2>
how do ou know what components to wrap with <NoSsr/> with a large
application
If you want to do pre-rendering on a large scale project, then react-snap
is not the right tool. It is more or less an experiment and prove of
concept and have a lot of imperfection. Instead, you should use a proper
solution like Gatsby.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#99 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALIR2LPUV73ZDZRYGIQHTZTTMHLOPANCNFSM4EKQ4F6Q>
.
|
In our case of using Here is the solution we ended up with, that works every time: import React from 'react';
import { render, hydrate } from 'react-dom';
import { createGenerateId, JssProvider, SheetsRegistry } from 'react-jss';
import App from './App';
const rootElement = document.getElementById('root');
if (rootElement && rootElement.hasChildNodes()) {
hydrate(
<React.StrictMode>
<App />
</React.StrictMode>,
rootElement,
() => {
const reactSnapStyles = document.getElementById('react-snap-styles');
reactSnapStyles?.parentNode?.removeChild(reactSnapStyles);
},
);
} else {
const registry = new SheetsRegistry();
const generateId = createGenerateId();
render(
<JssProvider registry={registry} generateId={generateId}>
<React.StrictMode>
<App />
</React.StrictMode>
</JssProvider>,
rootElement,
() => {
if (navigator.userAgent === 'ReactSnap') {
const badStyles = document.querySelectorAll('[data-jss]');
badStyles.forEach((cssStyle) => cssStyle.parentNode?.removeChild(cssStyle));
const style = document.createElement('style');
style.innerHTML = registry.toString();
style.setAttribute('id', 'react-snap-styles');
const head = document.querySelector('head');
head.appendChild(style);
}
},
);
} Basically, when we're running the app with I hope this helps people that might end up here with a similar issue. |
The default class names (.jss123) created by jss will be merged from different dynamic imports which breaks the design.
Setup:
Solution:
replace default name generator (http://cssinjs.org/js-api/?v=v9.5.0#generate-your-own-class-names) with a variation which uses random strings as part of the class name and wrap your app with this HOC.
please add this to the recipe section of your docs
The text was updated successfully, but these errors were encountered: