markdalgleish / redial

Universal data fetching and route lifecycle management for React etc.
1.1k stars 42 forks source link

HTTP Status Codes (server rendering) #17

Closed indeyets closed 8 years ago

indeyets commented 8 years ago

Is there any way to trigger specific status-code during server-side rendering component? I have this implemented in my own hackish solution, but overall redial looks nicer so I think about switching.

The use-case is: page depends on 3 data-requests to server. result of first request defines if I should do another 2 requests or shortcut as 404 Not Found

markdalgleish commented 8 years ago

Redial is fairly low level, it only executes your hooks and hands you the async return values. It's up to you to wire these return values up to the response from your server.

Here's a small snippet from a project of mine that handles 404 responses. I've omitted code that is irrelevant to the concept we're discussing, so let me know if this is unclear. Please note that I'm not trying to say that this is the way to solve it in your case, or even that this is the best solution for my use case, just showing how to add your own semantics on top of redial.

return trigger('fetch', routeHandlers, locals)
  .then(() => resolve({
    // Return a 404 if any of the route handlers are my custom PageNotFound component
    statusCode: routeHandlers.some(routeHandler => routeHandler === PageNotFound) ? 404 : 200,
    html: render()
  }))
  .catch(...);

Closing this issue for now. Please re-open if you're still having issues.