我正在编写一个基于以下内容的同构 React 应用程序:
https://github.com/choonkending/react-webpack-node
我想使用 scss 而不是示例中使用的 css module。出于某种原因,我很难让他们工作。我的第一步是css
从服务器和客户端配置中删除webpack 加载器,将它们替换为scss
特定的加载器(以及删除postcss
):
loaders: [
'style-loader',
'css-loader?modules&localIdentName=[name]_[local]_[hash:base64:3]',
'sass-loader?sourceMap',
]
但是ReferenceError: window is not defined
当构建为 style-loader 显然不适合服务器端渲染时会抛出这个问题。所以我的下一个想法是使用isomorphic-style-loader
. 据我所知,要让它工作,我需要用它们的高阶组件来装饰我的组件withStyles
:
import React, { PropTypes } from 'react';
import classNames from 'classnames';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from '../assets/scss/common/index.scss';
const App = (props, context) => (
<div className={classNames('app')}>
<h1 className="home_header">Welcome!</h1>
{props.children}
</div>
);
export default withStyles(s)(App);
然后在服务器上的代码呈现页面中做一些技巧。但问题是,包文档中的示例显示了在 Express ( https://libraries.io/npm/isomorphic-style-loader#webpack-configuration ) 中触发的通量操作,并且我使用的样板文件使用react-router
. 所以我有点迷茫,我应该如何将这个对象注入insertCss
到上下文中。我试过这个:
import React from 'react';
import { renderToString } from 'react-dom/server';
import { RouterContext, match, createMemoryHistory } from 'react-router';
import axios from 'axios';
import { Provider } from 'react-redux';
import createRoutes from 'routes.jsx';
import configureStore from 'store/configureStore';
import headconfig from 'components/Meta';
import { fetchComponentDataBeforeRender } from 'api/fetchComponentDataBeforeRender';
const clientConfig = {
host: process.env.HOSTNAME || 'localhost',
port: process.env.PORT || '3001'
};
// configure baseURL for axios requests (for serverside API calls)
axios.defaults.baseURL = `http://${clientConfig.host}:${clientConfig.port}`;
function renderFullPage(renderedContent, initialState, head = {
title: 'cs3',
css: ''
}) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
${head.title}
${head.link}
<style type="text/css">${head.css.join('')}</style>
</head>
<body>
<div id="app">${renderedContent}</div>
<script type="text/javascript">window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};</script>
<script type="text/javascript" charset="utf-8" src="/assets/app.js"></script>
</body>
</html>
`;
}
export default function render(req, res) {
const history = createMemoryHistory();
const store = configureStore({
project: {}
}, history);
const routes = createRoutes(store);
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
const css = [];
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
const context = { insertCss: (styles) => css.push(styles._getCss()) };
const InitialView = (
<Provider context={context} store={store}>
<RouterContext {...renderProps} />
</Provider>
);
fetchComponentDataBeforeRender(store.dispatch, renderProps.components, renderProps.params)
.then(() => {
const componentHTML = renderToString(InitialView);
const initialState = store.getState();
res.status(200).end(renderFullPage(componentHTML, initialState, {
title: 'foo',
css
}));
})
.catch(() => {
res.end(renderFullPage('', {}));
});
} else {
res.status(404).send('Not Found');
}
});
}
但我仍然有Warning: Failed context type: Required context 'insertCss' was not specified in 'WithStyles(App)'.
任何想法如何解决这个问题?更重要的是 - 没有更简单的方法吗?这似乎是很多额外的工作。