-
Notifications
You must be signed in to change notification settings - Fork 308
Replaceable matchMedia() function for server side rendering #146
Conversation
Thanks @azazdeaz, this looks cool. In general, I don't think that we want to include something like Long term, I still think we need to do CSS generation for media queries + states as in #53 for proper isomorphic rendering, but this would help in the meantime. /cc @ianobermiller, thoughts? |
Yes, I like this. I'll add a bunch of comments in code, but I think a better and more scalable interface might be to expose a |
@@ -3,13 +3,14 @@ | |||
var prefix = require('./prefix'); | |||
|
|||
var kebabCase = require('lodash/string/kebabCase'); | |||
var isNode = require('detect-node'); |
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.
Instead of detect-node
, we should use a copy of React's execution environment (since we shouldn't be deep linking into React): https://github.com/facebook/react/blob/2aeb8a2a6beb00617a4217f7f8284924fa2ad819/src/vendor/core/ExecutionEnvironment.js
added functions: Config.canMatchMedia() Config.setMatchMedia() Config.matchMedia()
Hi! I rebased and made the changes regards your comments. Now the example looks like this: var Radium = require('radium');
var matchMediaMock = require('match-media-mock').create();
Radium.Config.setMatchMedia(matchMediaMock);
app.get('/app/:width/:height', function(req, res) {
matchMediaMock.setConfig({
type: 'screen',
width: req.params.width,
height: req.params.height,
});
var html = React.renderToString(<RadiumApp/>);
res.end(html);
}); And here you can check the match-media-mock module: https://github.com/azazdeaz/match-media-mock |
This looks great, thank you! |
Replaceable matchMedia() function for server side rendering
This pull request broke IE11. The |
Hi! We are using this fork both for server side rendering and generating mobile device preview on client side.
It replaces the
window.matchMedia()
calls withRadium.matchMedia()
which is working like a strategy so you can replace the currently used matchMedia function likeRadium.matchMedia.replace(iframe.contentWindow.matchMedia)
It provides css-mediaquery based substitution for
window.matchMedia()
(MatchMediaMock
) so it can be used with only js on the server side.ex:
As you see, it's not a full solution for the server side rendering because you have to be able to send some information about the client device in the request. I don't know whether this use case support is among your goals but if so, and you like this approach i would be happy to prepare this to be mergeable.