Skip to content

Commit ee515de

Browse files
authored
Features/koa 2 (#31)
* Tmp work for koa v2 * Working koa v2 version * Update extracttextplugin, and store * Update store * Update fetchData
1 parent 54a0bed commit ee515de

File tree

17 files changed

+170
-163
lines changed

17 files changed

+170
-163
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"rules": {
1111
"max-len": 0,
1212
"func-names": 0,
13+
"no-param-reassign": 0,
1314
"mocha/no-exclusive-tests": 2,
1415
"global-require": 0,
1516
"no-underscore-dangle": 0

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ In `server.js`, I initialse all middlewares from `config/middleware/index`, then
2929
from client side eventually will request to `/api/*`, which are created by `app/server/apis`. Rendering tasks will be delegated to
3030
[React-Router](https://github.com/rackt/react-router) to do server rendering for React.
3131

32+
## Requirement
33+
34+
Install [redux-devtools-extension](https://github.com/zalmoxisus/redux-devtools-extension) to have better experience when developing.
35+
3236
### Require assets in server
3337

3438
Leverage the power of [webpack-isomorphic-tools](https://github.com/halt-hammerzeit/webpack-isomorphic-tools) to hack `require` module with

app/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function render(store, routes, appDOM) {
1212
ReactDOM.render(<App store={store} routes={routes} />, appDOM);
1313
}
1414

15-
if (process.env.NODE_ENV !== 'production') {
15+
if (process.env.NODE_ENV === 'development') {
1616
const { whyDidYouUpdate } = require('why-did-you-update');
1717

1818
whyDidYouUpdate(React, {

app/client/components/main/app.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import React from 'react';
22
import { Provider } from 'react-redux';
3-
import Debug from './debug';
43

54
const App = ({ store, routes }) =>
65
<Provider key="provider" store={store}>
76
<div>
87
{routes}
9-
<Debug />
108
</div>
119
</Provider>;
1210

app/client/components/main/debug.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/client/helpers/fetch-data.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ export function clientFetchData(routes, store) {
2323
} else if (redirectLocation) {
2424
navigateTo(redirectLocation.pathname + redirectLocation.search);
2525
} else if (renderProps) {
26-
trigger('fetchData', renderProps.components, getLocals(store, renderProps));
26+
if (window.__data) {
27+
// Delete initial data so that subsequent data fetches can occur:
28+
delete window.__data;
29+
} else {
30+
// Fetch mandatory data dependencies for 2nd route change onwards:
31+
trigger('fetchData', renderProps.components, getLocals(store, renderProps));
32+
}
2733
} else {
2834
navigateTo('/404.html');
2935
}

app/client/main-store.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let middlewares = [
77
];
88
let enhancers = [];
99

10+
// support for development
1011
if (process.env.NODE_ENV === 'development' && !process.env.SERVER_RENDERING) {
1112
const logger = require('redux-logger')({ level: 'info' });
1213
const { persistState } = require('redux-devtools');
@@ -15,20 +16,26 @@ if (process.env.NODE_ENV === 'development' && !process.env.SERVER_RENDERING) {
1516
...middlewares,
1617
logger,
1718
];
19+
1820
enhancers = [
1921
...enhancers,
20-
require('./components/main/debug').default.instrument(),
2122
persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)),
2223
];
2324
}
2425

25-
const finalCreateStore = compose(
26-
applyMiddleware(...middlewares),
27-
...enhancers
28-
)(createStore);
26+
// support redux-devtools
27+
if (process.env.RUNTIME_ENV === 'client' && window.devToolsExtension) {
28+
enhancers = [
29+
...enhancers,
30+
window.devToolsExtension(),
31+
];
32+
}
2933

3034
export default function (initialState = {}) {
31-
const store = finalCreateStore(reducers, initialState);
35+
const store = createStore(reducers, initialState, compose(
36+
applyMiddleware(...middlewares),
37+
...enhancers
38+
));
3239

3340
if (module.hot) {
3441
module.hot.accept('./main-reducer', () =>

app/server-app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import koa from 'koa';
1+
import Koa from 'koa';
22
import debug from 'debug';
33
import * as config from 'server/middlewares';
44
import apis from 'server/apis/base';
55
import controllers from 'server/controllers/base';
66

7-
const app = koa();
7+
const app = new Koa();
88

99
// setup middlewares
1010
config.loggingLayer(app);

app/server/apis/v1/todo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function (router) {
2-
router.get('/api/v1/todos', function *() {
3-
this.body = [
2+
router.get('/api/v1/todos', (ctx) => {
3+
ctx.body = [
44
{
55
text: 'Todo 1',
66
complete: false,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default function (router) {
2-
router.get('*', function* () {
3-
this.body = yield this.prerender('application/index.marko');
2+
router.get('*', async (ctx) => {
3+
ctx.body = await ctx.prerender('application/index.marko');
44
});
55
}

0 commit comments

Comments
 (0)