1

I want to make a very basic reactjs based application with server-side rendering to make sure the first load is quick and also to make sure all crawlers can access my content.

For this, I first followed the official reactjs docs and then looked for a basic routing option for my need. I ended up using React Router. Now, I want to enable server-side rendering for it without having to completely change this to use Redux or something. What would be the most basic/simplest way to do this.

The code in its present condition is as below:

import React from 'react'
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import './index.css';


//Product Image Component
class ProductImage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: 'https://rukminim1.flixcart.com/image/832/832/j8bxvgw0-1/mobile/g/j/z/mi-mi-mix-2-na-original-imaeydgnjzmvxwfz.jpeg?q=70',
      alt: 'the product image'

    };


  }

  render() {
    return (
      <div className="ProductImageContainer">        
          <img className="ProductImage"
      src={this.state.value}
      alt={this.state.alt}
      />

      </div>
    );
  }
}


//Single Product Component
class ProductSingle extends React.Component {

  render() {
    return (
      <div className="single">
      <ProductImage />

      </div>
    );
  }
}


//Homepage
class Home extends React.Component {
  render() {
    return (
      <div>
         <h2>Home</h2>
         <p>The content is here.</p>
       </div>
    );
  }
}


//About Page
class About extends React.Component {
  render() {
    return (
      <div>
         <h2>About</h2>
         <p>The content is here.</p>
       </div>
    );
  }
}

//Topic component
class Topic extends React.Component {
  render() {
    const {match} = this.props;

    return (
      <div>
        <h3>{match.params.topicId}</h3>
      </div>
    );
  }
}

//Topics component
class Topics extends React.Component {
  render() {
    const {match} = this.props;

    return (

      <div>
       <h2>Topics</h2>
       <ul>
         <li>
           <Link to={`${match.url}/rendering`}>
             Rendering with React
           </Link>
         </li>
         <li>
           <Link to={`${match.url}/components`}>
             Components
           </Link>
         </li>
         <li>
           <Link to={`${match.url}/props-v-state`}>
             Props v. State
           </Link>
         </li>
       </ul>

       <Route path={`${match.url}/:topicId`} component={Topic}/>
       <Route exact path={match.url} render={() => (
        <h3>Please select a topic.</h3>
      )}/>
     </div>

    );
  }
}


//Main App component
class App extends React.Component {
  render() {
    return (
      <Router>
          <div>
            <ul className="menu">
              <li><Link to="/">Home</Link></li>
              <li><Link to="/about">About</Link></li>
              <li><Link to="/topics">Topics</Link></li>        
            </ul>



            <Route exact path="/" component={Home}/>
            <Route path="/about" component={ProductSingle}/>
            <Route path="/topics" component={Topics}/>
          </div>
        </Router>
    );
  }
}


ReactDOM.render(
  <App />,
  document.getElementById('root')
);
5
  • What server technology are you using? I personally haven't used it yet, but Next.js looks like a promising solution. Commented Dec 14, 2017 at 14:04
  • When you are on the server, switch StaticRouter for BrowserRouter and that is about it. reacttraining.com/react-router/web/api/StaticRouter Commented Dec 14, 2017 at 14:31
  • @DavinTryon I saw that in the doc. But I didn't understand what that means. As my site is fully static as of now and runs on the browser. No idea how this piece is connected... Commented Dec 14, 2017 at 16:17
  • If you are using routing to determine what is rendered (on the server), you need some sort of router that looks at the request url in order to correctly render on the server. Commented Dec 14, 2017 at 16:55
  • Read docs from react-router server-side rendering github.com/ReactTraining/react-router/blob/master/packages/… Commented Dec 14, 2017 at 21:11

1 Answer 1

1

https://github.com/gzoreslav/react-redux-saga-universal-application/blob/master/README.md please check my repo. I used redux and redux-saga, but I guess you may replace them with another flux that you need

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.