是的!我們目前正在這樣做:
// src/server/views/index-view.js
// …
module.exports = (req) => {
const app = (req.server && req.server.app) || req.app;
if (!app.routesEngine) {
app.routesEngine = new ReduxRouterEngine({
routes,
createReduxStore,
stringifyPreloadedState
});
}
return app.routesEngine.render(req);
};
這會在每次請求Express時提供。
如果您需要在初始化期間設置您的應用需要的配置參數,還可以在express-server.js
中創建存儲(並在index-view.js
中重新創建它以避免交叉連接污染)。
// express-server.js
// …
const loadConfigs = (userConfig) => {
if (_.get(userConfig, 'plugins.electrodeStaticPaths.enable')) {
// eslint-disable-next-line no-param-reassign
userConfig.plugins.electrodeStaticPaths.enable = false;
}
return Confippet.util.merge(Confippet.config, userConfig);
};
// fetch appContext FIRST because components may need it to load, eg: language
const createReduxStore = (config) => store.dispatch({
type: 'ADD_INITIAL_STATE',
payload: { appContext: config.$('storeConfig') },
})
// eslint-disable-next-line no-console
.catch(error => console.error('DISPATCH ERROR:', error))
// return the now-hydrated store
.then(() => store);
module.exports = function electrodeServer(userConfig, callback) {
const promise = Promise.resolve(userConfig)
.then(loadConfigs)
.then(createReduxStore)
// …
return callback ? promise.nodeify(callback) : promise;
};
然後電極將使其可以作爲類似window.__PRELOADED__