-
Notifications
You must be signed in to change notification settings - Fork 383
/
rules.js
1441 lines (1285 loc) · 42.9 KB
/
rules.js
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global YSLOW*/
/*jslint white: true, onevar: true, undef: true, nomen: true, regexp: true, continue: true, plusplus: true, bitwise: true, newcap: true, type: true, unparam: true, maxerr: 50, indent: 4*/
/**
*
* Example of a rule object:
*
* <pre>
* YSLOW.registerRule({
*
* id: 'myrule',
* name: 'Never say never',
* url: 'http://never.never/never.html',
* info: 'Short description of the rule',
*
* config: {
* when: 'ever'
* },
*
* lint: function(doc, components, config) {
* return {
* score: 100,
* message: "Did you just say never?",
* components: []
* };
* }
* });
</pre>
*/
//
// 3/2/2009
// Centralize all name and info of builtin tool to YSLOW.doc class.
//
YSLOW.registerRule({
id: 'ynumreq',
//name: 'Make fewer HTTP requests',
url: 'http://developer.yahoo.com/performance/rules.html#num_http',
category: ['content'],
config: {
max_js: 3,
// the number of scripts allowed before we start penalizing
points_js: 4,
// penalty points for each script over the maximum
max_css: 2,
// number of external stylesheets allowed before we start penalizing
points_css: 4,
// penalty points for each external stylesheet over the maximum
max_cssimages: 6,
// // number of background images allowed before we start penalizing
points_cssimages: 3 // penalty points for each bg image over the maximum
},
lint: function (doc, cset, config) {
var js = cset.getComponentsByType('js').length - config.max_js,
css = cset.getComponentsByType('css').length - config.max_css,
cssimg = cset.getComponentsByType('cssimage').length - config.max_cssimages,
score = 100,
messages = [];
if (js > 0) {
score -= js * config.points_js;
messages[messages.length] = 'This page has ' + YSLOW.util.plural('%num% external Javascript script%s%', (js + config.max_js)) + '. Try combining them into one.';
}
if (css > 0) {
score -= css * config.points_css;
messages[messages.length] = 'This page has ' + YSLOW.util.plural('%num% external stylesheet%s%', (css + config.max_css)) + '. Try combining them into one.';
}
if (cssimg > 0) {
score -= cssimg * config.points_cssimages;
messages[messages.length] = 'This page has ' + YSLOW.util.plural('%num% external background image%s%', (cssimg + config.max_cssimages)) + '. Try combining them with CSS sprites.';
}
return {
score: score,
message: messages.join('\n'),
components: []
};
}
});
YSLOW.registerRule({
id: 'ycdn',
//name: 'Use a CDN',
url: 'http://developer.yahoo.com/performance/rules.html#cdn',
category: ['server'],
config: {
// how many points to take out for each component not on CDN
points: 10,
// array of regexps that match CDN-ed components
patterns: [
'^([^\\.]*)\\.([^\\.]*)\\.yimg\\.com/[^/]*\\.yimg\\.com/.*$',
'^([^\\.]*)\\.([^\\.]*)\\.yimg\\.com/[^/]*\\.yahoo\\.com/.*$',
'^sec.yimg.com/',
'^a248.e.akamai.net',
'^[dehlps].yimg.com',
'^(ads|cn|mail|maps|s1).yimg.com',
'^[\\d\\w\\.]+.yimg.com',
'^a.l.yimg.com',
'^us.(js|a)2.yimg.com',
'^yui.yahooapis.com',
'^adz.kr.yahoo.com',
'^img.yahoo.co.kr',
'^img.(shopping|news|srch).yahoo.co.kr',
'^pimg.kr.yahoo.com',
'^kr.img.n2o.yahoo.com',
'^s3.amazonaws.com',
'^(www.)?google-analytics.com',
'.cloudfront.net', //Amazon CloudFront
'.ak.fbcdn.net', //Facebook images ebeded
'platform.twitter.com', //Twitter widget - Always via a CDN
'cdn.api.twitter.com', //Twitter API calls, served via Akamai
'apis.google.com', //Google's API Hosting
'.akamaihd.net' //Akamai - Facebook uses this for SSL assets
],
// array of regexps that will be treated as exception.
exceptions: [
'^chart.yahoo.com',
'^(a1|f3|f5|f3c|f5c).yahoofs.com', // Images for 360 and YMDB
'^us.(a1c|f3).yahoofs.com' // Personals photos
],
// array of regexps that match CDN Server HTTP headers
servers: [
'cloudflare-nginx' // not using ^ and $ due to invisible
],
// which component types should be on CDN
types: ['js', 'css', 'image', 'cssimage', 'flash', 'favicon']
},
lint: function (doc, cset, config) {
var i, j, url, re, match, hostname,
offender, len, lenJ, comp, patterns, headers,
score = 100,
offenders = [],
exceptions = [],
message = '',
util = YSLOW.util,
plural = util.plural,
kbSize = util.kbSize,
getHostname = util.getHostname,
docDomain = getHostname(cset.doc_comp.url),
comps = cset.getComponentsByType(config.types),
userCdns = util.Preference.getPref('cdnHostnames', ''),
hasPref = util.Preference.nativePref;
// array of custom cdns
if (userCdns) {
userCdns = userCdns.split(',');
}
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
url = comp.url;
hostname = getHostname(url);
headers = comp.headers;
// ignore /favicon.ico
if (comp.type === 'favicon' && hostname === docDomain) {
continue;
}
// experimental custom header, waiting for specification
match = headers['X-CDN'] || headers['X-Cdn'] || headers['X-cdn'];
if (match) {
continue;
}
// by hostname
patterns = config.patterns;
for (j = 0, lenJ = patterns.length; j < lenJ; j += 1) {
re = new RegExp(patterns[j]);
if (re.test(hostname)) {
match = 1;
break;
}
}
// by custom hostnames
if (userCdns) {
for (j = 0, lenJ = userCdns.length; j < lenJ; j += 1) {
re = new RegExp(util.trim(userCdns[j]));
if (re.test(hostname)) {
match = 1;
break;
}
}
}
if (!match) {
// by Server HTTP header
patterns = config.servers;
for (j = 0, lenJ = patterns.length; j < lenJ; j += 1) {
re = new RegExp(patterns[j]);
if (re.test(headers.Server)) {
match = 1;
break;
}
}
if (!match) {
// by exception
patterns = config.exceptions;
for (j = 0, lenJ = patterns.length; j < lenJ; j += 1) {
re = new RegExp(patterns[j]);
if (re.test(hostname)) {
exceptions.push(comp);
match = 1;
break;
}
}
if (!match) {
offenders.push(comp);
}
}
}
}
score -= offenders.length * config.points;
offenders.concat(exceptions);
if (offenders.length > 0) {
message = plural('There %are% %num% static component%s% ' +
'that %are% not on CDN. ', offenders.length);
}
if (exceptions.length > 0) {
message += plural('There %are% %num% component%s% that %are% not ' +
'on CDN, but %are% exceptions:', exceptions.length) + '<ul>';
for (i = 0, len = offenders.length; i < len; i += 1) {
message += '<li>' + util.prettyAnchor(exceptions[i].url,
exceptions[i].url, null, true, 120, null,
exceptions[i].type) + '</li>';
}
message += '</ul>';
}
if (userCdns) {
message += '<p>Using these CDN hostnames from your preferences: ' +
userCdns + '</p>';
} else {
message += '<p>You can specify CDN hostnames in your ' +
'preferences. See <a href="javascript:document.ysview.' +
'openLink(\'http://developer.yahoo.com/yslow/faq.html#' +
'faq_cdn\')">YSlow FAQ</a> for details.</p>';
}
// list unique domains only to avoid long list of offenders
if (offenders.length) {
offenders = util.summaryByDomain(offenders,
['size', 'size_compressed'], true);
for (i = 0, len = offenders.length; i < len; i += 1) {
offender = offenders[i];
offenders[i] = offender.domain + ': ' +
plural('%num% component%s%, ', offender.count) +
kbSize(offender.sum_size) + (
offender.sum_size_compressed > 0 ? ' (' +
kbSize(offender.sum_size_compressed) + ' GZip)' : ''
) + (hasPref ? (
' <button onclick="javascript:document.ysview.addCDN(\'' +
offender.domain + '\')">Add as CDN</button>') : '');
}
}
return {
score: score,
message: message,
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yexpires',
//name: 'Add an Expires header',
url: 'http://developer.yahoo.com/performance/rules.html#expires',
category: ['server'],
config: {
// how many points to take for each component without Expires header
points: 11,
// 2 days = 2 * 24 * 60 * 60 seconds, how far is far enough
howfar: 172800,
// component types to be inspected for expires headers
types: ['css', 'js', 'image', 'cssimage', 'flash', 'favicon']
},
lint: function (doc, cset, config) {
var ts, i, expiration, score, len,
// far-ness in milliseconds
far = parseInt(config.howfar, 10) * 1000,
offenders = [],
comps = cset.getComponentsByType(config.types);
for (i = 0, len = comps.length; i < len; i += 1) {
expiration = comps[i].expires;
if (typeof expiration === 'object' &&
typeof expiration.getTime === 'function') {
// looks like a Date object
ts = new Date().getTime();
if (expiration.getTime() > ts + far) {
continue;
}
}
offenders.push(comps[i]);
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% static component%s%',
offenders.length
) + ' without a far-future expiration date.' : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'ycompress',
//name: 'Compress components',
url: 'http://developer.yahoo.com/performance/rules.html#gzip',
category: ['server'],
config: {
// files below this size are exceptions of the gzip rule
min_filesize: 500,
// file types to inspect
types: ['doc', 'iframe', 'xhr', 'js', 'css'],
// points to take out for each non-compressed component
points: 11
},
lint: function (doc, cset, config) {
var i, len, score, comp,
offenders = [],
comps = cset.getComponentsByType(config.types);
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
if (comp.compressed || comp.size < 500) {
continue;
}
offenders.push(comp);
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% plain text component%s%',
offenders.length
) + ' that should be sent compressed' : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'ycsstop',
//name: 'Put CSS at the top',
url: 'http://developer.yahoo.com/performance/rules.html#css_top',
category: ['css'],
config: {
points: 10
},
lint: function (doc, cset, config) {
var i, len, score, comp,
comps = cset.getComponentsByType('css'),
offenders = [];
// expose all offenders
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
if (comp.containerNode === 'body') {
offenders.push(comp);
}
}
score = 100;
if (offenders.length > 0) {
// start at 99 so each ding drops us a grade
score -= 1 + offenders.length * parseInt(config.points, 10);
}
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% stylesheet%s%',
offenders.length
) + ' found in the body of the document' : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yjsbottom',
//name: 'Put Javascript at the bottom',
url: 'http://developer.yahoo.com/performance/rules.html#js_bottom',
category: ['javascript'],
config: {
points: 5 // how many points for each script in the <head>
},
lint: function (doc, cset, config) {
var i, len, comp, score,
offenders = [],
comps = cset.getComponentsByType('js');
// offenders are components not injected (tag found on document payload)
// except if they have either defer or async attributes
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
if (comp.containerNode === 'head' &&
!comp.injected && (!comp.defer || !comp.async)) {
offenders.push(comp);
}
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ?
YSLOW.util.plural(
'There %are% %num% JavaScript script%s%',
offenders.length
) + ' found in the head of the document' : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yexpressions',
//name: 'Avoid CSS expressions',
url: 'http://developer.yahoo.com/performance/rules.html#css_expressions',
category: ['css'],
config: {
points: 2 // how many points for each expression
},
lint: function (doc, cset, config) {
var i, len, expr_count, comp,
instyles = (cset.inline && cset.inline.styles) || [],
comps = cset.getComponentsByType('css'),
offenders = [],
score = 100,
total = 0;
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
if (typeof comp.expr_count === 'undefined') {
expr_count = YSLOW.util.countExpressions(comp.body);
comp.expr_count = expr_count;
} else {
expr_count = comp.expr_count;
}
// offence
if (expr_count > 0) {
comp.yexpressions = YSLOW.util.plural(
'%num% expression%s%',
expr_count
);
total += expr_count;
offenders.push(comp);
}
}
for (i = 0, len = instyles.length; i < len; i += 1) {
expr_count = YSLOW.util.countExpressions(instyles[i].body);
if (expr_count > 0) {
offenders.push('inline <style> tag #' + (i + 1) + ' (' +
YSLOW.util.plural(
'%num% expression%s%',
expr_count
) + ')'
);
total += expr_count;
}
}
if (total > 0) {
score = 90 - total * config.points;
}
return {
score: score,
message: total > 0 ? 'There are a total of ' +
YSLOW.util.plural('%num% expression%s%', total) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yexternal',
//name: 'Make JS and CSS external',
url: 'http://developer.yahoo.com/performance/rules.html#external',
category: ['javascript', 'css'],
config: {},
lint: function (doc, cset, config) {
var message,
inline = cset.inline,
styles = (inline && inline.styles) || [],
scripts = (inline && inline.scripts) || [],
offenders = [];
if (styles.length) {
message = YSLOW.util.plural(
'There are a total of %num% inline css',
styles.length
);
offenders.push(message);
}
if (scripts.length) {
message = YSLOW.util.plural(
'There are a total of %num% inline script%s%',
scripts.length
);
offenders.push(message);
}
return {
score: 'n/a',
message: 'Only consider this if your property is a common user home page.',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'ydns',
//name: 'Reduce DNS lookups',
url: 'http://developer.yahoo.com/performance/rules.html#dns_lookups',
category: ['content'],
config: {
// maximum allowed domains, excluding ports and IP addresses
max_domains: 4,
// the cost of each additional domain over the maximum
points: 5
},
lint: function (doc, cset, config) {
var i, len, domain,
util = YSLOW.util,
kbSize = util.kbSize,
plural = util.plural,
score = 100,
domains = util.summaryByDomain(cset.components,
['size', 'size_compressed'], true);
if (domains.length > config.max_domains) {
score -= (domains.length - config.max_domains) * config.points;
}
// list unique domains only to avoid long list of offenders
if (domains.length) {
for (i = 0, len = domains.length; i < len; i += 1) {
domain = domains[i];
domains[i] = domain.domain + ': ' +
plural('%num% component%s%, ', domain.count) +
kbSize(domain.sum_size) + (
domain.sum_size_compressed > 0 ? ' (' +
kbSize(domain.sum_size_compressed) + ' GZip)' : ''
);
}
}
return {
score: score,
message: (domains.length > config.max_domains) ? plural(
'The components are split over more than %num% domain%s%',
config.max_domains
) : '',
components: domains
};
}
});
YSLOW.registerRule({
id: 'yminify',
//name: 'Minify JS and CSS',
url: 'http://developer.yahoo.com/performance/rules.html#minify',
category: ['javascript', 'css'],
config: {
// penalty for each unminified component
points: 10,
// types of components to inspect for minification
types: ['js', 'css']
},
lint: function (doc, cset, config) {
var i, len, score, minified, comp,
inline = cset.inline,
styles = (inline && inline.styles) || [],
scripts = (inline && inline.scripts) || [],
comps = cset.getComponentsByType(config.types),
offenders = [];
// check all peeled components
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
// set/get minified flag
if (typeof comp.minified === 'undefined') {
minified = YSLOW.util.isMinified(comp.body);
comp.minified = minified;
} else {
minified = comp.minified;
}
if (!minified) {
offenders.push(comp);
}
}
// check inline scripts/styles/whatever
for (i = 0, len = styles.length; i < len; i += 1) {
if (!YSLOW.util.isMinified(styles[i].body)) {
offenders.push('inline <style> tag #' + (i + 1));
}
}
for (i = 0, len = scripts.length; i < len; i += 1) {
if (!YSLOW.util.isMinified(scripts[i].body)) {
offenders.push('inline <script> tag #' + (i + 1));
}
}
score = 100 - offenders.length * config.points;
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural('There %are% %num% component%s% that can be minified', offenders.length) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yredirects',
//name: 'Avoid redirects',
url: 'http://developer.yahoo.com/performance/rules.html#redirects',
category: ['content'],
config: {
points: 10 // the penalty for each redirect
},
lint: function (doc, cset, config) {
var i, len, comp, score,
offenders = [],
briefUrl = YSLOW.util.briefUrl,
comps = cset.getComponentsByType('redirect');
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
offenders.push(briefUrl(comp.url, 80) + ' redirects to ' +
briefUrl(comp.headers.Location, 60));
}
score = 100 - comps.length * parseInt(config.points, 10);
return {
score: score,
message: (comps.length > 0) ? YSLOW.util.plural(
'There %are% %num% redirect%s%',
comps.length
) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'ydupes',
//name: 'Remove duplicate JS and CSS',
url: 'http://developer.yahoo.com/performance/rules.html#js_dupes',
category: ['javascript', 'css'],
config: {
// penalty for each duplicate
points: 5,
// component types to check for duplicates
types: ['js', 'css']
},
lint: function (doc, cset, config) {
var i, url, score, len,
hash = {},
offenders = [],
comps = cset.getComponentsByType(config.types);
for (i = 0, len = comps.length; i < len; i += 1) {
url = comps[i].url;
if (typeof hash[url] === 'undefined') {
hash[url] = {
count: 1,
compindex: i
};
} else {
hash[url].count += 1;
}
}
for (i in hash) {
if (hash.hasOwnProperty(i) && hash[i].count > 1) {
offenders.push(comps[hash[i].compindex]);
}
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% duplicate component%s%',
offenders.length
) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yetags',
//name: 'Configure ETags',
url: 'http://developer.yahoo.com/performance/rules.html#etags',
category: ['server'],
config: {
// points to take out for each misconfigured etag
points: 11,
// types to inspect for etags
types: ['flash', 'js', 'css', 'cssimage', 'image', 'favicon']
},
lint: function (doc, cset, config) {
var i, len, score, comp, etag, headers,
offenders = [],
comps = cset.getComponentsByType(config.types);
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
headers = comp.headers;
etag = headers && (headers.ETag || headers.Etag);
if (etag && !YSLOW.util.isETagGood(etag)) {
offenders.push(comp);
}
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% component%s% with misconfigured ETags',
offenders.length
) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yxhr',
//name: 'Make Ajax cacheable',
url: 'http://developer.yahoo.com/performance/rules.html#cacheajax',
category: ['content'],
config: {
// points to take out for each non-cached XHR
points: 5,
// at least an hour in cache.
min_cache_time: 3600
},
lint: function (doc, cset, config) {
var i, expiration, ts, score, cache_control,
// far-ness in milliseconds
min = parseInt(config.min_cache_time, 10) * 1000,
offenders = [],
comps = cset.getComponentsByType('xhr');
for (i = 0; i < comps.length; i += 1) {
// check for cache-control: no-cache and cache-control: no-store
cache_control = comps[i].headers['Cache-Control'];
if (cache_control) {
if (cache_control.indexOf('no-cache') !== -1 ||
cache_control.indexOf('no-store') !== -1) {
continue;
}
}
expiration = comps[i].expires;
if (typeof expiration === 'object' &&
typeof expiration.getTime === 'function') {
// looks like a Date object
ts = new Date().getTime();
if (expiration.getTime() > ts + min) {
continue;
}
// expires less than min_cache_time => BAD.
}
offenders.push(comps[i]);
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% XHR component%s% that %are% not cacheable',
offenders.length
) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'yxhrmethod',
//name: 'Use GET for AJAX Requests',
url: 'http://developer.yahoo.com/performance/rules.html#ajax_get',
category: ['server'],
config: {
// points to take out for each ajax request
// that uses http method other than GET.
points: 5
},
lint: function (doc, cset, config) {
var i, score,
offenders = [],
comps = cset.getComponentsByType('xhr');
for (i = 0; i < comps.length; i += 1) {
if (typeof comps[i].method === 'string') {
if (comps[i].method !== 'GET' && comps[i].method !== 'unknown') {
offenders.push(comps[i]);
}
}
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% XHR component%s% that %do% not use GET HTTP method',
offenders.length
) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'ymindom',
//name: 'Reduce the Number of DOM Elements',
url: 'http://developer.yahoo.com/performance/rules.html#min_dom',
category: ['content'],
config: {
// the range
range: 250,
// points to take out for each range of DOM that's more than max.
points: 10,
// number of DOM elements are considered too many if exceeds maxdom.
maxdom: 900
},
lint: function (doc, cset, config) {
var numdom = cset.domElementsCount,
score = 100;
if (numdom > config.maxdom) {
score = 99 - Math.ceil((numdom - parseInt(config.maxdom, 10)) /
parseInt(config.range, 10)) * parseInt(config.points, 10);
}
return {
score: score,
message: (numdom > config.maxdom) ? YSLOW.util.plural(
'There %are% %num% DOM element%s% on the page',
numdom
) : '',
components: []
};
}
});
YSLOW.registerRule({
id: 'yno404',
//name: 'No 404s',
url: 'http://developer.yahoo.com/performance/rules.html#no404',
category: ['content'],
config: {
// points to take out for each 404 response.
points: 5,
// component types to be inspected for expires headers
types: ['css', 'js', 'image', 'cssimage', 'flash', 'xhr', 'favicon']
},
lint: function (doc, cset, config) {
var i, len, comp, score,
offenders = [],
comps = cset.getComponentsByType(config.types);
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
if (parseInt(comp.status, 10) === 404) {
offenders.push(comp);
}
}
score = 100 - offenders.length * parseInt(config.points, 10);
return {
score: score,
message: (offenders.length > 0) ? YSLOW.util.plural(
'There %are% %num% request%s% that %are% 404 Not Found',
offenders.length
) : '',
components: offenders
};
}
});
YSLOW.registerRule({
id: 'ymincookie',
//name: 'Reduce Cookie Size',
url: 'http://developer.yahoo.com/performance/rules.html#cookie_size',
category: ['cookie'],
config: {
// points to take out if cookie size is more than config.max_cookie_size
points: 10,
// 1000 bytes.
max_cookie_size: 1000
},
lint: function (doc, cset, config) {
var n,
cookies = cset.cookies,
cookieSize = (cookies && cookies.length) || 0,
message = '',
score = 100;
if (cookieSize > config.max_cookie_size) {
n = Math.floor(cookieSize / config.max_cookie_size);
score -= 1 + n * parseInt(config.points, 10);
message = YSLOW.util.plural(
'There %are% %num% byte%s% of cookies on this page',
cookieSize
);
}
return {
score: score,
message: message,
components: []
};
}
});
YSLOW.registerRule({
id: 'ycookiefree',
//name: 'Use Cookie-free Domains',
url: 'http://developer.yahoo.com/performance/rules.html#cookie_free',
category: ['cookie'],
config: {
// points to take out for each component that send cookie.
points: 5,
// which component types should be cookie-free
types: ['js', 'css', 'image', 'cssimage', 'flash', 'favicon']
},
lint: function (doc, cset, config) {
var i, len, score, comp, cookie,
offenders = [],
getHostname = YSLOW.util.getHostname,
docDomain = getHostname(cset.doc_comp.url),
comps = cset.getComponentsByType(config.types);
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
// ignore /favicon.ico
if (comp.type === 'favicon' &&