-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #806 from lidawang/lw-forward-test
Test page for page forwarding
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/react-server-test-pages/pages/navigation/forward.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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 | ||
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, | ||
}; | ||
} | ||
}); | ||
} | ||
|
||
getElements() { | ||
return <div>boop</div>; | ||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
packages/react-server-test-pages/pages/navigation/forwardEven.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
packages/react-server-test-pages/pages/navigation/forwardOdd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
]; | ||
} | ||
} |