fire-hydrant
lightweight hydration and serialization of state, targeted at Redux Server Side Rendering and ImmutableJS
Install
npm i -S fire-hydrant
Documentation
Functions used to store state objects, partial immutables, and full immutables to strings and restore them back to their original state.
react-router and react-router-redux with async routes example
configureStore.js
{ const reducer = const enhancer = return }
createRouter.js
Plug this router into your server.use()
import React from 'react'import Router from 'router'import Immutable from 'immutable'import Provider from 'react-redux'import createMemoryHistory match RouterContext from 'react-router'import syncHistoryWithStore from 'react-router-redux' import serialize createInitialState from 'fire-hydrant' /** Routes can be direct server defined routes or a webpack bundle */import routes from './routes' const InitialState = state const serialized = const __html = `window.__initialState__ = ` return <script = /> //-- ALTERNATE: Import createInitialState component factory for sanity checks and less boilerplate.const InitialState = const HTML = content store const state = store ? store : return <html> <head> <meta ="utf-8" /> <title>Fire Hydrant</title> </head> <body> <InitialState = /> <div ="root" =/> <script ="/app.js" /> </body> </html> const renderHTML = `<!doctype html>` { const router = router return router}
app.js
Client entry point - This is where the state gets rehydrated.
import React from 'react'import Immutable from 'immutable'import render from 'react-dom'import Provider from 'react-redux'import match Router browserHistory from 'react-router'import syncHistoryWithStore from 'react-router-redux' import fromHydrant from 'fire-hydrant' import routes from './routes' /** Rehydrate our server built state that is stored in __initialState__ global */const initialState = const store = const history = const rootElement = document
Unit tests
const regularObj = a: 'something' b: 1 2 3 c: foo: bar: true const partialImmutable = ...regularObj d: Immutable const topLevelImmutable = Immutable