@@ -196,6 +196,11 @@ export class App<State> {
196196 req : Request ,
197197 conn : Deno . ServeHandlerInfo = DEFAULT_CONN_INFO ,
198198 ) => {
199+ if ( this . config . cache && req . method === "GET" ) {
200+ const cachedResponse = await this . config . cache . match ( req ) ;
201+ if ( cachedResponse ) return cachedResponse ;
202+ }
203+
199204 const url = new URL ( req . url ) ;
200205 // Prevent open redirect attacks
201206 url . pathname = url . pathname . replace ( / \/ + / g, "/" ) ;
@@ -225,24 +230,28 @@ export class App<State> {
225230 span . setAttribute ( "http.route" , pattern ) ;
226231 }
227232
233+ let response : Response ;
228234 try {
229- if ( handlers . length === 1 && handlers [ 0 ] . length === 1 ) {
230- return handlers [ 0 ] [ 0 ] ( ctx ) ;
231- }
232- return await runMiddlewares ( handlers , ctx ) ;
235+ response = handlers . length === 1 && handlers [ 0 ] . length === 1
236+ ? await handlers [ 0 ] [ 0 ] ( ctx )
237+ : await runMiddlewares ( handlers , ctx ) ;
233238 } catch ( err ) {
234239 if ( err instanceof HttpError ) {
235240 if ( err . status >= 500 ) {
236241 // deno-lint-ignore no-console
237242 console . error ( err ) ;
238243 }
239- return new Response ( err . message , { status : err . status } ) ;
244+ response = new Response ( err . message , { status : err . status } ) ;
240245 }
241246
242247 // deno-lint-ignore no-console
243248 console . error ( err ) ;
244- return new Response ( "Internal server error" , { status : 500 } ) ;
249+ response = new Response ( "Internal server error" , { status : 500 } ) ;
250+ }
251+ if ( this . config . cache && req . method === "GET" ) {
252+ this . config . cache . put ( req , response . clone ( ) ) ;
245253 }
254+ return response ;
246255 } ;
247256 }
248257
0 commit comments