M
M
Max Sysoev2015-05-15 12:27:57
Node.js
Max Sysoev, 2015-05-15 12:27:57

Do I need to include the router in the isomorphic part?

We are talking about the react-router router , but this is not particularly important. I tried to include client-side routes in the server-side and no - both options. And now I can not decide which of these is better. Interested in the choice for an isomorphic application, say, an online store.
Including routes in the back-end seems to save time, but in fact it turns out to create more problems than it solves. And if you do not include them, then you have to clone client routes on the server.
We are talking about routes with data (such as a product page, a catalog - with a query to the database), and static pages (no database queries at all).
I will help the answerers a little and describe both approaches in more detail.
If we include routes, then:
The isomorphic part of React becomes middleware.
+

  • DRY for client routes - no need to describe them in the server router
-
  • There is a headache with getting data before rendering (FB came up with Relay as a solution)
  • Difficulties with the definition of exceptions, for example, 404. At the same time, and double rendering of the application, if you hit the route 404

If we don't include routes, then:
The isomorphic part of React is very similar to regular templates like Jade.
+
  • No problem getting data
  • Static pages are displayed through a route duplicated on the server
-
  • Describe routes twice (repeat yourself)

The whole problem is precisely in duplication ... Maybe the devil is not so terrible as he is painted?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Kureev, 2015-05-21
@ColCh

I'm sorry, but I don't understand what problems you are talking about with getting data in the first option. It doesn't look so scary: every React class can have static methods, in which you can make function calls that will fetch your data. You can see how I did it in my code .
And about 404.

T
Timur Shemsedinov, 2015-05-15
@MarcusAurelius

Duplication is hard to come to terms with morally. But there are three ways, two you have described, to duplicate and isomorphize. But I prefer the third one, when the server part contains everything in itself, a generalized problem to be solved, and dynamically generates the client part. That is, you do not need to write 2 times, but using metaprogramming to climb to such an abstraction layer where there is no client and server and solve the problem there, and then translate it into the client and server, into templates and routes into specifics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question