服务器端渲染无法自动更新
来源:2-8 开发时的服务端渲染

Joeeeee
2017-12-08
```
const axios = require('axios');
const webpack = require('webpack');
const path = require('path');
const MemoryFs = require('memory-fs');
const proxy = require('http-proxy-middleware');
const ReactDomServer = require('react-dom/server');
const serverConfig = require('../../build/webpack.config.server');
const getTemplate = () => {
return new Promise((resolve, reject) => {
axios.get('http://localhost:8888/public/index.html')
.then((res) => {
resolve(res.data);
})
.catch(reject);
});
};
const Module = module.constructor;
const mfs = new MemoryFs();
const serverCompiler = webpack(serverConfig);
serverCompiler.outputFileSystem = mfs;
let serverBundle;
serverCompiler.watch({}, (err, stats) => {
if (err) throw err;
stats = stats.toJson();
stats.errors.forEach(err => console.error(err));
stats.warnings.forEach(warn => console.warn(warn));
const bundlePath = path.join(
serverConfig.output.path,
serverConfig.output.filename,
);
const bundle = mfs.readFileSync(bundlePath, 'utf-8');
const m = new Module();
m._compile(bundle, 'server-entry.js');
serverBundle = m.exports.default;
});
module.exports = function(app) {
app.use('/public', proxy({
target: 'http://localhost:8888',
}));
app.get('*', (req, res) => {
getTemplate().then((template) => {
const content = ReactDomServer.renderToString(serverBundle);
res.send(template.replace('<!-- app -->', content));
});
});
};
```
照着视频来敲的,哪里出了问题?
热更新返回的json是html来的
1回答
-
你看一下webpack配置的output.publicPath,确保他的值是'/public/'而不是'/public'。
062019-04-08
相似问题