Skip to content

Commit d692e5a

Browse files
committed
Merge pull request #13 from ngduc/use-koa-session
feat: use koa-session to keep locale
2 parents 3c6a1de + 6539936 commit d692e5a

File tree

6 files changed

+66
-39
lines changed

6 files changed

+66
-39
lines changed

package.json

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-setup",
33
"description": "A Universal React setup with i18n: Babel 6, Koa 2, React, React Router, React Transmit, React Bootstrap, React-intl, Mocha, Webpack HMR, PostCSS, ESLint.",
4-
"version": "2.2.9",
4+
"version": "2.3.0",
55
"author": "Duc Nguyen (https://github.com/ngduc)",
66
"license": "MIT",
77
"homepage": "https://github.com/ngduc/react-setup",
@@ -46,64 +46,64 @@
4646
"clean": "rm -rf ./dist && rm -rf ./static"
4747
},
4848
"dependencies": {
49+
"autoprefixer": "6.3.3",
4950
"babel": "6.5.2",
5051
"babel-cli": "^6.6.5",
5152
"babel-core": "6.7.2",
5253
"babel-eslint": "^5.0.0",
5354
"babel-loader": "6.2.4",
5455
"babel-plugin-react-intl": "2.1.1",
56+
"babel-polyfill": "6.7.2",
5557
"babel-preset-es2015": "6.6.0",
5658
"babel-preset-react": "6.5.0",
5759
"babel-preset-react-hmre": "1.1.1",
5860
"babel-preset-stage-0": "6.5.0",
59-
"babel-polyfill": "6.7.2",
61+
"bootstrap": "^3.3.6",
62+
"classnames": "^2.2.3",
63+
"concurrently": "2.0.0",
64+
"copy-webpack-plugin": "^1.1.1",
65+
"css-loader": "^0.23.1",
66+
"extract-text-webpack-plugin": "^1.0.1",
6067
"fetch-plus": "3.8.1",
6168
"fetch-plus-bearerauth": "3.5.0",
6269
"fetch-plus-json": "3.6.0",
70+
"forever": "0.15.1",
71+
"glob": "^7.0.3",
72+
"html-webpack-plugin": "^2.10.0",
6373
"intl": "^1.1.0",
6474
"intl-locales-supported": "^1.0.0",
75+
"intl-messageformat-parser": "^1.2.0",
6576
"isomorphic-fetch": "2.2.1",
77+
"json-loader": "0.5.4",
78+
"just-wait": "1.0.5",
6679
"koa": "^2.0.0-alpha.3",
6780
"koa-bodyparser": "^3.0",
6881
"koa-compose": "^3.0.0",
6982
"koa-compress": "^2.0.0",
7083
"koa-convert": "^1.2",
71-
"koa-generic-session": "^1.10",
7284
"koa-passport": "^2.0",
7385
"koa-router": "7.0.1",
7486
"koa-serve-static": "^1.0.0",
75-
"mustache": "^2.2.1",
76-
"react": "0.14.7",
77-
"react-bootstrap": "^0.28.3",
78-
"react-dom": "0.14.7",
79-
"react-inline-css": "2.1.0",
80-
"react-intl": "2.0.0-rc-1",
81-
"react-router": "2.0.1",
82-
"react-transmit": "https://github.com/ngduc/react-transmit.git",
83-
"serialize-javascript": "^1.2.0",
84-
"forever": "0.15.1",
85-
"concurrently": "2.0.0",
86-
"autoprefixer": "6.3.3",
87-
"bootstrap": "^3.3.6",
88-
"classnames": "^2.2.3",
89-
"copy-webpack-plugin": "^1.1.1",
90-
"css-loader": "^0.23.1",
91-
"extract-text-webpack-plugin": "^1.0.1",
92-
"glob": "^7.0.3",
93-
"html-webpack-plugin": "^2.10.0",
94-
"intl-messageformat-parser": "^1.2.0",
95-
"json-loader": "0.5.4",
96-
"just-wait": "1.0.5",
87+
"koa-session": "^3.3.1",
9788
"mkdirp": "^0.5.1",
89+
"mustache": "^2.2.1",
9890
"postcss-cssnext": "^2.4.0",
9991
"postcss-import": "8.0.2",
10092
"postcss-loader": "^0.8.2",
10193
"postcss-mixins": "4.0.1",
10294
"postcss-modules-values": "1.1.1",
10395
"postcss-nested": "1.0.0",
10496
"postcss-simple-vars": "1.2.0",
97+
"react": "0.14.7",
10598
"react-addons-css-transition-group": "^0.14.7",
10699
"react-addons-test-utils": "^0.14.7",
100+
"react-bootstrap": "^0.28.3",
101+
"react-dom": "0.14.7",
102+
"react-inline-css": "2.1.0",
103+
"react-intl": "2.0.0-rc-1",
104+
"react-router": "2.0.1",
105+
"react-transmit": "https://github.com/ngduc/react-transmit.git",
106+
"serialize-javascript": "^1.2.0",
107107
"style-loader": "^0.13.0",
108108
"webpack": "1.12.14",
109109
"webpack-node-externals": "1.0.0"

src/containers/App/App.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,17 @@ import jaLocaleData from 'react-intl/locale-data/ja'
88
addLocaleData(enLocaleData)
99
addLocaleData(jaLocaleData)
1010

11-
let translations
1211
let currentLocale
1312
let messages
1413

15-
if (!__CLIENT__) {
16-
const i18n = require('server/libs/i18n') // eslint-disable-line global-require
17-
translations = i18n.getTranslations()
18-
}
19-
2014
export default class App extends React.Component {
2115
componentWillMount () {
2216
if (__CLIENT__) {
2317
currentLocale = (__CLIENT__ ? window.App.locale : 'en-US')
2418
messages = window.App.messages
2519
} else {
26-
currentLocale = this.props.location.query.locale || 'en-US'
27-
messages = translations[ currentLocale ]
20+
currentLocale = this.context.data.i18nData.locale || 'en-US'
21+
messages = this.context.data.i18nData.messages || {}
2822
}
2923
addLocaleData({
3024
locale: currentLocale,
@@ -42,3 +36,7 @@ export default class App extends React.Component {
4236
)
4337
}
4438
}
39+
40+
App.contextTypes = {
41+
data: React.PropTypes.object
42+
}

src/containers/Home/Home.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import Transmit from 'react-transmit'
33
import { Link } from 'react-router'
4-
import { injectIntl, FormattedDate } from 'react-intl'
4+
import { FormattedDate } from 'react-intl'
55

66
import { ButtonGroup, Button } from 'react-bootstrap'
77
import { CommentBox, NavBar, Utils } from 'components'

src/server.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Router from 'koa-router'
66
import serveStatic from 'koa-serve-static'
77
import koaConvert from 'koa-convert'
88
import koaCompress from 'koa-compress'
9+
import koaSession from 'koa-session'
910
import zlib from 'zlib'
1011

1112
import routesContainer from 'containers/routes'
@@ -15,11 +16,14 @@ import renderAppRouter from './server/renderAppRouter'
1516

1617
try {
1718
const app = new Koa()
19+
app.keys = ['seekreet', 'r3act-s3tup-k3y']
20+
1821
const hostname = process.env.HOSTNAME || 'localhost'
1922
const port = process.env.PORT || 8000
2023
let routes = routesContainer
2124

2225
app.use(koaCompress({ flush: zlib.Z_SYNC_FLUSH }))
26+
app.use(koaConvert(koaSession(app)))
2327

2428
app.use(apiRouter())
2529

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
import { RouterContext } from 'react-router'
3+
4+
5+
export function createRouterContextDataWrapper (dataObj) {
6+
return React.createClass({
7+
childContextTypes: {
8+
data: React.PropTypes.object.isRequired
9+
},
10+
getChildContext: () => {
11+
return {
12+
data: dataObj
13+
}
14+
},
15+
render: () => {
16+
return <RouterContext { ...this.props } />
17+
}
18+
})
19+
}

src/server/renderAppRouter.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ const IGNORED_FILES = ['/favicon.ico']
1111
const INDEX_TEMPLATE_FILE = 'dist/views/index.tpl.html'
1212

1313
const indexFileContent = fs.readFileSync(INDEX_TEMPLATE_FILE).toString()
14+
15+
import { createRouterContextDataWrapper } from './libs/RouterContextDataWrapper'
1416
const i18n = require('./libs/i18n')
15-
const i18nObj = { locale: '', messages: {} }
17+
let i18nData = { locale: '', messages: {} }
1618

1719

1820
export default function renderAppRouter () {
@@ -21,8 +23,11 @@ export default function renderAppRouter () {
2123
if (IGNORED_FILES.indexOf(location) >= 0) {
2224
return next()
2325
}
24-
i18nObj.locale = ctx.query.locale || 'en-US'
25-
const messagesJsonString = JSON.stringify(i18n.getLocaleMessages(i18nObj.locale))
26+
27+
const locale = ctx.query.locale || ctx.session.locale || 'en-US'
28+
ctx.session.locale = locale
29+
i18nData = i18n.getLocaleMessages(locale)
30+
const i18nDataString = JSON.stringify(i18nData)
2631

2732
match({ routes, location }, (error, redirectLocation, renderProps) => {
2833
if (redirectLocation) {
@@ -36,10 +41,11 @@ export default function renderAppRouter () {
3641
}
3742
else {
3843
const webserver = (__PRODUCTION__ ? '' : '//' + hostname + ':8080')
44+
const RouterContextDataWrapper = createRouterContextDataWrapper({ i18nData })
3945

40-
Transmit.renderToString(RouterContext, renderProps).then(({ reactString, reactData }) => {
46+
Transmit.renderToString(RouterContextDataWrapper, renderProps).then(({ reactString, reactData }) => {
4147
const renderedHtml = mustache.render(indexFileContent, {
42-
i18n: messagesJsonString,
48+
i18n: i18nDataString,
4349
reactString
4450
})
4551
const output = Transmit.injectIntoMarkup(renderedHtml, reactData, [`${webserver}/client.js`])

0 commit comments

Comments
 (0)