Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server Side Render re-render at client

I trying to implement server side render with django, express, react and react-router-dom.

server.js

const html = renderToString((
    <Provider store={store}>
      <StaticRouter basename={baseUrl} location={location} context={context}>
        <AppRoute />
      </StaticRouter>
    </Provider>
  ), null, 2);

  const preloadedState = store.getState();

  res.send(renderFullPage(html, preloadedState))

index.js for client.js

ReactDOM.render(
  <Provider store={store}>
    <Router>
      <AppRoute />
    </Router>
  </Provider>,
  document.getElementById('app')
);

AppRoute uses RouteAsync for client and RouteSync for server

AppRoute.js import

import {App} from './RouteSync'
import {CollegeList} from './RouteSync'
import {CollegeDetail} from './RouteSync'

RouteAsync.js

export const App = asyncRoute(() => System.import('../app'));
export const CollegeList = asyncRoute(() => System.import('../apps/college/CollegeList'));
export const CollegeDetail = asyncRoute(() => System.import('../apps/college/CollegeDetail'));

RouteSync.js

export { default as App } from '../app'
export { default as CollegeList } from '../apps/college/CollegeList'
export { default as CollegeDetail } from '../apps/college/CollegeDetail'

webpack NormalModuleReplacementPlugin changes RouteSync to RouteAsync

new webpack.NormalModuleReplacementPlugin(
      /\.\/RouteSync$/,
      './RouteAsync'
    ),

Server render is done and send to client. At client after SSR client again re-render the whole page when viewed from devtools performance.

devtools "performance" image showing blank page before client re-render

I hope react would only hook event listeners, not render the page.

Any suggestion to stop re-render in client side.

like image 428
Roshan Shrestha Avatar asked Jul 30 '17 08:07

Roshan Shrestha


People also ask

Is server-side rendering better than client-side rendering?

Between the two options, server-side rendering is better for SEO than client-side rendering. This is because server-side rendering can speed up page load times, which not only improves the user experience, but can help your site rank better in Google search results.

Is React server-side rendering or client-side?

By default, your React app will be client-side rendered. This means basically, all of the source code will live in JavaScript files referenced in a single HTML file that initializes your app.

Why does server-side rendering React?

Server-side rendering is an excellent option for rendering web pages to increase the initial page load speed, improve SEO and provide a better user experience. The best part about web technology is the availability of platforms and frameworks that make complex concepts easier to implement.

What is server-side rendering with example?

Server-side rendering is when content on your webpage is rendered on the server and not on your browser using JavaScript. For example, with a typical PHP or WordPress site, the page is loaded from content that is coming via HTTP, which was rendered on the server and comes as fully rendered HTML.


1 Answers

I also did not find answer for this. And created my own starter. It uses react-router v4, redux, redux-saga. To prevent re-render on the client for the first link open there used hydrate method. I’ve created this based of official docs and suggestions from react-router v4 and redux. Hope it helps.

https://github.com/gzoreslav/react-redux-saga-universal-application

like image 101
Zoreslav Goral Avatar answered Oct 16 '22 16:10

Zoreslav Goral