cerebral-legacy / cerebral-module-router

An opinionated URL change handler for Cerebral
http://cerebral-website.herokuapp.com/documentation/cerebral-module-router
19 stars 4 forks source link

[wip] enable work in server side #49

Closed Guria closed 8 years ago

Guria commented 9 years ago

POC, not to merge

Guria commented 9 years ago

@christianalfoni what do you say?

Guria commented 9 years ago

rebased to latest master

Guria commented 9 years ago

@christianalfoni https://github.com/christianalfoni/cerebral-router/pull/49#discussion_r44003889

Ah, you thinking about preparing for server... you know, I am actually not sure that the router has any purpose on the server. Lets say you want to open a list of todos, you go to: /todos. What you want to happen is this:

app.get('/todos', (req, res) => {
  db.getTodos()
    .then((todos) => {
      const state = Object.assign({}, initialState, {todos: todos});
      const controller = CerebralDummyController(state);
      const appHtml = renderToString(<Container controller={controller}><App/></Container>);
      res.render('index', {app: appHtml, state: state});
    })
});

So you do not care about the client router, you just care about the request to the server. That request should prepare some data that should be merged into the initial state of the application. Then you create a dummy controller to expose that state to the app when rendered. Then you have to inject the app and actually also the state into the html file. That way the client will render the app correctly, but also build up the initial client side state store correctly with the state from the server.

I think firing off signals etc. on the server will just be confusing. Just think about the components and inject the state they need.

I am sure it still could be useful. It will allow not to duplicate routing logic. Preparing inittialState could be achieved with universal actions and we just export state store. Not sure how it would look like, thought. So I'll just leave it here. Maybe sometimes someone will figure out hot it should be.

Guria commented 8 years ago

@christianalfoni it proved to be usefull for running signal test in node environment. So I have updated it and merged