1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144 |
- var debug = require('debug')('ylt:policies');
- var offendersHelpers = require('../offendersHelpers');
- var policies = {
- "DOMelementsCount": {
- "tool": "phantomas",
- "label": "DOM elements count",
- "message": "<p>A high number of DOM elements means a lot of work for the browser to render the page.</p><p>It also slows down JavaScript DOM queries, as there are more elements to search through.</p>",
- "isOkThreshold": 1000,
- "isBadThreshold": 2500,
- "isAbnormalThreshold": 4000,
- "hasOffenders": false
- },
- "DOMelementMaxDepth": {
- "tool": "phantomas",
- "label": "DOM max depth",
- "message": "<p>A deep DOM makes the CSS matching with DOM elements difficult.</p><p>It also slows down JavaScript modifications to the DOM because changing the dimensions of an element makes the browser re-calculate the dimensions of it's parents. Same thing for JavaScript events, that bubble up to the document root.</p>",
- "isOkThreshold": 10,
- "isBadThreshold": 20,
- "isAbnormalThreshold": 28,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- var domArrays = offenders.map(offendersHelpers.domPathToArray);
- return {
- count: offenders.length,
- tree: offendersHelpers.listOfDomArraysToTree(domArrays)
- };
- }
- },
- "iframesCount": {
- "tool": "phantomas",
- "label": "Number of iframes",
- "message": "<p>iFrames are the most complex HTML elements. They are pages, just like the main page, and the browser needs to create a new page context, which has a cost.</p>",
- "isOkThreshold": 2,
- "isBadThreshold": 15,
- "isAbnormalThreshold": 30,
- "hasOffenders": false
- },
- "DOMidDuplicated": {
- "tool": "phantomas",
- "label": "IDs duplicated",
- "message": "<p>IDs of HTML elements must be document-wide unique. This can cause problems with getElementById returning the wrong element.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 5,
- "isAbnormalThreshold": 10,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^(.*): ?(\d+) ?occurrences$/.exec(offender);
- if (!parts) {
- debug('DOMidDuplicated offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- id: parts[1],
- occurrences: parseInt(parts[2], 10)
- };
- })
- };
- }
- },
- "DOMinserts": {
- "tool": "phantomas",
- "label": "DOM inserts",
- "message": "<p>Working with the DOM in JavaScript triggers layout calculations and slows down the page.</p><p>Try, as much as possible, to have an HTML page fully generated by the server instead of making changes with JS.</p>",
- "isOkThreshold": 10,
- "isBadThreshold": 400,
- "isAbnormalThreshold": 1000,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^"(.*)" ?appended ?to ?"(.*)"$/.exec(offender);
- if (!parts) {
- debug('DOMinserts offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- insertedElement: offendersHelpers.domPathToDomElementObj(parts[1]),
- receiverElement: offendersHelpers.domPathToDomElementObj(parts[2])
- };
- })
- };
- }
- },
- "DOMqueries": {
- "tool": "phantomas",
- "label": "DOM queries",
- "message": "<p>DOM queries are like looking in a large catalog of items. Even if the browsers made progress on the performances of queries, websites often make hundreds of them.</p><p>Try to reduce the number of queries by refactoring your JavaScript code.</p><p>Avoid also to have a read query between two write queries. To be able to reduce the number repaints and optimize performances, browsers buffer the DOM writing operations and treat them in bulk. But each time a DOM reading is asked, the browser needs to empty the buffer. This can be particularly slow inside a loop.</p>",
- "isOkThreshold": 50,
- "isBadThreshold": 1000,
- "isAbnormalThreshold": 2000,
- "hasOffenders": false
- },
- "DOMqueriesWithoutResults": {
- "tool": "phantomas",
- "label": "DOM queries without result",
- "message": "<p>Number of queries that return no result.</p><p>It suggests the query is not used on the page, probably because it is some dead code.</p><p>Or maybe the code is trying to find an HTML block that is not always here. Look at the JS Timeline to see if the scripts correctly figures out the HTML block is not here and immediatly stops interacting further with the DOM.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 100,
- "isAbnormalThreshold": 200,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^(.*) ?\(in ?(.*)\) ?using ?(.*)$/.exec(offender);
- if (!parts) {
- debug('DOMqueriesWithoutResults offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- query: parts[1],
- context: offendersHelpers.domPathToDomElementObj(parts[2]),
- fn: parts[3]
- };
- })
- };
- }
- },
- "DOMqueriesAvoidable": {
- "tool": "phantomas",
- "label": "Duplicated DOM queries",
- "message": "<p>This is the number of queries that could be avoided by removing all duplicated queries.</p><p>Simply save the result of a query in a variable. Ok it is not always simple, especially with third-party scripts, but at least do it with your own code.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 200,
- "isAbnormalThreshold": 500,
- "hasOffenders": true,
- "takeOffendersFrom": "DOMqueriesDuplicated",
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^[^"]* ?"(.*)" ?with ?(.*) ?\(in ?context ?(.*)\): ?(.*)\s?queries$/.exec(offender);
- if (!parts) {
- debug('DOMqueriesAvoidable offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- query: parts[1],
- context: offendersHelpers.domPathToDomElementObj(parts[3]),
- fn: parts[2],
- count: parseInt(parts[4], 10)
- };
- })
- };
- }
- },
- "eventsBound": {
- "tool": "phantomas",
- "label": "Events bound",
- "message": "<p>Binding too many events has a cost.</p><p>It can be avoided by using \"event delegation\". Instead of binding events on each element one by one, events delegation binds them on the top level document element and uses the bubbling principle. It will imperceptibly slow down the event when it occurs, but the loading of the page will speed-up.</p>",
- "isOkThreshold": 100,
- "isBadThreshold": 800,
- "isAbnormalThreshold": 1500,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^"(.*)" ?bound ?to ?"(.*)"$/.exec(offender);
- if (!parts) {
- debug('eventsBound offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- eventName: parts[1],
- element: offendersHelpers.domPathToDomElementObj(parts[2])
- };
- })
- };
- }
- },
- "jsErrors": {
- "tool": "phantomas",
- "label": "JavaScript errors",
- "message": "<p>Just to let you know there are some errors on the page.</p><p><b>Please note that some errors only occur in the PhantomJS browser, so you might need to double check on other browsers.</b></p>",
- "isOkThreshold": 0,
- "isBadThreshold": 1,
- "isAbnormalThreshold": 4,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^(.*) - (.*)$/.exec(offender);
- if (!parts) {
- debug('jsErrors offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- var backtraceArray = offendersHelpers.backtraceToArray(parts[2]);
- return {
- error: parts[1],
- backtrace: backtraceArray || []
- };
- })
- };
- }
- },
- "evalCalls": {
- "tool": "phantomas",
- "label": "eval calls",
- "message": "<p>The 'eval' function is slow and is a bad coding practice. Try to get rid of it.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 10,
- "isAbnormalThreshold": 20,
- "hasOffenders": false
- },
- "documentWriteCalls": {
- "tool": "phantomas",
- "label": "document.write calls",
- "message": "<p>They slow down the page construction, especially if they are used to insert scripts in the page. Remove them ASAP.</p><p>If you cannot remove them because they come from a third-party script (such as ads), have a look at <a href=\"https://github.com/krux/postscribe\" target=\"_blank\">PostScribe</a>.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 10,
- "isAbnormalThreshold": 20,
- "hasOffenders": false
- },
- "consoleMessages": {
- "tool": "phantomas",
- "label": "Console messages",
- "message": "<p>Try to keep your console clean when in production. Debugging is good for development only.</p><p>Writing in the console has a cost, especially when dumping large object variables.</p><p>There is also a problem with Internet Explorer 8, not knowing the console object.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 10,
- "isAbnormalThreshold": 25,
- "hasOffenders": false
- },
- "globalVariables": {
- "tool": "phantomas",
- "label": "Global variables",
- "message": "<p>It is a bad practice because they clutter up the global namespace. If two scripts use the same variable name in the global scope, it can cause conflicts and it is generally hard to debug.</p><p>Global variables also take a (very) little bit longer to be accessed than variables in the local scope of a function.</p>",
- "isOkThreshold": 10,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 200,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offendersHelpers.sortVarsLikeChromeDevTools(offenders)
- };
- }
- },
- "jQueryVersion": {
- "label": "jQuery version",
- "message": "<p>Current latest versions of jQuery are 1.11 (with support for old IE versions) and 2.1 (without).</p><p>Each new version of jQuery optimizes performances. Do not keep an old version of jQuery. Updating can sometimes break a few things, but it is generally quite easy to fix them up. So don't hesitate.</p>",
- "hasOffenders": false,
- "scoreFn": function(data) {
- var differentVersions = data.toolsResults.phantomas.metrics.jQueryVersionsLoaded;
- if (differentVersions === 0 || differentVersions > 1 || !data.toolsResults.phantomas.metrics.jQueryVersion) {
- // Not applicable
- return null;
- } else {
- var value = data.toolsResults.phantomas.metrics.jQueryVersion;
- var score;
- if (value.indexOf('1.11.') === 0 ||
- value.indexOf('1.12.') === 0 ||
- value.indexOf('2.1.') === 0 ||
- value.indexOf('2.2.') === 0 ||
- value.indexOf('3.0.') === 0) {
- score = 100;
- } else if (value.indexOf('1.10.') === 0 ||
- value.indexOf('2.0.') === 0) {
- score = 90;
- } else if (value.indexOf('1.9.') === 0) {
- score = 70;
- } else if (value.indexOf('1.8.') === 0) {
- score = 50;
- } else if (value.indexOf('1.7.') === 0) {
- score = 40;
- } else if (value.indexOf('1.6.') === 0) {
- score = 30;
- } else if (value.indexOf('1.5.') === 0) {
- score = 20;
- } else if (value.indexOf('1.4.') === 0) {
- score = 10;
- } else if (value.indexOf('1.3.') === 0) {
- score = 0;
- } else if (value.indexOf('1.2.') === 0) {
- score = 0;
- } else {
- debug('Unknown jQuery version "%s"', value);
- return null;
- }
- // Truncate version number (can be long sometimes, no clue why but it can...)
- if (value.length > 30) {
- value = value.substr(0, 28) + '...';
- }
- return {
- value: value,
- score: score,
- bad: value < 100,
- abnormal: false,
- abnormalityScore: 0
- };
- }
- }
- },
- "jQueryVersionsLoaded": {
- "tool": "phantomas",
- "label": "Several versions loaded",
- "message": "<p>jQuery is a heavy library. You should <b>never</b> load jQuery more than once on the same page.</p>",
- "isOkThreshold": 1,
- "isBadThreshold": 2,
- "isAbnormalThreshold": 2,
- "hasOffenders": true
- },
- "cssParsingErrors": {
- "tool": "phantomas",
- "label": "CSS syntax error",
- "message": "<p>Yellow Lab Tools failed to parse a CSS file. I doubt the problem comes from the css parser.</p><p>Maybe a <a href=\"http://jigsaw.w3.org/css-validator\" target=\"_blank\">CSS validator</a> can help you.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 1,
- "isAbnormalThreshold": 1,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^(?:(?:<([^ \(]*)>|\[inline CSS\]) ?)?(?:\((((?! @ ).)*)(?: @ (\d+):(\d+))?\))?$/.exec(offender);
- if (!parts) {
- debug('cssParsingErrors offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- error: parts[2],
- file: parts[1] || null,
- line: (parts[4] && parts[5]) ? parseInt(parts[4], 10) : null,
- column: (parts[4] && parts[5]) ? parseInt(parts[5], 10) : null
- };
- })
- };
- }
- },
- "cssRules": {
- "tool": "phantomas",
- "label": "Rules count",
- "message": "<p>Having a huge number of CSS rules hurts performances. If the number of CSS rules is higher than the number of DOM elements, there is clearly a problem.</p><p>Huge stylesheets generally occur when the different pages of a website load all the CSS, concatenated in a single stylesheet, even if a large part of the rules are page-specific. Solution is to create one main CSS file with global rules and one custom file per page.</p>",
- "isOkThreshold": 500,
- "isBadThreshold": 2500,
- "isAbnormalThreshold": 4000,
- "hasOffenders": false
- },
- "cssComplexSelectors": {
- "tool": "phantomas",
- "label": "Complex selectors",
- "message": "<p>Complex selectors are CSS selectors with 4 or more expressions, like \"#header ul li .foo\".</p><p>They are adding more work for the browser, and this could be avoided by simplifying selectors.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 500,
- "isAbnormalThreshold": 2000,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- return splittedOffender;
- })
- };
- }
- },
- "cssComplexSelectorsByAttribute": {
- "tool": "phantomas",
- "label": "Complex attributes selector",
- "message": "<p>Complex attributes selectors are one of these:<ul><li>.foo[type*=bar] (contains bar)</li><li>.foo[type^=bar] (starts with bar)</li><li>.foo[type|=bar] (starts with bar or bar-)</li><li>.foo[type$=bar] (ends with bar)</li><li>.foo[type~=bar baz] (bar or baz)</li></ul></p><p>Their matching process needs more CPU and it has a cost on performances.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 100,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- splittedOffender.bolded = splittedOffender.css.replace(/(\[[^ ]+[~\|\^\$\*]="[^"]+"\])/g, '<b>$1</b>');
- return splittedOffender;
- })
- };
- }
- },
- "cssColors": {
- "tool": "phantomas",
- "label": "Different colors",
- "message": "<p>This is the number of different colors defined in CSS.</p><p>Your CSS project will be easier to maintain if you keep a small color set.</p>",
- "isOkThreshold": 30,
- "isBadThreshold": 150,
- "isAbnormalThreshold": 400,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders, ruleObject) {
- var deduplicatedObj = {};
- offenders.map(function(offender) {
- var parts = /^([^ ]*) \((\d+) times\)$/.exec(offender);
- if (!parts) {
- debug('cssColors offenders transform function error with "%s"', offender);
- return;
- }
- var color = parts[1];
- var count = parseInt(parts[2], 10);
- deduplicatedObj[color] = (deduplicatedObj[color] || 0) + count;
- });
- var deduplicatedTable = [];
- for (var color in deduplicatedObj) {
- deduplicatedTable.push({
- color: color,
- occurrences: deduplicatedObj[color]
- });
- }
- deduplicatedTable.sort(function(a, b) {
- return b.occurrences - a.occurrences;
- });
- // Override rules.value
- ruleObject.value = deduplicatedTable.length;
- return {
- count: deduplicatedTable.length,
- palette: deduplicatedTable
- };
- }
- },
- "cssImports": {
- "tool": "phantomas",
- "label": "Uses of @import",
- "message": "<p>It’s bad for performance to use @import because CSS files don't get downloaded in parallel.</p><p>You should use <link rel='stylesheet' href='a.css'> instead.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 1,
- "isAbnormalThreshold": 1,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- return splittedOffender;
- })
- };
- }
- },
- "cssDuplicatedSelectors": {
- "tool": "phantomas",
- "label": "Duplicated selectors",
- "message": "<p>This is when two or more selectors are strictly identical and should be merged.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 40,
- "isAbnormalThreshold": 80,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^(.*) \((\d+) times\)$/.exec(offender);
- if (!parts) {
- debug('cssDuplicatedSelectors offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- rule: parts[1],
- occurrences: parseInt(parts[2], 10)
- };
- })
- };
- }
- },
- "cssDuplicatedProperties": {
- "tool": "phantomas",
- "label": "Duplicated properties",
- "message": "<p>This is the number of property definitions duplicated within a selector.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 100,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- var parts = /^([^{]+) {([^ ]+): (.+)}$/.exec(splittedOffender.css);
- if (!parts) {
- debug('cssDuplicatedProperties offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- property: parts[2],
- rule: parts[1],
- file: splittedOffender.file,
- line: splittedOffender.line,
- column: splittedOffender.column
- };
- })
- };
- }
- },
- "cssEmptyRules": {
- "tool": "phantomas",
- "label": "Empty rules",
- "message": "<p>Very easy to fix: remove all empty rules.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 40,
- "isAbnormalThreshold": 100,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- return splittedOffender;
- })
- };
- }
- },
- "cssExpressions": {
- "tool": "phantomas",
- "label": "CSS expressions",
- "message": "<p>Such as: expression( document.body.clientWidth > 600 ? \"600px\" : \"auto\" )</p><p>This is a bad practice as it slows down browsers. There are some simpler CSS3 methods for doing this.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 1,
- "isAbnormalThreshold": 20,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- var parts = /^(.*) {([^ ]+): expression\((.*)\)}$/.exec(splittedOffender.css);
- if (!parts) {
- debug('cssExpressions offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- rule: parts[1],
- property: parts[2],
- expression: parts[3],
- file: splittedOffender.file,
- line: splittedOffender.line,
- column: splittedOffender.column
- };
- })
- };
- }
- },
- "cssImportants": {
- "tool": "phantomas",
- "label": "Uses of !important",
- "message": "<p>It can be useful, but only as a last resort. It is a bad practice because it overrides the normal cascading logic. The more you use !important, the more you need it again to over-override. This conducts to a poor maintainability.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 150,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- var parts = /^(.*) {([^ ]+): (.*) ?\!important}$/.exec(splittedOffender.css);
- if (!parts) {
- debug('cssImportants offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- rule: parts[1],
- property: parts[2],
- value: parts[3],
- file: splittedOffender.file,
- line: splittedOffender.line,
- column: splittedOffender.column
- };
- })
- };
- }
- },
- "cssOldIEFixes": {
- "tool": "phantomas",
- "label": "Old IE fixes",
- "message": "<p>What browser do you need to support? Once you've got the answer, take a look at these old rules that pollute your CSS code and remove them.</p><p>IE6:<ul><li>* html</li><li>html > body (everything but IE6)</li></ul><p><p>IE7:<ul><li><b>*</b>height: 123px;</li><li>height: 123px <b>!ie</b>;</li></ul><p><p>IE9:<ul><li>-ms-filter</li><li>progid:DXImageTransform.Microsoft</li></ul></p>",
- "isOkThreshold": 0,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 300,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- var parts = /^([^{]*)( {([^ ]+): (.*)})?$/.exec(splittedOffender.css);
- if (!parts) {
- debug('cssOldIEFixes offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- var rule = parts[1];
- var property = parts[3];
- var value = parts[4];
- var browser = null;
- if (rule.indexOf('* html') === 0) {
- rule = rule.replace(/^\* html/, '<b>* html</b>');
- browser = 'IE6';
- } else if (rule.indexOf('html>body') === 0) {
- rule = rule.replace(/^html>body/, '<b>html>body</b>');
- browser = 'IE6';
- } else if (property.indexOf('*') === 0) {
- property = '<b>' + property + '</b>';
- browser = 'IE7';
- } else if (value.match(/\!ie$/)) {
- value = value.replace(/\!ie$/, '<b>!ie</b>');
- browser = 'IE7';
- } else if (property === '-ms-filter') {
- property = '<b>-ms-filter</b>';
- browser = 'IE9';
- } else if (value.indexOf('progid:DXImageTransform.Microsoft') >= 0) {
- value = value.replace(/progid:DXImageTransform\.Microsoft/, '<b>progid:DXImageTransform.Microsoft</b>');
- browser = 'IE9';
- }
- var propertyAndValue = (property && value) ? ' {' + property + ': ' + value + '}' : '';
- splittedOffender.bolded = rule + propertyAndValue;
- splittedOffender.browser = browser;
- return splittedOffender;
- })
- };
- }
- },
- "cssOldPropertyPrefixes": {
- "tool": "phantomas",
- "label": "Old prefixes",
- "message": "<p>Many property prefixes such as -moz- or -webkit- are not needed anymore, or by very few people. You can remove them or replace them with the non-prefixed version. This will help reducing your stylesheets weight.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 300,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- var parts = /^([^{]*)(?: ?{ ?([^ ]+): (.*) ?}) \/\/ (.*)$/.exec(splittedOffender.css);
- if (!parts) {
- debug('cssOldPropertyPrefixes offenders transform function error with "%s"', offender);
- return {
- parseError: offender
- };
- }
- return {
- rule: parts[1],
- property: parts[2],
- value: parts[3],
- message: parts[4],
- file: splittedOffender.file,
- line: splittedOffender.line,
- column: splittedOffender.column
- };
- })
- };
- }
- },
- "cssUniversalSelectors": {
- "tool": "phantomas",
- "label": "Universal selectors",
- "message": "<p>Universal selectors are the most expensive CSS selectors.</p><p>More informations <a href=\"http://perfectionkills.com/profiling-css-for-fun-and-profit-optimization-notes/\" target=\"_blank\">here</a>.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 40,
- "isAbnormalThreshold": 150,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- return splittedOffender;
- })
- };
- }
- },
- "cssRedundantBodySelectors": {
- "tool": "phantomas",
- "label": "Redundant body selectors",
- "message": "<p>This is one way to remove complexity from a CSS rule. Generally, when \"body\" is specified in a rule it can be removed, because an element is necessarily inside the body.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 200,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- splittedOffender.bolded = splittedOffender.css.replace(/body/, '<b>body</b>');
- return splittedOffender;
- })
- };
- }
- },
- "cssRedundantChildNodesSelectors": {
- "tool": "phantomas",
- "label": "Redundant tags selectors",
- "message": "<p>Some tags included inside other tags are obvious. For example, when \"ul li\" is specified in a rule, \"ul\" can be removed because the \"li\" element is <b>always</b> inside a \"ul\". Same thing for \"tr td\", \"select option\", ...</p><p>Lowering compexity in CSS selectors can make the page load a little faster.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 50,
- "isAbnormalThreshold": 200,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var splittedOffender = offendersHelpers.cssOffenderPattern(offender);
- var rule = splittedOffender.css || '';
- var redundanters = [
- ['ul', 'li'],
- ['ol', 'li'],
- ['select', 'option'],
- ['table', 'tr'],
- ['table', 'th'],
- ];
- redundanters.forEach(function(couple) {
- rule = rule.replace(new RegExp('(^| |>)' + couple[0] + '([^ >]*)?([ >]| > )' + couple[1] + '([^\\w-]|$)', 'g'), '$1<b>' + couple[0] + '</b>$2$3<b>' + couple[1] + '</b>$4');
- });
- splittedOffender.bolded = rule;
- return splittedOffender;
- })
- };
- }
- },
- "requests": {
- "tool": "phantomas",
- "label": "Total requests number",
- "message": "<p>This is one of the most important performance rule. Every request is slowing down the page loading.</p><p>There are several technics to reduce their number:<ul><li>Concatenate JS files</li><li>Concatenate CSS files</li><li>Embed or inline small JS or CSS files in the HTML</li><li>Create sprites or icon fonts</li><li>Base64 encode small images in HTML or stylesheets</li><li>Use lazyloading for images</li></ul></p>",
- "isOkThreshold": 15,
- "isBadThreshold": 100,
- "isAbnormalThreshold": 200,
- "hasOffenders": true,
- "takeOffendersFrom": ["htmlCount", "jsCount", "cssCount", "imageCount", "webfontCount", "videoCount", "jsonCount", "jsonCount"],
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders
- .map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- }).sort(function(a, b) {
- return b.size - a.size;
- })
- };
- }
- },
- "htmlCount": {
- "tool": "phantomas",
- "label": "Document count",
- "message": "<p>The number of HTML pages requests, HTML fragments or iframes.</p>",
- "isOkThreshold": 10,
- "isBadThreshold": 20,
- "isAbnormalThreshold": 30,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "jsCount": {
- "tool": "phantomas",
- "label": "Script count",
- "message": "<p>Reduce the number of scripts by concatenating them.</p>",
- "isOkThreshold": 5,
- "isBadThreshold": 15,
- "isAbnormalThreshold": 30,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "cssCount": {
- "tool": "phantomas",
- "label": "CSS count",
- "message": "<p>Reduce the number of stylesheets by concatenating them.</p>",
- "isOkThreshold": 3,
- "isBadThreshold": 10,
- "isAbnormalThreshold": 22,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "imageCount": {
- "tool": "phantomas",
- "label": "Image count",
- "message": "<p>Reduce the number of images by lazyloading them, by spriting them or by creating an icons font.</p>",
- "isOkThreshold": 15,
- "isBadThreshold": 40,
- "isAbnormalThreshold": 70,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "webfontCount": {
- "tool": "phantomas",
- "label": "Font count",
- "message": "<p>Fonts are loaded on the critical path of the head. Load as few as possible.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 3,
- "isAbnormalThreshold": 5,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "videoCount": {
- "tool": "phantomas",
- "label": "Video count",
- "message": "<p>The number of videos loaded.</p>",
- "isOkThreshold": 1,
- "isBadThreshold": 5,
- "isAbnormalThreshold": 15,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "jsonCount": {
- "tool": "phantomas",
- "label": "JSON count",
- "message": "<p>The number of AJAX requests to JSON files or webservices.</p>",
- "isOkThreshold": 2,
- "isBadThreshold": 10,
- "isAbnormalThreshold": 25,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "otherCount": {
- "tool": "phantomas",
- "label": "Other types of requests",
- "message": "<p>They can be Flash, XML, music or any unknown format.</p>",
- "isOkThreshold": 5,
- "isBadThreshold": 20,
- "isAbnormalThreshold": 40,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "smallJsFiles": {
- "tool": "phantomas",
- "label": "Small JS files",
- "message": "<p>Number of JS assets smaller than 2 KB that could probably be inlined or merged.</p>",
- "isOkThreshold": 2,
- "isBadThreshold": 10,
- "isAbnormalThreshold": 16,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "smallCssFiles": {
- "tool": "phantomas",
- "label": "Small CSS files",
- "message": "<p>Number of CSS assets smaller than 2 KB that could probably be inlined or merged.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 8,
- "isAbnormalThreshold": 12,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "smallImages": {
- "tool": "phantomas",
- "label": "Small images",
- "message": "<p>Images smaller than 2 KB that could be base64 encoded or merged into a sprite.</p>",
- "isOkThreshold": 2,
- "isBadThreshold": 17,
- "isAbnormalThreshold": 30,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- return offendersHelpers.fileWithSizePattern(offender);
- })
- };
- }
- },
- "notFound": {
- "tool": "phantomas",
- "label": "404 not found",
- "message": "<p>404 errors are never cached, so each time a page ask for it, it hits the server. Even if it is behind a CDN or a reverse-proxy cache.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 1,
- "isAbnormalThreshold": 1,
- "hasOffenders": true
- },
- "closedConnections": {
- "tool": "phantomas",
- "label": "Connections closed",
- "message": "<p>This counts the number of requests not keeping the connection alive (specifying \"Connection: close\" in the response headers). It is only counting a request if it is followed by another request on the same domain.</p><p>This is slowing down the next request, because the brower needs to open a new connection to the server, which means an additional round-trip.</p><p>Correct the problem by setting a Keep-Alive header on the guilty server.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 8,
- "isAbnormalThreshold": 20,
- "hasOffenders": true
- },
- "multipleRequests": {
- "tool": "phantomas",
- "label": "Duplicated requests",
- "message": "<p>This only happens when the asset has no cache and is requested more than once on the same page. Be very careful about it.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 3,
- "isAbnormalThreshold": 10,
- "hasOffenders": true
- },
- "cachingDisabled": {
- "tool": "phantomas",
- "label": "Caching disabled",
- "message": "<p>Counts responses with caching disabled (max-age=0)</p><p>Fix immediatly if on static assets.</p>",
- "isOkThreshold": 0,
- "isBadThreshold": 12,
- "isAbnormalThreshold": 25,
- "hasOffenders": true
- },
- "cachingNotSpecified": {
- "tool": "phantomas",
- "label": "Caching not specified",
- "message": "<p>When no caching is specified, each browser will handle it differently. Most of the time, it will automatically add a cache for you, but a poor one. You'd better handle it yourself.</p>",
- "isOkThreshold": 5,
- "isBadThreshold": 20,
- "isAbnormalThreshold": 40,
- "hasOffenders": true
- },
- "cachingTooShort": {
- "tool": "phantomas",
- "label": "Caching too short",
- "message": "<p>Responses with too short caching time (less than a week).</p><p>The longer you cache, the better. Add versionning to your static assets, if it's not already done, and set their cache time to one year.</p>",
- "isOkThreshold": 5,
- "isBadThreshold": 20,
- "isAbnormalThreshold": 40,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders
- .map(function(offender) {
- var parts = /^([^ ]*) cached for (-?\d+(\.\d+)?) s$/.exec(offender);
- if (!parts) {
- debug('cachingTooShort offenders transform function error with "%s"', offender);
- return {
- file: offender
- };
- }
- return {
- file: parts[1],
- ttl: Math.round(parseFloat(parts[2]))
- };
- }).sort(function(a, b) {
- return a.ttl - b.ttl;
- }).map(function(obj) {
- var duration = obj.ttl;
- var unit = 'seconds';
- if (duration >= 120) {
- duration = Math.round(duration / 60);
- unit = 'minutes';
- }
- if (duration >= 120) {
- duration = Math.round(duration / 60);
- unit = 'hours';
- }
- if (duration >= 48) {
- duration = Math.round(duration / 24);
- unit = 'days';
- }
- obj.ttlWithUnit = duration;
- obj.unit = unit;
- return obj;
- })
- };
- }
- },
- "domains": {
- "tool": "phantomas",
- "label": "Different domains",
- "message": "<p>For each domain met, the browser needs to make a DNS look-up, which is slow. Avoid having to many different domains and the page should render faster.</p><p>By the way, domain sharding is not a good practice anymore.</p>",
- "isOkThreshold": 10,
- "isBadThreshold": 25,
- "isAbnormalThreshold": 50,
- "hasOffenders": true,
- "offendersTransformFn": function(offenders) {
- return {
- count: offenders.length,
- list: offenders.map(function(offender) {
- var parts = /^([^ ]*): (\d+) request\(s\)$/.exec(offender);
- if (!parts) {
- debug('domains offenders transform function error with "%s"', offender);
- return {
- file: offender
- };
- }
- return {
- domain: parts[1],
- requests: parseInt(parts[2])
- };
- })
- };
- }
- }
- };
- module.exports = policies;
|