Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test page for page forwarding #806

Merged
merged 5 commits into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/react-server-test-pages/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 50 additions & 0 deletions packages/react-server-test-pages/pages/navigation/forward.js
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor this section a bit to limit code reuse? Something like this should work:

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,
	};
}

if (typeof window !== 'undefined') { //would be nice if this is `process.env.isServer`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change this comment to be a // TODO:, we can track it better. Eventually when we get server side Webpack up and running, it'll include a fix for this.

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,
};
}
}
});
}

getElements() {
return <div>boop</div>;
}
}

23 changes: 23 additions & 0 deletions packages/react-server-test-pages/pages/navigation/forwardEven.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {ReactServerAgent, RootElement} from "react-server";

const Even = ({body}) => <div className="even">Hey look I'm even because I am {body}</div>;

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 [
<RootElement when={this.data}><Even /></RootElement>,
];
}
}
23 changes: 23 additions & 0 deletions packages/react-server-test-pages/pages/navigation/forwardOdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {ReactServerAgent, RootElement} from "react-server";

const Odd = ({body}) => <div className="odd">Hey look I'm odd because I am {body}</div>;

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 [
<RootElement when={this.data}><Odd /></RootElement>,
];
}
}