@@ -3,7 +3,7 @@ import express from 'express'
33import path from 'path'
44import deserialize from '../shared/deserialize'
55import prefix from '../shared/prefix'
6- import context , { generateContext } from './context'
6+ import context , { getCurrentContext , generateCurrentContext } from './context'
77import emulatePrerender from './emulatePrerender'
88import environment from './environment'
99import exposeServerFunctions from './exposeServerFunctions'
@@ -13,7 +13,6 @@ import generateManifest from './manifest'
1313import { prerender } from './prerender'
1414import printError from './printError'
1515import registry from './registry'
16- import reqres from './reqres'
1716import generateRobots from './robots'
1817import template from './template'
1918import { generateServiceWorker } from './worker'
@@ -31,7 +30,9 @@ server.use(async (request, response, next) => {
3130 typeof context . start === 'function' && ( await context . start ( ) )
3231 contextStarted = true
3332 }
34- next ( )
33+ generateCurrentContext ( { request, response} , ( ) => {
34+ next ( )
35+ } )
3536} )
3637
3738emulatePrerender ( server )
@@ -120,7 +121,6 @@ server.start = function () {
120121
121122 server . all ( `/${ prefix } /:hash/:methodName.json` , async ( request , response ) => {
122123 const payload = request . method === 'GET' ? request . query . payload : request . body
123- reqres . set ( request , response )
124124 const args = deserialize ( payload )
125125 const { hash, methodName } = request . params
126126 const [ invokerHash , boundHash ] = hash . split ( '-' )
@@ -137,25 +137,21 @@ server.start = function () {
137137 const method = registry [ key ]
138138 if ( method !== undefined ) {
139139 try {
140- const subcontext = generateContext ( { request, response, ...args } )
141- const result = await method . call ( boundKlass , subcontext )
142- reqres . clear ( )
140+ const currentContext = getCurrentContext ( args )
141+ const result = await method . call ( boundKlass , currentContext )
143142 response . json ( { result } )
144143 } catch ( error ) {
145144 printError ( error )
146- reqres . clear ( )
147145 response . status ( 500 ) . json ( { } )
148146 }
149147 } else {
150- reqres . clear ( )
151148 response . status ( 404 ) . json ( { } )
152149 }
153150 } )
154151
155152 if ( module . hot ) {
156153 server . all ( `/${ prefix } /:version/:hash/:methodName.json` , async ( request , response ) => {
157154 const payload = request . method === 'GET' ? request . query . payload : request . body
158- reqres . set ( request , response )
159155 const args = deserialize ( payload )
160156 const { version, hash, methodName } = request . params
161157 const [ invokerHash , boundHash ] = hash . split ( '-' )
@@ -178,17 +174,14 @@ server.start = function () {
178174 const method = registry [ key ]
179175 if ( method !== undefined ) {
180176 try {
181- const subcontext = generateContext ( { request, response, ...args } )
182- const result = await method . call ( boundKlass , subcontext )
183- reqres . clear ( )
177+ const currentContext = getCurrentContext ( args )
178+ const result = await method . call ( boundKlass , currentContext )
184179 response . json ( { result } )
185180 } catch ( error ) {
186181 printError ( error )
187- reqres . clear ( )
188182 response . status ( 500 ) . json ( { } )
189183 }
190184 } else {
191- reqres . clear ( )
192185 response . status ( 404 ) . json ( { } )
193186 }
194187 }
@@ -210,15 +203,11 @@ server.start = function () {
210203 if ( request . originalUrl . split ( '?' ) [ 0 ] . indexOf ( '.' ) > - 1 ) {
211204 return next ( )
212205 }
213- reqres . set ( request , response )
214206 const scope = await prerender ( request , response )
215207 if ( ! response . headersSent ) {
216208 const status = scope . context . page . status
217209 const html = template ( scope )
218- reqres . clear ( )
219210 response . status ( status ) . send ( html )
220- } else {
221- reqres . clear ( )
222211 }
223212 } )
224213
0 commit comments