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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With