From 86b178f3100d54c67991a9af7ec0110e0537db1a Mon Sep 17 00:00:00 2001 From: Levi Botelho Date: Sat, 14 May 2016 16:49:17 +0200 Subject: [PATCH] Add redirecting to async rendering example --- docs/guides/ServerRendering.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/guides/ServerRendering.md b/docs/guides/ServerRendering.md index d623a59e32..192516010c 100644 --- a/docs/guides/ServerRendering.md +++ b/docs/guides/ServerRendering.md @@ -6,7 +6,7 @@ Server rendering is a bit different than in a client because you'll want to: - Send `30x` responses for redirects - Fetch data before rendering (and use the router to help you do it) -To facilitate these needs, you drop one level lower than the [``](/docs/API.md#Router) API with: +To facilitate these needs, you drop one level lower than the [``](/docs/API.md#Router) API with: - [`match`](/docs/API.md#match-routes-location-history-options--cb) to match the routes to a location without rendering - `RouterContext` for synchronous rendering of route components @@ -53,9 +53,25 @@ render(, mountNode) You need to do ```js -match({ history, routes }, (error, redirectLocation, renderProps) => { - render(, mountNode) -}) +function initiateRender(history) { + match({ history, routes }, (error, redirectLocation, renderProps) => { + if (redirectLocation == null) { + render(, mountNode) + } else { + switch (redirectLocation.action) { + case "PUSH": + history.push(redirectLocation); + break; + case "REPLACE": + history.replace(redirectLocation); + break; + } + initiateRender(history); + } + }) +} + +initiateRender(history) ``` ## History Singletons