1
- // noinspection JSUnresolvedReference
1
+ // noinspection JSUnresolvedReference, JSUnusedGlobalSymbols
2
2
3
3
import * as memoryCache from './memory.js' ;
4
4
@@ -69,7 +69,15 @@ export default {
69
69
* @param {Object } response - The `ResponseHandler` object.
70
70
*/
71
71
const serveCacheIfAvailable = ( request , response ) => {
72
- if ( isPathExcluded ( request ) ) return ;
72
+ if ( isPathExcluded ( request ) ) {
73
+ response . setHeaders ( { CACHE_STATUS_HEADER_KEY : 'EXCLUDED' } ) ;
74
+ return ;
75
+ }
76
+
77
+ if ( ! isRequestValid ( request ) ) {
78
+ response . setHeaders ( { CACHE_STATUS_HEADER_KEY : 'IGNORED' } ) ;
79
+ return ;
80
+ }
73
81
74
82
const entry = memoryCache . get ( request . url ) ;
75
83
if ( entry ) {
@@ -78,7 +86,7 @@ const serveCacheIfAvailable = (request, response) => {
78
86
if ( entry . isInfinite ) {
79
87
headers [ 'cache-control' ] = 'public, immutable' ;
80
88
} else {
81
- const maxAge = Math . round ( configOptions . expirationTime / 1000 ) ;
89
+ const maxAge = Math . round ( entry . timeout / 1000 ) ;
82
90
headers [ 'cache-control' ] = `max-age=${ maxAge } ` ;
83
91
}
84
92
}
@@ -108,6 +116,11 @@ const saveCache = (request, interceptor) => {
108
116
return ;
109
117
}
110
118
119
+ if ( ! isRequestValid ( request ) ) {
120
+ headers [ CACHE_STATUS_HEADER_KEY ] = 'IGNORED' ;
121
+ return ;
122
+ }
123
+
111
124
const excludeCache = headers [ 'apicache-exclude' ] ?? false ;
112
125
if ( excludeCache ) {
113
126
headers [ CACHE_STATUS_HEADER_KEY ] = 'EXCLUDED' ;
@@ -137,3 +150,15 @@ const isPathExcluded = (request) => {
137
150
: exclude . test ( request . path ) ,
138
151
) ;
139
152
} ;
153
+
154
+ /**
155
+ * We shouldn't really cache the `PUT`, `POST` or `DELETE` method types.
156
+ *
157
+ * @param {Object } request - The `RequestHandler` object.
158
+ * @returns {boolean } False if the request method is one of the above-mentioned, true otherwise.
159
+ */
160
+ const isRequestValid = ( request ) => {
161
+ const method = request . method ;
162
+ const methodsToExclude = [ 'put' , 'post' , 'delete' ] ;
163
+ return ! methodsToExclude . includes ( method ) ;
164
+ } ;
0 commit comments