Apollo SSR
with Koa

Motivation

We need to produce a mutiple unique web page with unique meta data for FB crawl or Line crawl

As we know...

Client-Side Rendering only have one index.html

<!DOCTYPE html>
<html>
  <head>  
    <body>
      <div id="root" />
      <script src="/bundle.js"></script>
    </body>
  </head>
</html>

FB or Line Crawler
Not Excute JS

We can not only use CSR to solve this problem

Server Side Rendering

  1. How it Works

  2. Pros and cons

How it Works

How it Works

How it Works

How it Works

Props and Cons

Props

  1. Search engines can crawl the site for better SEO.

  2. The initial page load is faster.

  3. Great for static sites.

Props and Cons

Cons

  1. Frequent server requests.

  2. An overall slow page rendering.

  3. Full page reloads.

  4. Non-rich site interactions.

Use Both CSR & SSR

  • React.renderToString

  • React.renderToStaticMarkup

Not Real SSR

  • if you do not want to pre load data

  • Or you should use koa2-router to replace react-router in client-side, and you need to query data every single route needs

Using Apollo SSR

solve routing and queries

Using Apollo SSR

solve routing and queries

React Component Tree

class UserImage extends React.Component {
  render() {
    return (
      <div>
        <img src={this.props.userImageUrl} />
      </div>
    )
  }
}

export default graphql(
  gql`
    query UserImage($userId: UserId) {
      user(userId: $userId) {
        userImageUrl
      }
    }
  `, {
    options: ({ userId }) => ({ variables: { userId }})
  }
)(UserImage)

React Apollo Crawling

Text

define visitChildren (instance):
    children = result of calling the `render` function
        For each child in children:
            visit(child's Component with the expected props and 
            context)
  
define visit (Component):
    instance = result of running the component's constructor and 
               componentWillMount
    if instance defines a data requirement function:
        Initiate resolving that data requirement
        When it resolves:
          visitChildren(instance)
    else
        visitChildren(instance)

React Apollo Crawling

Text

Demo

Made with Slides.com