@@ -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,22 @@ 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+ console . log ( { currentContext } )
142+ const result = await method . call ( boundKlass , currentContext )
143143 response . json ( { result } )
144144 } catch ( error ) {
145145 printError ( error )
146- reqres . clear ( )
147146 response . status ( 500 ) . json ( { } )
148147 }
149148 } else {
150- reqres . clear ( )
151149 response . status ( 404 ) . json ( { } )
152150 }
153151 } )
154152
155153 if ( module . hot ) {
156154 server . all ( `/${ prefix } /:version/:hash/:methodName.json` , async ( request , response ) => {
157155 const payload = request . method === 'GET' ? request . query . payload : request . body
158- reqres . set ( request , response )
159156 const args = deserialize ( payload )
160157 const { version, hash, methodName } = request . params
161158 const [ invokerHash , boundHash ] = hash . split ( '-' )
@@ -178,17 +175,15 @@ server.start = function () {
178175 const method = registry [ key ]
179176 if ( method !== undefined ) {
180177 try {
181- const subcontext = generateContext ( { request , response , ... args } )
182- const result = await method . call ( boundKlass , subcontext )
183- reqres . clear ( )
178+ console . log ( "DEVY" )
179+ const currentContext = getCurrentContext ( args )
180+ const result = await method . call ( boundKlass , currentContext )
184181 response . json ( { result } )
185182 } catch ( error ) {
186183 printError ( error )
187- reqres . clear ( )
188184 response . status ( 500 ) . json ( { } )
189185 }
190186 } else {
191- reqres . clear ( )
192187 response . status ( 404 ) . json ( { } )
193188 }
194189 }
@@ -210,15 +205,11 @@ server.start = function () {
210205 if ( request . originalUrl . split ( '?' ) [ 0 ] . indexOf ( '.' ) > - 1 ) {
211206 return next ( )
212207 }
213- reqres . set ( request , response )
214208 const scope = await prerender ( request , response )
215209 if ( ! response . headersSent ) {
216210 const status = scope . context . page . status
217211 const html = template ( scope )
218- reqres . clear ( )
219212 response . status ( status ) . send ( html )
220- } else {
221- reqres . clear ( )
222213 }
223214 } )
224215
0 commit comments