react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

How to using it with react-loadable SSR preloadAll?

Open terfender opened this issue 6 years ago • 5 comments

I want to use this lib with react-loadable with SSR. An example given in react-loadable using Express.

const app = express();

app.get('/', (req, res) => {
  res.send(`
    <!doctype html>
    <html lang="en">
      <head>...</head>
      <body>
        <div id="app">${ReactDOMServer.renderToString(<App/>)}</div>
        <script src="/dist/main.js"></script>
      </body>
    </html>
  `);
});


Loadable.preloadAll().then(() => {
  app.listen(3000, () => {
    console.log('Running on http://localhost:3000/');
  });
})

where Loadable.preloadAll() preloads all the lazyloads component then serves JS file.

I can load my app normally on client side. But with prerender: true, I get the follow error.

React::ServerRendering::PrerenderError - Encountered error "#<ExecJS::ProgramError: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.>" when prerendering App with {___PROPS___}
invariant ((execjs):118729:19)
ReactDOMServerRenderer.render ((execjs):122170:11)
ReactDOMServerRenderer.read ((execjs):121879:33)
Object.renderToString ((execjs):122390:31)
Object.serverRender ((execjs):169169:42)
(execjs):6:45
(execjs):18:13

How can I go achieve it in Rails?

terfender avatar Feb 08 '19 19:02 terfender

Hello, I'm interested with this too, been trying different things for several hours already.

I'm getting window is not define upon importing react-loadable however.

dachinat avatar Feb 12 '19 00:02 dachinat

Check out loadable-components.

While React on Rails was advocating react-loadable 6 months ago, we no longer are.

react-loadable may not be maintained.

My team at ShakaCode is working on an example of this with React on Rails.

justin808 avatar Jan 23 '20 08:01 justin808

@justin808 any news? I would like to see an example of SSR with loadable-components on rails

ops1ops avatar Apr 20 '21 10:04 ops1ops

@ops1ops if you Sign up for https://www.shakacode.com/react-on-rails-pro/ , you'll be up super quickly. Feel free to jump on our Slack and ask for this. Fairly easy.

justin808 avatar Jun 02 '21 07:06 justin808

@justin808 nvm for now, just learned how SSR works and it became easy to implement it on ruby

ops1ops avatar Jun 05 '21 12:06 ops1ops

If this issue is still relevant, please comment.

ahangarha avatar Jan 05 '23 14:01 ahangarha