From 892f237a90da7e780b6c42319c9bd887a68f328d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B8vik?= Date: Thu, 1 Dec 2016 18:06:22 +0100 Subject: [PATCH 1/5] Support redirects on the server side triggered by history modifications --- .../src/app/server/reactRenderer.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js index 3ac3362..bde7a6e 100755 --- a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js +++ b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js @@ -82,6 +82,12 @@ export function reactRender({ reduxSagas, }) { return new Promise((resolve) => { + let currentLocation = {}; + + history.listen((location) => { + currentLocation = location; + }); + match({ history, routes: createRoutes(store), location: url }, (error, redirect, renderProps) => { if (redirect) { @@ -126,6 +132,15 @@ export function reactRender({ } return result; }).then(({ redialMap, redialProps }) => { + const currentUrl = currentLocation.pathname + currentLocation.search; + + if (currentUrl !== url) { + log(`Redirect request to ${currentUrl}`); + return resolve({ + redirect: currentUrl, + }); + } + let component = applyRouterMiddleware(useRedial({ redialMap }))(renderProps); if (store) { From ec876384592113c25651f973b33cc3a158ec07d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B8vik?= Date: Fri, 2 Dec 2016 11:30:46 +0100 Subject: [PATCH 2/5] Support custom basename on history-triggered redirects --- .../src/app/server/reactRenderer.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js index bde7a6e..8d8637b 100755 --- a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js +++ b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js @@ -132,12 +132,14 @@ export function reactRender({ } return result; }).then(({ redialMap, redialProps }) => { - const currentUrl = currentLocation.pathname + currentLocation.search; + const currentUrl = `${currentLocation.pathname}${currentLocation.search}`; if (currentUrl !== url) { - log(`Redirect request to ${currentUrl}`); + const base = currentLocation.basename ? currentLocation.basename : ''; + + log(`Redirect request to ${base}${currentUrl} due to history location modification`); return resolve({ - redirect: currentUrl, + redirect: `${base}${currentUrl}`, }); } From 667fce773776c61a09e7e776ea498b909699d51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B8vik?= Date: Fri, 2 Dec 2016 17:12:49 +0100 Subject: [PATCH 3/5] Provide history in hook locals --- .../roc-package-web-app-react/app/client/create-client.js | 5 ++++- .../src/app/server/reactRenderer.js | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/extensions/roc-package-web-app-react/app/client/create-client.js b/extensions/roc-package-web-app-react/app/client/create-client.js index 179cb32..fc3b342 100644 --- a/extensions/roc-package-web-app-react/app/client/create-client.js +++ b/extensions/roc-package-web-app-react/app/client/create-client.js @@ -92,7 +92,9 @@ export default function createClient({ createRoutes, createStore, mountNode }) { } let routes; - let locals = {}; + let locals = { + history, + }; const createComponent = [(component) => component]; const createDevComponent = [(component) => component]; @@ -117,6 +119,7 @@ export default function createClient({ createRoutes, createStore, mountNode }) { locals = { dispatch: store.dispatch, getState: store.getState, + history, }; createComponent.push((component) => ( diff --git a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js index 8d8637b..a4a110e 100755 --- a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js +++ b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js @@ -112,7 +112,10 @@ export function reactRender({ const locals = store ? { dispatch: store.dispatch, getState: store.getState, - } : {}; + history, + } : { + history, + }; const hooks = rocConfig.runtime.fetch.server; From 6702d2628f9370dd4cc4bc6b6211ecbeff975cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B8vik?= Date: Fri, 2 Dec 2016 17:20:20 +0100 Subject: [PATCH 4/5] Support custom basename on react-router triggered redirects and improve checks --- .../src/app/server/reactRenderer.js | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js index a4a110e..36439a9 100755 --- a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js +++ b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js @@ -91,9 +91,12 @@ export function reactRender({ match({ history, routes: createRoutes(store), location: url }, (error, redirect, renderProps) => { if (redirect) { - log(`Redirect request to ${redirect.pathname + redirect.search}`); + const base = redirect.basename ? redirect.basename : ''; + const redirectUrl = `${base}${redirect.pathname}${redirect.search}`; + log(`Redirect request to ${redirectUrl} due to React Router`); + return resolve({ - redirect: redirect.pathname + redirect.search, + redirect: redirectUrl, }); } else if (error) { log('Router error', pretty.render(error)); @@ -135,15 +138,18 @@ export function reactRender({ } return result; }).then(({ redialMap, redialProps }) => { - const currentUrl = `${currentLocation.pathname}${currentLocation.search}`; - - if (currentUrl !== url) { - const base = currentLocation.basename ? currentLocation.basename : ''; - - log(`Redirect request to ${base}${currentUrl} due to history location modification`); - return resolve({ - redirect: `${base}${currentUrl}`, - }); + if (Object.keys(currentLocation).length > 0) { + const currentUrl = `${currentLocation.pathname}${currentLocation.search}`; + + if (currentUrl !== url) { + const base = currentLocation.basename ? currentLocation.basename : ''; + const redirectUrl = `${base}${currentUrl}`; + + log(`Redirect request to ${redirectUrl} due to history location modification`); + return resolve({ + redirect: `${redirectUrl}`, + }); + } } let component = applyRouterMiddleware(useRedial({ redialMap }))(renderProps); From 10606d1bd4675780752d072cca313a3e3afddc9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B8vik?= Date: Sat, 3 Dec 2016 19:09:10 +0100 Subject: [PATCH 5/5] Improve readability of currentLocation related logic --- .../src/app/server/reactRenderer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js index 36439a9..de6fad6 100755 --- a/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js +++ b/extensions/roc-package-web-app-react/src/app/server/reactRenderer.js @@ -82,7 +82,7 @@ export function reactRender({ reduxSagas, }) { return new Promise((resolve) => { - let currentLocation = {}; + let currentLocation; history.listen((location) => { currentLocation = location; @@ -91,7 +91,7 @@ export function reactRender({ match({ history, routes: createRoutes(store), location: url }, (error, redirect, renderProps) => { if (redirect) { - const base = redirect.basename ? redirect.basename : ''; + const base = redirect.basename || ''; const redirectUrl = `${base}${redirect.pathname}${redirect.search}`; log(`Redirect request to ${redirectUrl} due to React Router`); @@ -138,11 +138,11 @@ export function reactRender({ } return result; }).then(({ redialMap, redialProps }) => { - if (Object.keys(currentLocation).length > 0) { + if (currentLocation) { const currentUrl = `${currentLocation.pathname}${currentLocation.search}`; if (currentUrl !== url) { - const base = currentLocation.basename ? currentLocation.basename : ''; + const base = currentLocation.basename || ''; const redirectUrl = `${base}${currentUrl}`; log(`Redirect request to ${redirectUrl} due to history location modification`);