@@ -35,16 +35,26 @@ function requireUncached(module: string): any {
3535 return require ( module ) ;
3636}
3737
38- function loadTwilioFunction (
39- fnPath : string ,
40- config : StartCliConfig
41- ) : ServerlessFunctionSignature {
42- if ( config . live ) {
43- debug ( 'Uncached loading of %s' , fnPath ) ;
44- return requireUncached ( fnPath ) . handler ;
45- } else {
46- return require ( fnPath ) . handler ;
47- }
38+ function loadTwilioFunction ( fnPath : string ) : ServerlessFunctionSignature {
39+ return require ( fnPath ) . handler ;
40+ }
41+
42+ function requireCacheCleaner (
43+ req : ExpressRequest ,
44+ res : ExpressResponse ,
45+ next : NextFunction
46+ ) {
47+ debug ( 'Deleting require cache' ) ;
48+ Object . keys ( require . cache ) . forEach ( key => {
49+ // Entries in the cache that end with .node are compiled binaries, deleting
50+ // those has unspecified results, so we keep them.
51+ // Entries in the cache that include "twilio-run" are part of this module
52+ // or its dependencies, so don't need to be cleared.
53+ if ( ! ( key . endsWith ( '.node' ) || key . includes ( 'twilio-run' ) ) ) {
54+ delete require . cache [ key ] ;
55+ }
56+ } ) ;
57+ next ( ) ;
4858}
4959
5060export async function createServer (
@@ -77,6 +87,7 @@ export async function createServer(
7787
7888 if ( config . live ) {
7989 app . use ( nocache ( ) ) ;
90+ app . use ( requireCacheCleaner ) ;
8091 }
8192
8293 if ( config . legacyMode ) {
@@ -160,7 +171,7 @@ export async function createServer(
160171 }
161172
162173 debug ( 'Load & route to function at "%s"' , functionPath ) ;
163- const twilioFunction = loadTwilioFunction ( functionPath , config ) ;
174+ const twilioFunction = loadTwilioFunction ( functionPath ) ;
164175 if ( typeof twilioFunction !== 'function' ) {
165176 return res
166177 . status ( 404 )
0 commit comments