(This is part 3. See part one and part two.)

(这是第3部分。请参见第1部分和第2部分。)

There are two concepts to remember when working on your YSlow extensions and customizations:

在处理YSlow扩展和自定义项时,要记住两个概念:

  • rules (or "recommendations" if you will, or "best practices" or simply "lint checks"), and

    规则(如果可以的话,也可以是“建议”,或者是“最佳做法”,或者仅仅是“皮棉检查”),以及

  • rulesets which are lists of rules

    规则集是规则列表

An example rule is "Reduce HTTP requests". An example ruleset is "Small site or blog" (which is less strict than the default ruleset, because it assumes a small site has no CDN budget for example)

规则示例为“减少HTTP请求”。 示例规则集为“小型网站或博客”(比默认规则集严格,因为它假定小型网站没有CDN预算)

YSlow has a number of rules defined. How many? Easy to check once you have your setup from the last blog post. Open the console and go:

YSlow定义了许多规则。 多少? 在上一篇博客文章中进行设置后,易于检查。 打开控制台并转到:

>>> Object.keys(YSLOW.controller.rules).length
23

And how many rulesets?

多少规则集?

>>>Object.keys(YSLOW.controller.rulesets)
["ydefault", "yslow1", "yblog"]

Each ruleset has an id (e.g. ydefault), friendly name, list of rules and list of weights for each rule:

每个规则集都有一个ID(例如ydefault ),友好名称,规则列表和每个规则的权重列表:

>>> YSLOW.controller.rulesets.ydefault
Object
id: "ydefault"
name: "YSlow(V2)"
rules: Object
weights: Object

The weights define what is the relative importance of each rule in the final score. And the rules contain rule-name => rule-config pairs. Because each rule is configurable. For an example configuration consider the "Thou shalt use CDN" rule. The patterns that match CDN hostnames are configurable. So is the number of points subtracted from the score for each violation.

weights定义了每个规则在最终分数中的相对重要性。 rules包含规则名称=>规则配置对。 因为每个规则都是可配置的。 对于示例配置,请考虑“您应使用CDN”规则。 与CDN主机名匹配的模式是可配置的。 从每次违规得分中减去的分数也是如此。

