Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server-side rendering / client-side rendering out of sync #59

Closed
svdoever opened this issue Jun 15, 2016 · 3 comments
Closed

server-side rendering / client-side rendering out of sync #59

svdoever opened this issue Jun 15, 2016 · 3 comments

Comments

@svdoever
Copy link

I'm getting the following error:

warning.js:44 Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
 (client) om MyComponent</h1><ons-button data-reac
 (server) om MyComponent</h1></div></section>

It is clear that what is rendered at server side is not equal to what was rendered at client side.

What I did was including an existing ReactJS component library called react-onsenui (see https://onsen.io/v2/react.html).

I added to both the client.tsx file and the server.tsx file the following lines:

require('onsenui/css/onsenui.css');
require('onsenui/css/onsen-css-components.css');
require('onsenui');

and in the Home.tsx container I did:

import * as React from 'react';
const s = require('./style.css');

const Button = require('react-onsenui').Button;

class Home extends React.Component<any, any> {
  constructor(props) {
    super(props);
    this.handleButtonClick = this.handleButtonClick.bind(this);
  }

  handleButtonClick() {
    console.log("CLICKED");
  }

  render() {
    return (
      <div className={s.home}>
        <img src={require('./barbar.png')} />
        <p>Hello world!</p>
        <Button onClick={this.handleButtonClick}>MY BUTTON</Button>
      </div>
    );
  }
}
export { Home }

What do I need to do to keep client and server rendering in sync?

@altaywtf
Copy link
Contributor

altaywtf commented Jun 16, 2016

Hi @svdoever ,

If you've added onseen-ui during the development server's running process and just reloaded the page, its normal because the part that you've added did not exist on the initial render which is being done on the server side. However, styles should appear properly.

Do you see these warnings after you restart the app? (or when you build the app for production).
If not, it's okay.

Quick note: We suggest importing the core parts of external ui-libraries like onseen-ui as a seperate vendor file instead of requiring them both on the client and server's entry files. To achieve this, you can create a vendor folder in the src directory and main.ts file inside it. Then, add that vendor/main.ts file into the webpack configuration (dev and prod) as an entry point. Actually, we've already prepared this file here: https://github.com/barbar/vortigern/blob/master/src/vendor/main.ts and set the related webpack configs.

@altaywtf
Copy link
Contributor

@svdoever any updates on this?

@svdoever
Copy link
Author

excuse me, thought I already replied... yes is solved!! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants