From 7602e877c4e9fde97fafe5e5089586f3a2af3e11 Mon Sep 17 00:00:00 2001 From: Lida Wang Date: Wed, 28 Dec 2016 14:51:19 -0800 Subject: [PATCH 1/5] Make test page to test passing down of data from forwarding page to forwarded pages Also ensure that bundle-splitting works for forwarded-to pages --- .../react-server-test-pages/entrypoints.js | 4 ++ .../pages/navigation/forward.js | 50 +++++++++++++++++++ .../pages/navigation/forwardEven.js | 23 +++++++++ .../pages/navigation/forwardOdd.js | 23 +++++++++ 4 files changed, 100 insertions(+) create mode 100644 packages/react-server-test-pages/pages/navigation/forward.js create mode 100644 packages/react-server-test-pages/pages/navigation/forwardEven.js create mode 100644 packages/react-server-test-pages/pages/navigation/forwardOdd.js diff --git a/packages/react-server-test-pages/entrypoints.js b/packages/react-server-test-pages/entrypoints.js index 15c6236c7..3b5092877 100644 --- a/packages/react-server-test-pages/entrypoints.js +++ b/packages/react-server-test-pages/entrypoints.js @@ -29,6 +29,10 @@ module.exports = { entry: "/navigation/playground", description: "Navigation playground", }, + Forwarding: { + entry: "/navigation/forward", + description: "Test how page forwarding works with passing data down", + }, StylePromises: { entry: "/styles/promises", description: "Stylesheets returned from promises", diff --git a/packages/react-server-test-pages/pages/navigation/forward.js b/packages/react-server-test-pages/pages/navigation/forward.js new file mode 100644 index 000000000..3785cd35c --- /dev/null +++ b/packages/react-server-test-pages/pages/navigation/forward.js @@ -0,0 +1,50 @@ +import {ReactServerAgent} from "react-server"; + +export default class ForwardPage { + handleRoute() { + const request = this.getRequest(); + let params = request.getQuery(); + if (params) { + params = params.value; + } else { + params = 0; + } + + //fetch some data + this.data = ReactServerAgent.get('/data/delay?ms=1000&val='+params); + + return this.data.then((res) => { + //then depending on said data, forward to one of two pages, and pass along the data we pre-fetched + if (res.body % 2 === 0) { + if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` + require.ensure(["./forwardEven"], () => { + return { + page: require("./forwardEven").default, + }; + }); + } else { + return { + page: require("./forwardEven").default, + }; + } + } else { + if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` + require.ensure(["./forwardOdd"], () => { + return { + page: require("./forwardOdd").default, + }; + }); + } else { + return { + page: require("./forwardOdd").default, + }; + } + } + }); + } + + getElements() { + return
boop
; + } +} + diff --git a/packages/react-server-test-pages/pages/navigation/forwardEven.js b/packages/react-server-test-pages/pages/navigation/forwardEven.js new file mode 100644 index 000000000..c98f6ace7 --- /dev/null +++ b/packages/react-server-test-pages/pages/navigation/forwardEven.js @@ -0,0 +1,23 @@ +import {ReactServerAgent, RootElement} from "react-server"; + +const Even = ({body}) =>
Hey look I'm even because I am {body}
; + +export default class ForwardEvenPage { + handleRoute() { + const request = this.getRequest(); + let params = request.getQuery(); + if (params) params = params.value; + else params = 0; + + //fetch some data (should be from cache) + this.data = ReactServerAgent.get('/data/delay?ms=1000&val='+params); + + return {code: 200}; + } + + getElements() { + return [ + , + ]; + } +} diff --git a/packages/react-server-test-pages/pages/navigation/forwardOdd.js b/packages/react-server-test-pages/pages/navigation/forwardOdd.js new file mode 100644 index 000000000..aad497e19 --- /dev/null +++ b/packages/react-server-test-pages/pages/navigation/forwardOdd.js @@ -0,0 +1,23 @@ +import {ReactServerAgent, RootElement} from "react-server"; + +const Odd = ({body}) =>
Hey look I'm odd because I am {body}
; + +export default class ForwardOddPage { + handleRoute() { + const request = this.getRequest(); + let params = request.getQuery(); + if (params) params = params.value; + else params = 0; + + //fetch some data (should be from cache) + this.data = ReactServerAgent.get('/data/delay?ms=1000&val='+params); + + return {code: 200}; + } + + getElements() { + return [ + , + ]; + } +} From 74c4e77069100e77cb66a9980849effd2881392f Mon Sep 17 00:00:00 2001 From: Lida Wang Date: Fri, 6 Jan 2017 16:40:59 -0800 Subject: [PATCH 2/5] Fix build breakage --- packages/react-server-test-pages/pages/navigation/forward.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-server-test-pages/pages/navigation/forward.js b/packages/react-server-test-pages/pages/navigation/forward.js index 3785cd35c..9a27f4f3c 100644 --- a/packages/react-server-test-pages/pages/navigation/forward.js +++ b/packages/react-server-test-pages/pages/navigation/forward.js @@ -17,7 +17,7 @@ export default class ForwardPage { //then depending on said data, forward to one of two pages, and pass along the data we pre-fetched if (res.body % 2 === 0) { if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` - require.ensure(["./forwardEven"], () => { + return require.ensure(["./forwardEven"], () => { return { page: require("./forwardEven").default, }; @@ -29,7 +29,7 @@ export default class ForwardPage { } } else { if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` - require.ensure(["./forwardOdd"], () => { + return require.ensure(["./forwardOdd"], () => { return { page: require("./forwardOdd").default, }; From 3799dbc471d41ec69b21586c185556073143dcb2 Mon Sep 17 00:00:00 2001 From: PC Drew Date: Sun, 15 Jan 2017 08:31:12 -0700 Subject: [PATCH 3/5] Refactor code in forward.js Committing changes after review. --- .../pages/navigation/forward.js | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/packages/react-server-test-pages/pages/navigation/forward.js b/packages/react-server-test-pages/pages/navigation/forward.js index 9a27f4f3c..32161bc3f 100644 --- a/packages/react-server-test-pages/pages/navigation/forward.js +++ b/packages/react-server-test-pages/pages/navigation/forward.js @@ -15,31 +15,18 @@ export default class ForwardPage { return this.data.then((res) => { //then depending on said data, forward to one of two pages, and pass along the data we pre-fetched - if (res.body % 2 === 0) { - if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` - return require.ensure(["./forwardEven"], () => { - return { - page: require("./forwardEven").default, - }; - }); - } else { - return { - page: require("./forwardEven").default, - }; - } - } else { - if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` - return require.ensure(["./forwardOdd"], () => { - return { - page: require("./forwardOdd").default, - }; - }); - } else { - return { - page: require("./forwardOdd").default, - }; - } - } + const pageName = (res.body % 2 === 0) ? "./forwardEven" : "./forwardOdd"; + if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` + return require.ensure([pageName], () => { + return { + page: require(pageName).default, + }; + }); + } else { + return { + page: require(pageName).default, + }; + } }); } From 5cc9b71fbb1b3f2959e7f8acef6dc38c6024aa13 Mon Sep 17 00:00:00 2001 From: PC Drew Date: Sun, 15 Jan 2017 08:32:34 -0700 Subject: [PATCH 4/5] Convert comment to TODO --- packages/react-server-test-pages/pages/navigation/forward.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/react-server-test-pages/pages/navigation/forward.js b/packages/react-server-test-pages/pages/navigation/forward.js index 32161bc3f..e488229a9 100644 --- a/packages/react-server-test-pages/pages/navigation/forward.js +++ b/packages/react-server-test-pages/pages/navigation/forward.js @@ -16,7 +16,8 @@ export default class ForwardPage { return this.data.then((res) => { //then depending on said data, forward to one of two pages, and pass along the data we pre-fetched const pageName = (res.body % 2 === 0) ? "./forwardEven" : "./forwardOdd"; - if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer` + // TODO: change this to use isBrowser when that check is available. + if (typeof window !== 'undefined') { return require.ensure([pageName], () => { return { page: require(pageName).default, From db40e27627095cdc01b6b3dd88b67a113f4fa60d Mon Sep 17 00:00:00 2001 From: PC Drew Date: Sun, 15 Jan 2017 08:35:30 -0700 Subject: [PATCH 5/5] Fix whitespace issues. --- .../pages/navigation/forward.js | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/react-server-test-pages/pages/navigation/forward.js b/packages/react-server-test-pages/pages/navigation/forward.js index e488229a9..a839f867c 100644 --- a/packages/react-server-test-pages/pages/navigation/forward.js +++ b/packages/react-server-test-pages/pages/navigation/forward.js @@ -17,17 +17,17 @@ export default class ForwardPage { //then depending on said data, forward to one of two pages, and pass along the data we pre-fetched const pageName = (res.body % 2 === 0) ? "./forwardEven" : "./forwardOdd"; // TODO: change this to use isBrowser when that check is available. - if (typeof window !== 'undefined') { - return require.ensure([pageName], () => { - return { - page: require(pageName).default, - }; - }); - } else { - return { - page: require(pageName).default, - }; - } + if (typeof window !== 'undefined') { + return require.ensure([pageName], () => { + return { + page: require(pageName).default, + }; + }); + } else { + return { + page: require(pageName).default, + }; + } }); }