1.0.3 • Published 6 years ago

get-loadables-and-graphql v1.0.3

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

npm

Package was built to have unanimous capability to use react-apollo and loadable-components while approaching ssr.

Follow setups required for react-apollo and loadable-components.

Install using:

npm install get-loadables-and-graphql

After having followed setup for react-apollo and loadable-components

import GetLoadableAndGraphqlData from 'get-loadables-and-graphql';

// Code needed - middlewares
const appComponent = (
  <ApolloProvider client={ apolloClient }>
    <App
      />
  </ApolloProvider>
);

const loadableScripts = await GetLoadableAndGraphqlData( appComponent );
const app = ReactDOM.renderToString( appComponent );
const initialState = apolloClient.extract();

const html  = `
  <html>
    <body>
      <script
        dangerouslySetInnerHTML={ {
          __html: this.props.loadableScripts,
        } }
        />
      <script
        dangerouslySetInnerHTML={ {
          __html: `window.__APOLLO_STATE__=${ JSON.stringify( this.props.initialState ).replace( /</g, '\\u003c' ) };`,
        } }
        />
    </body>
  </html>
`;