@@ -10,7 +10,7 @@ import {
10
10
import type { Request , Response } from 'express' ;
11
11
import { HTMLRewriter } from 'htmlrewriter' ;
12
12
13
- import { BadRequest , Config , URLHelper } from '../../fundamentals' ;
13
+ import { BadRequest , Cache , Config , URLHelper } from '../../fundamentals' ;
14
14
import type { LinkPreviewRequest , LinkPreviewResponse } from './types' ;
15
15
import {
16
16
appendUrl ,
@@ -31,6 +31,7 @@ export class WorkerController {
31
31
32
32
constructor (
33
33
config : Config ,
34
+ private readonly cache : Cache ,
34
35
private readonly url : URLHelper
35
36
) {
36
37
this . allowedOrigin = [
@@ -138,6 +139,17 @@ export class WorkerController {
138
139
this . logger . debug ( 'Processing request' , { origin, url : targetURL } ) ;
139
140
140
141
try {
142
+ const cachedResponse = await this . cache . get < string > ( targetURL . toString ( ) ) ;
143
+ if ( cachedResponse ) {
144
+ return resp
145
+ . status ( 200 )
146
+ . header ( {
147
+ 'content-type' : 'application/json;charset=UTF-8' ,
148
+ ...getCorsHeaders ( origin ) ,
149
+ } )
150
+ . send ( cachedResponse ) ;
151
+ }
152
+
141
153
const response = await fetch ( targetURL , {
142
154
headers : cloneHeader ( request . headers ) ,
143
155
} ) ;
@@ -245,6 +257,7 @@ export class WorkerController {
245
257
responseSize : json . length ,
246
258
} ) ;
247
259
260
+ await this . cache . set ( targetURL . toString ( ) , res ) ;
248
261
return resp
249
262
. status ( 200 )
250
263
. header ( {
0 commit comments