File tree 8 files changed +61
-17
lines changed
spec/unit/server/middleware
8 files changed +61
-17
lines changed Original file line number Diff line number Diff line change
1
+ module . exports = require ( './lib/client' ) ;
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ describe('Render Middleware', () => {
28
28
. get ( '/' )
29
29
. end ( ( err , res ) => {
30
30
if ( err ) throw err ;
31
- assert . match ( res . text , / < h t m l > < b o d y > < s c r i p t t y p e = \" a p p l i c a t i o n \/ j s o n \" i d = \" m y a p p - s t a t e \" > (?: .+ ) < \/ s c r i p t > < d i v (?: .* ) i d = " m y a p p " (?: .* ) > (?: .* ) < \/ d i v > < \/ b o d y > < \/ h t m l > / )
31
+ assert . match ( res . text , / < h t m l > < b o d y > < s c r i p t t y p e = \" a p p l i c a t i o n \/ j s o n \" i d = \" m y a p p - s t a t e \" > (?: .+ ) < \/ s c r i p t > \n < d i v (?: .* ) i d = " m y a p p " (?: .* ) > (?: .* ) < \/ d i v > < \/ b o d y > < \/ h t m l > / )
32
32
done ( ) ;
33
33
} ) ;
34
34
} ) ;
Original file line number Diff line number Diff line change
1
+ import render from './render' ;
2
+
3
+ export default { render } ;
Original file line number Diff line number Diff line change
1
+ import { parse } from '../shared/route-parser' ;
2
+ import React from 'react' ;
3
+ import ReactDOM from 'react-dom' ;
4
+ import decode from 'ent/decode' ;
5
+
6
+ export default function render ( application ) {
7
+ let url = `${ window . location . protocol } //${ window . location . host } ${ window . location . pathname } ${ window . location . search } ` ;
8
+
9
+ let matches = parse ( application . routes , url ) ;
10
+
11
+ if ( matches ) {
12
+ let { route, context } = matches ;
13
+ const stateNode = document . getElementById ( `${ application . domRoot } -state` ) ;
14
+ const state = JSON . parse ( decode ( stateNode . text ) ) ;
15
+ let rootNode = document . getElementById ( application . domRoot ) ;
16
+ const Component = route . component ;
17
+
18
+ ReactDOM . render (
19
+ < Component
20
+ context = { context }
21
+ state = { state }
22
+ /> ,
23
+ rootNode ) ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change 1
1
import koa from 'koa' ;
2
-
3
2
import init from './middleware/init' ;
4
- import render from './middleware/render' ;
5
3
import router from './middleware/router' ;
4
+ import render from './middleware/render' ;
6
5
7
6
/**
8
7
* Instantiate a server given an application.
@@ -11,11 +10,14 @@ import router from './middleware/router';
11
10
12
11
export default function server ( application , options ) {
13
12
const app = koa ( ) ;
14
-
15
- app
16
- . use ( init ( application , options ) )
17
- . use ( router )
18
- . use ( render )
19
-
20
- return app ;
13
+ return {
14
+ use : ( mw ) => app . use ( mw ) ,
15
+ start : ( ) => {
16
+ return app
17
+ . use ( init ( application , options ) )
18
+ . use ( router )
19
+ . use ( render )
20
+ . listen ( options . port )
21
+ }
22
+ }
21
23
}
Original file line number Diff line number Diff line change @@ -10,9 +10,12 @@ export default function* renderMiddleware(next) {
10
10
let Component = this . arch . route . component ;
11
11
12
12
let body = ReactDOMServer . renderToString (
13
- < Component context = { this . arch . context } />
13
+ < Component
14
+ context = { this . arch . context }
15
+ state = { this . arch . state }
16
+ />
14
17
) ;
15
18
16
- this . body = template . replace ( '{{root}}' , `<script type="application/json" id="${ domRoot } -state">${ encode ( JSON . stringify ( this . arch . state ) ) } </script><div id="${ domRoot } ">${ body } </div>` ) ;
19
+ this . body = template . replace ( '{{root}}' , `<script type="application/json" id="${ domRoot } -state">${ encode ( JSON . stringify ( this . arch . state ) ) } </script>\n <div id="${ domRoot } ">${ body } </div>` ) ;
17
20
yield next ;
18
21
}
Original file line number Diff line number Diff line change 1
- import { match } from '../../shared/route-parser' ;
1
+ import { parse } from '../../shared/route-parser' ;
2
2
3
3
export default function * routerMiddleware ( next ) {
4
4
if ( typeof this . arch === 'undefined' ) throw new Error ( 'Router middleware must be run inside an Arch server.' ) ;
5
5
6
- let routes = this . arch . application . routes ;
7
- let context ;
8
- let route = routes . find ( ( { path } ) => context = match ( this . originalUrl , path ) ) ;
6
+ let matched = parse ( this . arch . application . routes , this . originalUrl ) ;
9
7
10
- if ( route ) {
8
+ if ( matched ) {
9
+ let { route, context } = matched ;
11
10
this . arch . route = route ;
12
11
this . arch . context = context ;
13
12
yield next ;
Original file line number Diff line number Diff line change @@ -42,3 +42,14 @@ export function match(originalUrl, route) {
42
42
route
43
43
} ;
44
44
}
45
+
46
+ export function parse ( routes , url ) {
47
+ let context ;
48
+ let route = routes . find ( ( { path } ) => context = match ( url , path ) ) ;
49
+
50
+ if ( route ) {
51
+ return { route, context } ;
52
+ } else {
53
+ return null ;
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments