forked from webhintio/hint
/
http-cache.ts
437 lines (349 loc) · 15.6 KB
/
http-cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* @fileoverview Checks if your cache-control header and asset strategy follows best practices
*/
import * as pluralize from 'pluralize';
import { Category } from '../../enums/category';
import { IRule, IRuleBuilder, IFetchEnd } from '../../types';
import { normalizeString } from '../../utils/misc';
import { RuleContext } from '../../rule-context';
type targetType = 'fetch' | 'manifest' | 'target';
type Directives = Map<string, number>;
type ParsedDirectives = {
header: string;
invalidDirectives: Map<string, string>;
invalidValues: Map<string, string>;
usedDirectives: Directives;
};
const rule: IRuleBuilder = {
create(context: RuleContext): IRule {
/**
* Max time the HTML of a page can be cached.
* https://jakearchibald.com/2016/caching-best-practices/#used-carefully-max-age-mutable-content-can-be-beneficial
*/
const maxAgeTarget = context.ruleOptions && context.ruleOptions.maxAgeTarget || 180; // 3 minutes
/** Max time a resource (CSS, JS, images, etc.) can be cached.*/
const maxAgeResource = context.ruleOptions && context.ruleOptions.maxAgeResource || 31536000; // 1 year
/** Resources' mediaType that should be cached for a long time.*/
const longCached = [
'application/manifest+json',
'audio/ogg',
'audio/mpeg',
'audio/mp4',
'font/collection',
'application/vnd.ms-fontobject',
'font/opentype',
'font/otf',
'font/ttf',
'font/woff',
'font/woff2',
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
'image/svg+xml',
'image/webp',
'image/x-icon',
'text/css',
'text/javascript',
'video/mp4',
'video/ogg',
'video/webm'];
/** The predefined patterns for file revving.*/
const predefinedRevvingPatterns: Array<RegExp> = [
/*
* E.g.:
* - https://example.com/assets/script.12345.js
* - https://example.com/assets/script-2.12345.js
* - https://example.com/assets/script_2-12345.js
* - https://example.com/assets/icon_meca_1473335663.svg
*
* Live example: https://regex101.com/r/6VduJO/2/
*/
/\/(\w|-|_)+(\.|-|_)\w+\.\w+$/i
];
/** The cache revving patterns to use for matching.*/
let cacheRevvingPatterns: Array<RegExp> = [];
/**
* Parses the `Cache-Control` header of a response creating an object with valid and invalid directives,
* as well as invalid values.
*/
const parseCacheControlHeader = (cacheControlHeader: string): ParsedDirectives => {
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
const directives = ['must-revalidate', 'no-cache', 'no-store', 'no-transform', 'public', 'private', 'proxy-revalidate'];
const valueDirectives = ['max-age', 's-maxage'];
const extensionDirectives = ['immutable', 'stale-while-revalidate', 'stale-if-error'];
const usedDirectives = cacheControlHeader.split(',').map((value) => {
return value.trim();
});
const parsedCacheControlHeader = usedDirectives.reduce((parsed: ParsedDirectives, current: string) => {
const [directive, value] = current.split('=');
// Validate directive with value. E.g.: max-age=<seconds>
if (directive && value) {
/*
* Check if the directive has a value when it shouldn't
* E.g.: no-cache=12345
*/
if (!valueDirectives.includes(directive)) {
parsed.invalidValues.set(directive, value);
return parsed;
}
/*
* Check if the directive has the right value (positive integer)
* E.g.: max-age=3600
*/
const seconds = parseFloat(value);
if (!value || isNaN(seconds) || !Number.isInteger(seconds) || seconds < 0) {
parsed.invalidValues.set(directive, value);
return parsed;
}
parsed.usedDirectives.set(directive, seconds);
return parsed;
}
/*
* Check the directive is valid
* E.g.: no-cache
*/
if (directives.includes(directive) || extensionDirectives.includes(directive)) {
parsed.usedDirectives.set(directive, null);
} else {
parsed.invalidDirectives.set(directive, null);
}
return parsed;
},
{
header: cacheControlHeader,
invalidDirectives: new Map(),
invalidValues: new Map(),
usedDirectives: new Map<string, number>()
});
return parsedCacheControlHeader;
};
/**
* Transforms a Map of directives to a string with the following format:
*
* directive1=value1
* directive2
* diretive3=value3
*/
const directivesToString = (directives: Map<string, any>) => {
let str = '';
directives.forEach((val, key) => {
if (str.length > 0) {
str += '\n';
}
str += key;
if (val) {
str += `=${val}`;
}
});
return str;
};
/**
* Compares if the `max-age` or `s-maxage` directives of `directives` are smaller (<0),
* equal (0), or bigger (>0) than the given `threshold`.
*/
const compareToMaxAge = (directives: Directives, threshold: number): number => {
const maxAge = directives.get('max-age');
const sMaxAge = directives.get('s-maxage');
if (maxAge) {
return maxAge === threshold ? 0 : maxAge - threshold;
}
if (sMaxAge) {
return sMaxAge === threshold ? 0 : sMaxAge - threshold;
}
return -1;
};
/*
* ------------------------------------------------------------------------------
* Directive validators
* ------------------------------------------------------------------------------
*/
/**
* Prevents agains the usage of non recommended directives (`must-revalidate`)
*/
const nonRecommendedDirectives = (directives: Directives): string => {
const noDirectives = ['must-revalidate', 'no-store'];
for (const noDirective of noDirectives) {
if (directives.has(noDirective)) {
return noDirective;
}
}
return null;
};
/*
* Validate if cache-control exists, not having this header is
* an error because it's up to the browser vendor to decide what
* to do.
*/
const hasCacheControl = async (directives: ParsedDirectives, fetchEnd: IFetchEnd): Promise<boolean> => {
const { resource, response: { headers } } = fetchEnd;
const cacheControl: string = headers && headers['cache-control'] || null;
if (!cacheControl) {
await context.report(resource, fetchEnd.element, `No "cache-control" header or empty value found. It should have a value`);
return false;
}
return true;
};
/*
* Validates if all the cache-control directives and values are correct.
*/
const hasInvalidDirectives = async (directives: ParsedDirectives, fetchEnd: IFetchEnd): Promise<boolean> => {
const { invalidDirectives, invalidValues } = directives;
const { resource } = fetchEnd;
if (invalidDirectives.size > 0) {
const message: string = `The ${pluralize('directive', invalidDirectives.size)} ${Array.from(invalidDirectives.keys()).join(', ')} ${pluralize('is', invalidDirectives.size)} invalid`;
await context.report(resource, fetchEnd.element, message);
return false;
}
if (invalidValues.size > 0) {
const message: string = `The following ${pluralize('directive', invalidValues.size)} ${pluralize('have', invalidValues.size)} an invalid value:\n${directivesToString(invalidValues)}`;
await context.report(resource, fetchEnd.element, message);
return false;
}
return true;
};
/*
* Validates if there is any non recommended directives.
*/
const hasNoneNonRecommendedDirectives = async (directives: ParsedDirectives, fetchEnd: IFetchEnd): Promise<boolean> => {
const { usedDirectives } = directives;
const { resource } = fetchEnd;
const nonRecommendedDirective = nonRecommendedDirectives(usedDirectives);
if (nonRecommendedDirective) {
const message: string = `The directive "${nonRecommendedDirective}" is not recommended`;
await context.report(resource, fetchEnd.element, message);
return false;
}
return true;
};
/**
* Validates that `no-cache` and `no-store` are not used in combination
* with `max-age` or `s-maxage`.
*/
const validateDirectiveCombinations = async (directives: ParsedDirectives, fetchEnd: IFetchEnd): Promise<boolean> => {
const { header, usedDirectives } = directives;
if (usedDirectives.has('no-cache') || usedDirectives.has('no-store')) {
const hasMaxAge = (usedDirectives.has('max-age') || usedDirectives.has('s-maxage'));
if (hasMaxAge) {
const message: string = `The following Cache-Control header is using a wrong combination of directives:\n${header}`;
await context.report(fetchEnd.resource, fetchEnd.element, message);
return false;
}
}
return true;
};
/**
* Validates the target uses no-cache or a small max-age value
*/
const hasSmallCache = async (directives: ParsedDirectives, fetchEnd: IFetchEnd): Promise<boolean> => {
const { header, usedDirectives } = directives;
if (usedDirectives.has('no-cache')) {
return true;
}
const isValidCache = compareToMaxAge(usedDirectives, maxAgeTarget) <= 0;
if (!isValidCache) {
const message: string = `The target should not be cached, or have a small "max-age" value (${maxAgeTarget}):\n${header}`;
await context.report(fetchEnd.resource, fetchEnd.element, message);
return false;
}
return true;
};
/**
* Validates that a resource (JS, CSS, images, etc.) has the right caching directives.
*/
const hasLongCache = async (directives: ParsedDirectives, fetchEnd: IFetchEnd): Promise<boolean> => {
const { header, usedDirectives } = directives;
const { resource, element } = fetchEnd;
const longCache = compareToMaxAge(usedDirectives, maxAgeResource) >= 0;
const immutable = usedDirectives.has('immutable');
// We want long caches with "immutable" for static resources
if (usedDirectives.has('no-cache') || !(longCache && immutable)) {
const message: string = `Static resources should have a long cache value (${maxAgeResource}) and use the immutable directive:\n${header}`;
await context.report(resource, element, message);
return false;
}
return true;
};
/**
* Validates that a resource (JS, CSS, images, etc.) is using the right file revving format.
*/
const usesFileRevving = async (directives: ParsedDirectives, fetchEnd: IFetchEnd): Promise<boolean> => {
const { element, resource } = fetchEnd;
const matches = cacheRevvingPatterns.find((pattern) => {
return !!resource.match(pattern);
});
if (!matches) {
const message: string = `No patterns for file revving match ${resource}. See docs to add custom pattern.`;
await context.report(resource, element, message);
return false;
}
return true;
};
const validateFetch = (type: targetType) => {
return async (fetchEnd: IFetchEnd) => {
const headers = fetchEnd.response.headers;
const cacheControl: string = headers && headers['cache-control'] || '';
const parsedDirectives: ParsedDirectives = parseCacheControlHeader(normalizeString(cacheControl));
const { response: { mediaType } } = fetchEnd;
const validators = [
hasCacheControl,
hasInvalidDirectives,
hasNoneNonRecommendedDirectives,
validateDirectiveCombinations
];
if (type === 'target') {
validators.push(hasSmallCache);
} else if (type === 'fetch' && longCached.includes(mediaType)) {
validators.push(hasLongCache);
// Check if there are custom revving patterns
let customRegex = context.ruleOptions && context.ruleOptions.revvingPatterns || null;
if (customRegex) {
customRegex = customRegex.map((reg) => {
return new RegExp(reg, 'i');
});
}
cacheRevvingPatterns = customRegex || predefinedRevvingPatterns;
validators.push(usesFileRevving);
}
// TODO: It will be so nice to have an async `every` here instead of this...
for (const validator of validators) {
const result = await validator(parsedDirectives, fetchEnd);
if (!result) {
return;
}
}
return;
};
};
return {
'fetch::end': validateFetch('fetch'),
'manifestfetch::end': validateFetch('fetch'),
'targetfetch::end': validateFetch('target')
};
},
meta: {
docs: {
category: Category.performance,
description: `Checks if your cache-control header and asset strategy follows best practices`
},
recommended: true,
schema: [{
additionalProperties: false,
definitions: {
'string-array': {
items: { type: 'string' },
minItems: 1,
type: 'array',
uniqueItems: true
}
},
properties: {
maxAgeResource: 'number',
maxAgeTarget: 'number',
revvingPatterns: { $ref: '#/definitions/string-array' }
}
}],
worksWithLocalFiles: false
}
};
module.exports = rule;