Skip to content

Commit

Permalink
Fix: Add new patterns to http-cache revving
Browse files Browse the repository at this point in the history
* Add `_` as another separator for file revving in `http-cache` rule
* Point to the docs when no pattern is found so users know it can be
  configured

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Fix webhintio#741
  • Loading branch information
molant committed Jan 16, 2018
1 parent 188d270 commit f4255d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/lib/rules/http-cache/http-cache.ts
Expand Up @@ -63,9 +63,11 @@ const rule: IRuleBuilder = {
* - 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,
/\/(\w|-|_)+-\w+\.\w+$/i
/\/(\w|-|_)+(\.|-|_)\w+\.\w+$/i
];

/** The cache revving patterns to use for matching.*/
Expand Down Expand Up @@ -344,7 +346,7 @@ const rule: IRuleBuilder = {
});

if (!matches) {
const message: string = `No patterns for file revving match ${resource}`;
const message: string = `No patterns for file revving match ${resource}. See docs to add custom pattern.`;

await context.report(resource, element, message);

Expand Down
24 changes: 19 additions & 5 deletions tests/lib/rules/http-cache/tests.ts
Expand Up @@ -213,7 +213,7 @@ const defaultTests: Array<IRuleTest> = [
},
{
name: 'JS with long max-age, immutable and no file revving fails',
reports: [{ message: 'No patterns for file revving match http://localhost/script.js' }],
reports: [{ message: 'No patterns for file revving match http://localhost/script.js. See docs to add custom pattern.' }],
serverConfig: {
'/': generateHTMLPage('<link rel="icon" href="/favicon.123.ico"><script src="/script.js"></script>'),
'/favicon.123.ico': {
Expand All @@ -240,9 +240,23 @@ const defaultTests: Array<IRuleTest> = [
}
}
},
{
name: 'JS with long max-age, immutable and file revving with `_` passes',
serverConfig: {
'/': generateHTMLPage('<link rel="icon" href="/favicon.123.ico"><script src="/script_123.js"></script>'),
'/favicon_123.ico': {
content: '',
headers: { 'Cache-Control': 'max-age=31536000, immutable' }
},
'/script_123.js': {
content: 'var a = 10;',
headers: { 'Cache-Control': 'max-age=31536000, immutable' }
}
}
},
{
name: 'JS with long max-age, immutable and parameter file revving fails',
reports: [{ message: 'No patterns for file revving match http://localhost/script.js?v=123' }],
reports: [{ message: 'No patterns for file revving match http://localhost/script.js?v=123. See docs to add custom pattern.' }],
serverConfig: {
'/': generateHTMLPage('<link rel="icon" href="/favicon.123.ico"><script src="/script.js?v=123"></script>'),
'/favicon.123.ico': {
Expand Down Expand Up @@ -273,7 +287,7 @@ const defaultTests: Array<IRuleTest> = [
},
{
name: 'CSS with long max-age, immutable and no file revving fails',
reports: [{ message: 'No patterns for file revving match http://localhost/styles.css' }],
reports: [{ message: 'No patterns for file revving match http://localhost/styles.css. See docs to add custom pattern.' }],
serverConfig: {
'/': generateHTMLPage('<link rel="icon" href="/favicon.123.ico"><link rel="stylesheet" href="styles.css">'),
'/favicon.123.ico': {
Expand Down Expand Up @@ -302,7 +316,7 @@ const defaultTests: Array<IRuleTest> = [
},
{
name: 'CSS with long max-age, immutable and parameter file revving fails',
reports: [{ message: 'No patterns for file revving match http://localhost/styles.css?v=123' }],
reports: [{ message: 'No patterns for file revving match http://localhost/styles.css?v=123. See docs to add custom pattern.' }],
serverConfig: {
'/': generateHTMLPage('<link rel="icon" href="/favicon.123.ico"><link rel="stylesheet" href="styles.css?v=123">'),
'/favicon.123.ico': {
Expand All @@ -319,7 +333,7 @@ const defaultTests: Array<IRuleTest> = [
const customRegexTests: Array<IRuleTest> = [
{
name: 'JS with long max-age, immutable and file revving fails custom regex',
reports: [{ message: 'No patterns for file revving match http://localhost/script.123.js' }],
reports: [{ message: 'No patterns for file revving match http://localhost/script.123.js. See docs to add custom pattern.' }],
serverConfig: {
'/': generateHTMLPage('<link rel="icon" href="/12345/favicon.ico"><script src="/script.123.js"></script>'),
'/12345/favicon.ico': {
Expand Down

0 comments on commit f4255d2

Please sign in to comment.