question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way of using fetch on the server for isomorphic rendering?

See original GitHub issue

Steps to reproduce

Require isomorphic-fetch or node-fetch on server_rendering.js

Expected behavior

Fetch should work, and it should finish the request (and callback) before generating the html for the browser.

Actual behavior

I’m requiring node-fetch with const fetch = require('node-fetch');. Using it results in

<ExecJS::ProgramError: ReferenceError: fetch is not defined>.

If, instead, I do const fetch = require('isomorphic-fetch'); I get

Unhandled promise rejection (rejection id: 2): ReferenceError: XMLHttpRequest is not defined.

System configuration

Sprockets or Webpacker version: 3.0.2 React-Rails version: 2.4.2 Rect_UJS version: 2.4.2 Rails version: 5.1.4 Ruby version: 2.4.1


How can I solve this? I want a product list in an app to be filled with data from an API, this currently works on client-rendering, doing a fetch call in componentWillMount and calling this.setState with the response (while showing a loading screen meanwhile).

But for server-side rendering I need the data to be inserted when the server compiles it. I don’t know how to make it work. Maybe I could:

  1. Make my products component stateless, with the product list as props.
  2. Implement a custom renderer which does the api call and load the response as props in an App component.
  3. Make a wrapper component for the product list which only tries to fetch the data if the props from the app are empty.

The problem is I’d still have to do the requests in Ruby, and it seems awfully complex for something I could solve just using fetch on the server. Would the solution above work? Is there any easier solution?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
catmandocommented, Dec 3, 2017

Have a look at http://ruby-hyperloop.org (the hyper-react gem). Look at isomorphic helpers module for how to get this to work…

1reaction
programrailscommented, Aug 9, 2018

catmando This one? https://github.com/ruby-hyperloop/hyper-react/blob/master/lib/reactive-ruby/isomorphic_helpers.rb

I haven’t seen anything applicable there. JS server-side fetch is really necessary - to optimize the Rails data loading on SSR. Currently I have to pre-load everything via props - whereas I would prefer to load it dynamically in portions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Side API Calls with Isomorphic Fetch - React - YouTube
See a professional front-end developer at work. Unscripted. Mistakes included.I am going to make API calls on the server side using an ......
Read more >
How can I fetch data in server before render in react ...
Answering this in 2019 because seems like there is no clear answer yet. If your backend services are using GraphQL, you can actually...
Read more >
Isomorphic Implementation of React | by Yudhajit Adhikary
React Router renders components based on path,it integrates Redux using Connected Router,it also uses history module to inject custom path and works in...
Read more >
Client- and Server-Side Data Fetching in React
Render-as-You-Fetch: we start fetching as early as possible and then start rendering immediately, without waiting for the data to be ready. In a ......
Read more >
Isomorphic Rendering vs. Dynamic Rendering for JavaScript
This library is used for fetching data and creating an initial state. It uses React to send the application on the server. It...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found