-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[autoprefixer] Fix server side rendering #2172
[autoprefixer] Fix server side rendering #2172
Conversation
style = prefixer.prefix(style); | ||
} else { | ||
style = InlineStylePrefixer.prefixAll(style); | ||
} | ||
}, | ||
|
||
getPrefix(key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondering if we shouldn't remove this method. It's no longer used.
Confirmed that checking this branch out fixed the problem for me (I was having the problem in #2119). 👍 |
@bradparks You should probably report this issue to the inline-style-prefixer repository. |
[autoprefixer] Fix server side rendering
@oliviertassinari Hello, I don't know your process, when do you think a new version with this fix can be released ? |
Same here. When do expect the next release to go out? |
@sutat I forked the repo and have a compiled version available https://github.com/mfrye-intuit/material-ui. @oliviertassinari You guys have done a great job with this repo, but it would be nice if you practiced CI / CD. I've had to switch and pull from master so many times now because your latest code isn't pushed to npm. |
@oliviertassinari Do you have unit test for this fix? |
@raydemandforce Nop, but it would be great to have unit test for the server side rendering. We broke it up two times lately. |
Same issue as @vedraan. Solved the navigator.userAgent problem by defining it, no problem. I'm still getting the "React attempted to reuse markup in a container but the checksum was invalid" warning in-browser though. |
@mfrye-intuit Thank you I will try that this evening |
@oliviertassinari I think this PR should not be pushed to a release, you should optionally only update the package.json to use the latest The solution to the problem presented in this PR is a hack and doesn't work too good as you can see in my earlier post. It creates rendering differences between the server and client and throws errors client-side. The server _MUST_ know the user agent visiting the app, otherwise there is no guarantee that it will serve identical CSS as the one that will be generated client side. The node console log message was actually very correct saying:
It would just be good if you could post server side rendering instructions somewhere. Here is what I did and it works perfectly. If you already installed from master, uninstall and install the latest NPM release instead: Then, _in your server setup, for example if you are using Express to serve as I do, add this middleware before the route that is serving your application or before any other middleware you might have_:
And that's it! Perfect rendering, no errors in either node or browser console. I have tested using Chrome, FF, IE and simulating Googlebot and Facebook bot user agents. |
Yes, that's the original intent of #2007.
I'm not convinced, I think it can. However we could make three different improvement:
|
I'm not a fan of globals either. I'm not sure if you're thinking of React context as an alternative. I'm fairly new to react, but have already seen a large number of different approaches to server side rendering so I'm not sure if there is a reliable and an easy to understand (to beginners at least) way of passing the req.header to React context. I've never used context prior to material-ui, didn't need to. Perhaps instead of using globals something like this could be used in the middleware:
Which would then handle the rest. Regarding allowing the prefixAll in browser, I didn't really dig into how it all works (had no time) so I have no opinion about that part. As long as the server renders it the same as the client, it's good enough for me. |
@bdsabian @vedraan This middleware is not thread-safe according to me. _Exemple :_
If I miss something, feel free to tell me RTFM, but please give me the manual I have to read ! |
@sutat, you're right. It should probably be moved to context as suggested On Tue, Nov 17, 2015 at 12:36 PM, Sutat notifications@github.com wrote:
|
@sutat that's correct, node globals which are not constant are bad and should be avoided. The fact that such high concurrency will rarely happen doesn't mean this is OK. It fixes problems for my prototype app, but creating a middleware material-ui function which is not using globals is the right answer for production apps. |
@vedraan awesome solution, dude. Thank you! |
thank you very much for the solution, it worked perfectly |
So.. how do you create a middleware material-ui function to provide material-ui with GLOBAL.navigator, if Material UI is looking for the userAgent in GLOBAL.navigator? Does anyone have an example of this in code? |
@oliviertassinari sorry, i missed the last bit in your previous comment. If I understand this correctly, I think this is the way to go on the server side:
E.g. On the server, use prefixAll. That kind of makes sense, given a "normal" situation, you'd likely have all the vendor prefixes in your CSS. |
I wouldn't say that is the way to go on the server side. I proposed this to be more flexible. If you know the userAgent, it's more efficient to only prefix for this userAgent. |
Could you briefly mention why it's more efficient? In terms of size or processing? From my naive investigation, it looks like it's only used in 10 files. The The bigger picture is that to make this work properly (via browser prefixAll or user-decision), we'd have to include a |
It's more efficient because less data are transfered down the wires. So, it's in terme of size and network. |
Thanks for clarifying! |
@mattkrick I'm not sure if cutting out inline-style-prefixer and auto-prefixer would do more good than harm. That would mean having to rewrite every material-ui component to include the missing CSS properties to support all mobile and other browsers. Out of the box, a lot of them do not work well on all modern devices (for example late-gen Android phones) and they require CSS prefixes. |
Agreed, making sure we gather all the css, even older browsers, would be a pain. If you were willing to explore it, we could do a 2 part PR where part 1 includes the static text & additionally runs The only other alternatives I see are not supporting SSR (current) or having each prefixed component look for a |
Should fix #2159 and #2119