All Projects → denvned → Isomorphic Relay Router

denvned / Isomorphic Relay Router

Licence: bsd-2-clause
Adds server side rendering support to react-router-relay

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Isomorphic Relay Router

react-relay-example
Example project how to use React, Relay and TypeScript
Stars: ✭ 27 (-80.85%)
Mutual labels:  relay, react-router
react-ssr-spa
Server side rendered single page app using reactjs official libraries.
Stars: ✭ 30 (-78.72%)
Mutual labels:  isomorphic, react-router
server
Serve one or more react apps! - Custom routes, HotReloading, Authenticated Apps/routes, Relay, Webpack..
Stars: ✭ 20 (-85.82%)
Mutual labels:  relay, isomorphic
Arc
React starter kit based on Atomic Design
Stars: ✭ 2,780 (+1871.63%)
Mutual labels:  isomorphic, react-router
React App
Create React App with server-side code support
Stars: ✭ 614 (+335.46%)
Mutual labels:  relay, isomorphic
Isomorphic Relay
Adds server side rendering support to React Relay
Stars: ✭ 247 (+75.18%)
Mutual labels:  relay, isomorphic
isomorphic-relay-app
Example isomorphic React-Relay-(Modern / Classic)-Router app and helper lib that connects to Artsy's GraphQL service
Stars: ✭ 13 (-90.78%)
Mutual labels:  relay, isomorphic
Isomorphic Lab
Isomorphic React experimentation
Stars: ✭ 144 (+2.13%)
Mutual labels:  isomorphic, react-router
Isomorphic React
A simple but powerful React application built on a standards-compliant back-end
Stars: ✭ 318 (+125.53%)
Mutual labels:  isomorphic, react-router
isomorphic-relay-boilerplate
No description or website provided.
Stars: ✭ 30 (-78.72%)
Mutual labels:  relay, isomorphic
Universal React Router4
Demo app showing how to use react-router v4 for server- and client-side rendering
Stars: ✭ 216 (+53.19%)
Mutual labels:  isomorphic, react-router
Universal React Tutorial
📓 How to build universal web apps with React.
Stars: ✭ 136 (-3.55%)
Mutual labels:  isomorphic, react-router
React Pages
A complete solution for building a React/Redux application: routing, page preloading, (optional) server-side rendering, asynchronous HTTP requests, document metadata, etc.
Stars: ✭ 182 (+29.08%)
Mutual labels:  isomorphic, react-router
universal-react-relay-starter-kit
A starter kit for React in combination with Relay including a GraphQL server, server side rendering, code splitting, i18n, SEO.
Stars: ✭ 14 (-90.07%)
Mutual labels:  relay, isomorphic
React Redux Styled Hot Universal
react boilerplate used best practices and focus on performance
Stars: ✭ 147 (+4.26%)
Mutual labels:  isomorphic, react-router
react-ssr-starter
🔥 ⚛️ A React boilerplate for a universal web app with a highly scalable, offline-first foundation and our focus on performance and best practices.
Stars: ✭ 40 (-71.63%)
Mutual labels:  isomorphic, react-router
FlipED
A LMS built specifically for Thailand's Education 4.0 system.
Stars: ✭ 24 (-82.98%)
Mutual labels:  isomorphic, react-router
Preact Redux Isomorphic
preact-redux-isomorphic PWA SPA SSR best practices and libraries in under 80kB page size (for live demo click the link below)
Stars: ✭ 85 (-39.72%)
Mutual labels:  isomorphic, react-router
Project Webcube
Continuously updated JS infrastructure for modern web dev
Stars: ✭ 141 (+0%)
Mutual labels:  isomorphic, react-router
Egg React Ssr
最小而美的Egg + React + SSR 服务端渲染应用骨架,同时支持JS和TS
Stars: ✭ 1,837 (+1202.84%)
Mutual labels:  isomorphic

Isomorphic react-router-relay npm version

Adds server side rendering support to react-router-relay using isomorphic-relay.

Installation

npm install -S isomorphic-relay isomorphic-relay-router

How to use

Create a Relay network layer on the server. And if you are using Relay.DefaultNetworkLayer, specify the full url to the GraphQL endpoint:

const GRAPHQL_URL = `http://localhost:8080/graphql`;

const networkLayer = new Relay.DefaultNetworkLayer(GRAPHQL_URL);

When processing a request on the server, get renderProps using match function from react-router (see here), prepare the data using IsomorphicRouter.prepareData, then render React markup using IsomorphicRouter.render (pass the props returned by IsomorphicRouter.prepareData), and send the React output along with the data to the client:

import IsomorphicRouter from 'isomorphic-relay-router';

app.get('/*', (req, res, next) => {
  match({routes, location: req.originalUrl}, (error, redirectLocation, renderProps) => {
    if (error) {
      next(error);
    } else if (redirectLocation) {
      res.redirect(302, redirectLocation.pathname + redirectLocation.search);
    } else if (renderProps) {
      IsomorphicRouter.prepareData(renderProps, networkLayer).then(render).catch(next);
    } else {
      res.status(404).send('Not Found');
    }

    function render({data, props}) {
      const reactOutput = ReactDOMServer.renderToString(IsomorphicRouter.render(props));

      res.render(path.resolve(__dirname, '..', 'views', 'index.ejs'), {
        preloadedData: JSON.stringify(data),
        reactOutput
      });
    }
  });
});

On page load in the browser, create an instance of Relay.Environment, inject an Relay network layer to it. Get renderProps using match function from react-router, inject the prepared data to the Relay store using IsomorphicRelay.injectPreparedData, then prepare initial render using IsomorphicRelay.prepareInitialRender, and render React using Router from react-router (pass the props returned by IsomorphicRouter.prepareInitialRender):

import IsomorphicRouter from 'isomorphic-relay-router';

const environment = new Relay.Environment();

environment.injectNetworkLayer(new Relay.DefaultNetworkLayer('/graphql'));

const data = JSON.parse(document.getElementById('preloadedData').textContent);

IsomorphicRelay.injectPreparedData(environment, data);

const rootElement = document.getElementById('root');

// use the same routes object as on the server
match({routes, history: browserHistory}, (error, redirectLocation, renderProps) => {
  IsomorphicRouter.prepareInitialRender(environment, renderProps).then(props => {
    ReactDOM.render(<Router {...props} />, rootElement);
  });
});

Example

See here.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].