Universal React Express middleware package. A universal react dev environment provided through npm install
Kube comes with with a middleware that will,
react-router/routes
component serverside Webpack module loaders are awesome and brings lot of value to your react development. You can now compose a package with all it’s depedencies together. However, problems rise when you attempt to use module loaders for server side rendering purposes. Normal implementations of SSR uses the React-DOMServer to render a component to string. When the DOMServer attempts to require in a file(like an image) normally handled by a webpack loader, it will error out. Node’s require expects all loaded files to be a JSON.
Using Kube, components rendered from the server are also prebuilt using webpack. Meaning all the module loaders have parsed through the require statements already. This results in a clean translation between client/server, allowing you to easily use module loaders on react universally.
The repo currently contains a boilerplate demo. To try,
npm install -g kube
cd your-project-dir
kube
npm install –save-dev kube
// /index.js const express = require('express'); const app = express() /* Loads middleware and provides 1. the webpack dev server 2. the webpack hot moldule reloader 3. res.kube.render */ require('kube')(app) app.get('/', function(req,res){ let state = { ssr: 'server state' } res.kube.render(state) })
// /src/routes.js import React from 'react'; import {IndexRoute, Route} from 'react-router'; const Home = function(props){ return <div>{JSON.stringify(props.location.state)}</div> } export default ( <Route path="/" component={Home} /> );
// /src/index.js import React from 'react'; import ReactDOM from 'react-dom'; import { RouterContext, Router, browserHistory } from 'react-router'; import routes from './routes'; const render = function(props){ props.location.state = window.state return <RouterContext {...props} /> } ReactDOM.render( <Router history={browserHistory} render={render}> {routes} </Router>, document.getElementById('root') );
Currently, Reax is still in heavy development and being ironed out. There are many places for improvement to add flexibility and configuration. In the current stage there are some strong dependencies the app requires. Until configuration api end points are created, please keep the following in mind.
Some form of the 3 files mentioned above need to exist.
/index/js
: is flexible but needs to load in the hackable-reax middleware /src/routes.js
: res.reax.render
depends on react-router/routes
to automatically create your components per a given url. This file must currently exist at /src/routes.js
/src/index.js
: this is the entry point for your browser side code. Again, this file must exist and implement ReactRouter
to render the routes
to the dom. Also must exist at /src/index.js
转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Kube – Universal React with HMR in a Package