(I can talk more about scores, but it's not all that important. The thinking was that people might be offended by and disagree with the scores. So we should let them customize the scoring algo)

(我可以谈论更多关于分数的信息,但这并不是那么重要。我们的想法是人们可能会因分数而感到生气和不同意。因此,我们应该让他们自定义得分算法)

Alrighty, enough talking, let's create one new custom ruleset.

好了,足够多的讨论,让我们创建一个新的自定义规则集。

用户界面中的新规则集 (New ruleset from the UI)

  1. Click "Edit" next to the rulesets dropdown. A list of rules appear each with a helpful hint on mouseover and a friendly checkbox for your checking pleasure
    单击规则集下拉菜单旁边的“编辑”。 出现规则列表,每条规则都提供有关鼠标悬停的有用提示以及一个友好的复选框,可让您愉快地进行检查
  2. Click "New Set" to clear all default checks
    点击“新设置”以清除所有默认检查
  3. Check the most "duh!" rules, those that require no effort and are just sanity
    检查最“ du!” 规则,那些不需要努力,只是理智的规则
  4. Click "Save ruleset as..."
    点击“将规则集另存为...”
  5. Type a name, like "Duh", save
    输入名称,例如“ Duh”,保存

Congratulations! You have a new ruleset.

恭喜你! 您有一个新的规则集。

If that wasn't the bookmarklet version, YSlow would remember this new ruleset. But YSlow doesn't (yet) remember settings in bookmarklet version. (Try another YSlow run in a different tab if you don't believe it).

如果那不是书签版本,则YSlow会记住该新规则集。 但是,YSlow尚未记住书签版本中的设置。 (如果您不相信,请尝试在其他标签中运行另一个YSlow)。

But you can still save your ruleset, and even share it with others in your team.

但是您仍然可以保存规则集,甚至可以与团队中的其他人共享。

编码规则集 (Coded ruleset)

This above was all-UI way of creating the ruleset. Behind the UI there's a simple JS object (that can be serialized to JSON for future use) that defines the ruleset as explained above.

以上是创建规则集的全UI方法。 UI后面有一个简单的JS对象(可以序列化为JSON以供将来使用),该对象定义了如上所述的规则集。

>>>JSON.stringify(YSLOW.controller.rulesets.Duh)
"{"custom":true,"rules":{"ycompress":{},"yredirects":{},"yno404":{},"yemptysrc":{}},"weights":{},"id":"Duh","name":"Duh"}"

Tada!

多田

Now just take this JSON string, paste into your mystuff/stuff.js (from the previous post), clean it up a little and add a call to the YSlow API to register this new rule.

现在,只需使用此JSON字符串,将其粘贴到mystuff/stuff.js (从上mystuff/stuff.js文章中),对其进行一点清理,然后添加对YSlow API的调用即可注册此新规则。

parent.YUI = parent.YUI || YUI;
parent.YSLOW = YSLOW;
var duh = {
id: "duh",
name: "Duh",
rules: {
ycompress: {},
yredirects: {},
yno404: {},
yemptysrc: {}
},
weights: {}
};
YSLOW.registerRuleset(duh);

Than build and push:

比构建并推动:

$ make bookmarklet config="config-phpied.js"; \
scp build/bookmarklet/* \
username@perfplanet.com:~/phpied.com/files/yslow

So we have our own rule and we can run it and it can spit out reports.

因此,我们有自己的规则,可以运行它,它可以吐出报告。

(Note: Small correction from the previous post: in the Makefile your mystuff.js should go before the bookmarklet controller, which is responsible for the initialization. Because you want your registerRuleset() call to run before the initialization)

(注意:上一篇文章的小幅修正:在Makefile中,您的mystuff.js应该放在负责初始化的bookmarklet控制器之前。因为您希望您的registerRuleset()调用在初始化之前运行)

(Another Note: Disable Chrome's cache if you're testing with Chrome, because it's pretty aggressive in this bookmarklet scenario)

(另一注:如果您正在使用Chrome进行测试,请禁用Chrome的缓存,因为在这种书签场景中,Chrome的缓存非常激进)

If we decide to tweak the scores and weights a little bit (take out 50 out 100 points for a single non-gzipped component and increase the rule's relative weight), we can do:

如果我们决定稍微调整分数和权重(对于单个非固定组件,从100分中拿出50分,并增加规则的相对权重),则可以执行以下操作:

var duh = {
id: "duh",
name: "Duh",
rules: {
ycompress: {
points: 50
},
yredirects: {},
yno404: {},
yemptysrc: {}
},
weights: {
ycompress: 10,
yredirects: 3,
yno404: 3,
yemptysrc: 5
}
};

The the result of running the tweaked ruleset on the same page is different this time:

这次在同一页面上运行经过调整的规则集的结果有所不同:

You can inspect each rule's default config like:

您可以检查每个规则的默认配置,例如:

>>> YSLOW.controller.rules.ycompress.config
Object
min_filesize: 500
points: 11
types: Array[5]
0: "doc"
1: "iframe"
2: "xhr"
3: "js"
4: "css"

阿迪奥斯(Adios)

This is it for tonight, next time: how to write your own rules.

下次是今晚,这是:如何编写自己的规则。

pssst, a hack to make your new rule the default because bookmarklets don't remember preferences:

pssst,一种使您的新规则成为默认规则的技巧,因为bookmarklet不会记住首选项:

// HACK
YSLOW.util.Preference.getPref = function(name, def) {
return name === "defaultRuleset" ? 'duh' : def;
};

And the final version of mystuff/stuff.js for completeness (and without global variables this time):

以及完整版本的mystuff/stuff.js的最终版本(这次没有全局变量):


YSLOW.registerRuleset({
id: "duh",
name: "Duh",
rules: {
ycompress: {
points: 50
},
yredirects: {},
yno404: {},
yemptysrc: {}
},
weights: {
ycompress: 10,
yredirects: 3,
yno404: 3,
yemptysrc: 5
}
});
// HACK
YSLOW.util.Preference.getPref = function(name, def) {
return name === "defaultRuleset" ? 'duh' : def;
};
// DEBUG
parent.YUI = parent.YUI || YUI;
parent.YSLOW = YSLOW;

Tell your friends about this post on Facebook and Twitter

在Facebook和Twitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/yslow-development-custom-rulesets/

YSlow开发:自定义规则集相关推荐

  1. Android开发自定义View

    Android中View组件的作用类似于Swing变成中的JPanel,它只是一个空白的矩形区域,View组件中没有任何内容.对于Android应用的其他UI组件来说,它们都继承了View组件,然后在 ...

  2. 开发自定义JSF组件(4) 保存状态与恢复状态

    2019独角兽企业重金招聘Python工程师标准>>> 完整的教材: 开发自定义JSF组件(1) HelloWorld 开发自定义JSF组件(2) 使用Render渲染器 开发自定义 ...

  3. integer加1_利用Abaqus UEL开发自定义单元1

    1. 简介 在Abaqus/Standard模块中,用户可以利用子程序UEL来开发自定义单元,用以实现一些通过Abaqus内置单元无法实现的功能.如果编写恰当,用户自定义单元可以正常使用Abaqus/ ...

  4. 【Android开发】用户界面设计-开发自定义的View

    效果图: Android中,所有的UI界面都是由View类和ViewGroup类及其子类组合而成的.View是所有UI组件的基类(父类),为ViewGroup类是容纳这些UI组件的容器,其本身也是Vi ...

  5. java自定义菜单跳转页面_微信公众号开发 自定义菜单跳转页面并获取用户信息实例详解...

    微信公众号开发 自定义菜单 请先读完本文再进行配置开发 请先前往微信平台开发者文档阅读"网页授权获取用户基本信息"的接口说明 在微信公众账号开发中,往往有定义一个菜单,然后用户点击 ...

  6. Cognos开发自定义排序规则的报表和自定义排名报表

    Cognos开发自定义排序规则的报表和自定义排名报表 场景:有一个简单的销售数据分析,可以按照日期,按照商品类型来分析订单笔数和订单金额. 目的:用户可以自定义查看按照不同指标排序的数据,用户可以查看 ...

  7. android自定义省略号,Android开发自定义TextView省略号样式的方法

    本文实例讲述了Android开发自定义TextView省略号样式的方法.分享给大家供大家参考,具体如下: 在布局xml中设置textView的字段 android:maxLines="2&q ...

  8. BizTalk开发系列(二十二) 开发自定义Map Functoid

    更多内容请查看:BizTalk动手实验系列目录                       BizTalk 开发系列 尽管 BizTalk Server 提供许多Functoid以支持一系列不同的操作 ...

  9. 微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

    微信开发公众平台自定义菜单需要花钱认证才能实现,不想花钱只能玩测试账号了,不过这并不影响开发.我的开发都是基于柳峰老师的微信公众平台应用开发做的. 只要我们使用公众平台测试账号就可以开发自定义菜单了, ...

最新文章

  1. python3.7 6如何安装-Python 3.7.1在CentOS 6.10 安装部署
  2. Spring之Bean的配置(一)
  3. C语言 变量 函数 (类型、作用域、生命周期、存储位置)
  4. Java程序员需要掌握的计算机底层知识(四):内存管理
  5. mysql解释器_atitit.java解析sql语言解析器解释器的实现
  6. c语言中把每个字母向前移1位,C语言:将字符串中的前导*号全部移到字符串的尾部。...
  7. 牛客算法周周练4 题解
  8. UIControl事件
  9. 学习《css世界》笔记之使用overflow做文字溢出点点点效果
  10. linux 文件列添加字段,如何在linux中加入所需列的文件?
  11. github第一步之初始化操作
  12. protocol buffer安装及使用(非常详细)
  13. c语言printf输出格式
  14. JAVA定义矩形类 方法一
  15. 华为云.通信云服务激活无限商业潜力
  16. 联想笔记本连不上手机热点_笔记本电脑连接不上手机热点该怎么解决?
  17. nmn是真的还是假的,如何鉴别高质量的nmn,方法一览
  18. 【每日蓝桥】12、一三年省赛Java组真题“振兴中华”
  19. 团队作业——项目测试
  20. BAPI_GOODSMVT_CREATE(调拨 收货 发货 入库 退货)BAPI

热门文章

  1. 卸载百度超级搜霸过程
  2. X-Forwarded
  3. java jsp中的日历表,jsp日历表格怎么做
  4. LNMP搭建+论坛搭建
  5. Androidstudio的约束布局,安卓面试宝典pdf
  6. ipad分屏怎么用_如何装备一个学术型的 iPad?
  7. S4 HANA迁移驾驶舱(Migration cockpit : LTMC/LTMOM)简介和服务配置激活
  8. 论文阅读:ECCV 2020 | Self-Challenging Improves Cross-Domain Generalization
  9. 程序员如何优雅使用Mac
  10. ORA-01849 :小时值必须介于1和12之间!