Coder Social home page Coder Social logo

ssr-boilerplate's Introduction

ssr-boilerplate

Simple SSR boilerplate that just works.

Usage

npm run dev
npm start

Features

  • Server side rendering
  • Hot module replacement
  • React Router
  • React Helmet
  • Dynamic component with @loadable/component

Define your routes

import Index from './components/index'
import List from './components/list'
import {getText} from './api'

const routes = [
  {
    path: '/',
    exact: true,
    component: Index,
  },
  {
    path: '/list',
    component: List,
    getInitialProps: async (context) => {
      context.text = await getText();
    }
  }
]

export default routes;

One addition is the getInitialProps, it's the async function that will run on server to inject remote data for your component.

Define your component.

Note: All React components must reside inside components folder.

import React from "react";
import Nav from './nav'
import {getText} from '../api'
export default class List extends React.Component {
  async componentDidMount() {
    const {text} = this.props
    if (!text) {
      const tmp = await getText()
      this.setState({
        text: tmp
      })
    }
  }

  constructor(props) {
    super(props)
    this.state = {
      text: props.text
    }
  }

  render() {
    const {text} = this.state
    return (
      <div>
        <Nav />
        {text && text.login}
      </div>
    )
  }
}

nav.js

import React from 'react'
import { Link } from 'react-router-dom'

const Nav = () => {
  return (
    <ul>
      <li><Link to="/">Home</Link></li>
      <li><Link to="/list">List</Link></li>
    </ul>
  )
}

export default Nav

ssr-boilerplate's People

Contributors

revskill10 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.