@@ -9,6 +9,7 @@ import { BodyFile } from './BodyFile';
9
9
import { SyntheticApiResponse } from './SyntheticApiResponse' ;
10
10
import { BuildCacheDirArg } from '../CacheRoute/options' ;
11
11
import { HttpMethod , ResolvedCacheRouteOptions } from '../CacheRoute' ;
12
+ import { debug } from '../debug' ;
12
13
13
14
export class CacheRouteHandler {
14
15
private req : Request ;
@@ -23,6 +24,7 @@ export class CacheRouteHandler {
23
24
this . req = route . request ( ) ;
24
25
}
25
26
27
+ // eslint-disable-next-line complexity, max-statements
26
28
async handle ( ) {
27
29
if ( ! this . isRequestMatched ( ) ) {
28
30
await this . route . fallback ( ) ;
@@ -40,10 +42,14 @@ export class CacheRouteHandler {
40
42
this . buildCacheDir ( ) ;
41
43
this . storeLastModified ( ) ;
42
44
43
- const response =
44
- forceUpdate || this . isExpired ( )
45
- ? await this . fetchFromServer ( ) // prettier-ignore
46
- : await this . fetchFromCache ( ) ;
45
+ const useRealRequest = forceUpdate || this . isExpired ( ) ;
46
+ const response = useRealRequest
47
+ ? await this . fetchFromServer ( ) // prettier-ignore
48
+ : await this . fetchFromCache ( ) ;
49
+
50
+ if ( useRealRequest && this . matchHttpStatus ( response ) ) {
51
+ await this . saveResponse ( response ) ;
52
+ }
47
53
48
54
await this . fulfillRoute ( response ) ;
49
55
}
@@ -62,17 +68,13 @@ export class CacheRouteHandler {
62
68
}
63
69
64
70
private async fetchFromServer ( ) {
71
+ debug ( `Fetching from server: ${ this . req . method ( ) } ${ this . req . url ( ) } ` ) ;
65
72
const overrides = toFunction ( this . options . overrides ) ( this . req ) ;
66
- const response = await this . route . fetch ( overrides ) ;
67
-
68
- if ( this . matchHttpStatus ( response ) ) {
69
- await this . trySaveResponse ( response ) ;
70
- }
71
-
72
- return response ;
73
+ return this . route . fetch ( overrides ) ;
73
74
}
74
75
75
76
private async fetchFromCache ( ) {
77
+ debug ( `Fetching from cache: ${ this . req . method ( ) } ${ this . req . url ( ) } ` ) ;
76
78
const responseInfo = await new HeadersFile ( this . cacheDir ) . read ( ) ;
77
79
const bodyFile = new BodyFile ( this . cacheDir , responseInfo ) ;
78
80
const body = await bodyFile . read ( ) ;
@@ -90,7 +92,7 @@ export class CacheRouteHandler {
90
92
return lastModified > this . lastModified ;
91
93
}
92
94
93
- private async trySaveResponse ( response : APIResponse ) {
95
+ private async saveResponse ( response : APIResponse ) {
94
96
const responseInfo : ResponseInfo = {
95
97
url : response . url ( ) ,
96
98
status : response . status ( ) ,
@@ -100,8 +102,11 @@ export class CacheRouteHandler {
100
102
const body = await response . body ( ) ;
101
103
// file can be updated by another worker
102
104
if ( ! this . isUpdated ( ) ) {
105
+ debug ( `Writing cache: ${ this . cacheDir } ` ) ;
103
106
new HeadersFile ( this . cacheDir ) . save ( responseInfo ) ;
104
107
new BodyFile ( this . cacheDir , responseInfo ) . save ( body ) ;
108
+ } else {
109
+ debug ( `Skip writing cache, updated by another worker: ${ this . cacheDir } ` ) ;
105
110
}
106
111
}
107
112
0 commit comments