istranger / yii-resource-smart-load
用于防止在客户端已经存在的资源在AJAX请求中重新加载的扩展
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-12 05:28:50 UTC
README
本扩展用于yii 1.1.x防止在客户端已经存在的资源在AJAX请求中重新加载。
需求
- PHP 5.4.0+. 理论上应在5.3上也能工作,但未进行测试。
- YiiFramework 1.1.14+
功能
- 防止JS文件重新加载
- 防止JS内联块重新加载
- 防止CSS文件重新加载
- 防止CSS内联块重新加载
- 灵活配置资源禁用
限制
- 由于扩展使用cookie和HTTP头部,因此会增加来自客户端到服务器的流量。这对于包含大量资源文件的网站尤为重要。这可以通过更改哈希方法来调整:`crc32b`比`md5`占用的空间更小(请参阅扩展选项)。此外,还应记住cookie的大小是有限的(在浏览器中)。
- 如果用户启用了HTTP头部的过滤(例如,在企业代理上),并且浏览器不接受cookie,则扩展将无法工作。然而,我们假设这种情况发生的概率很低。
安装
- 手动安装:将文件提取到`protected/extensions/yii-resource-smart-load`下。
- 通过composer安装:将以下行添加到您的composer.json文件中的"require"部分
"istranger/yii-resource-smart-load": "dev-master"
(请参阅packagist页面) - 添加到配置文件中(默认为config/main.php)
// application components 'components' => array( // ... 'clientScript' => array( // Path of main class: // -- if installed manually: 'class' => 'ext.yii-resource-smart-load.RSmartLoadClientScript', // -- if installed via composer: // 'class' => 'application.vendor.istranger.yii-resource-smart-load.RSmartLoadClientScript', // Hashing method for resource names, // see possible values: https://php.ac.cn/manual/en/function.hash.php#104987 // 'hashMethod' => 'md5', // default = 'crc32b' // Types of resources, that will be tracked by current extension. // If =null, include all resource types: // array('jsFile', 'cssFile', 'jsInline', 'cssInline') // 'resourceTypes' => array('jsFile', 'jsInline'), // default = null // Enable log on server and client side (debug mode) // 'enableLog' => true, // default = false // Activate "smart" disabling of resources on all pages // 'activateOnAllPages' => true // default = true // List of resources, that always should be loaded on client // (by name, hash, or full URL) // 'alwaysReloadableResources' => array('jquery.yiiactiveform.js') // default = array() ) // ... ),
使用方法
典型用例
默认情况下,此扩展禁用所有匹配资源的重新加载(js/css文件和内联块)。也就是说,页面上的每个资源都将只加载一次,即使后来从这个页面上删除。因此,所有JS回调(在首次加载时)应使用jQuery方法.on()绑定到全局容器(例如,document)。在后续的AJAX请求中,已加载的CSS内联块(或CSS文件)可能被新内容替换,因此,如果您遇到CSS样式问题,建议设置选项
'resourceTypes' => array('jsFile', 'jsInline'), // or => array('jsFile')
高级用例
为了分析已禁用/加载的脚本,可以使用选项enableLog,该选项在浏览器控制台中输出有用的调试信息
'enableLog' => true, // default = false
您可以使用方法更灵活地管理某些页面上的资源加载
- RSmartLoadClientScript::disableAllResources(array $types = null); 禁用加载所有资源。调用此方法将禁用所有资源的加载,即使它们在调用此方法后注册。
- RSmartLoadClientScript::disableLoadedResources(array $types = null); 禁用已加载资源的加载。调用此方法将禁用已加载的“客户端”资源的加载,即使它们在调用此方法后注册。
这些方法可以在任何操作中调用。参数$types是资源类型的数组,可以使用常量定义
array( RSmartLoadClientScript::RESOURCE_TYPE_JS_FILE, // = 'jsFile' RSmartLoadClientScript::RESOURCE_TYPE_JS_INLINE, // = 'jsInline' RSmartLoadClientScript::RESOURCE_TYPE_CSS_FILE, // = 'cssFile' RSmartLoadClientScript::RESOURCE_TYPE_CSS_INLINE, // = 'cssInline' )
此外,您可以设置activateOnAllPages = false,并且扩展将在所有页面上禁用。您需要手动配置某些页面上资源的禁用(使用这些方法)。
或者,您可以配置资源排除列表
'alwaysReloadableResources' => array('jquery.yiiactiveform.js') // default = array()
这些资源总是会加载到客户端。每个资源都可以表示
- 资源文件:作为 哈希、完整URL 或 基本名称。
- 资源内联块:作为 哈希 或 资源内容。
可以通过扩展的全局对象中的浏览器控制台获取特定资源的哈希(enableLog == true)。例如
yiiResourceSmartLoad.resources = { "fd425af9": { "resource": "/test_yii/assets/62fbda6e/jquery.maskedinput.js", "hash": "fd425af9", "source": "2015-02-08 23:14:30, GET, url = /test_yii/index.php?r=site/ajaxForm, referrer = http://test.dev/test_yii/index.php?r=site/contact" }, "5e030b8c": { "resource": "jQuery('#yw0').tabs({'collapsible':true});", "hash": "5e030b8c", "source": "2015-02-08 23:14:30, GET, url = /test_yii/index.php?r=site/ajaxForm, referrer = http://test.dev/test_yii/index.php?r=site/contact" }, "5ce96349": { "resource": "(function ($) { // yii-resource-smart-load extension window.yiiResourceSmartLoad.initExtension({\"...", "hash": "5ce96349", "source": "2015-02-08 23:14:30, GET, url = /test_yii/index.php?r=site/ajaxForm, referrer = http://test.dev/test_yii/index.php?r=site/contact" }, "d60b9939": { "resource": "jQuery('#contact-form').yiiactiveform({'validateOnSubmit':true,'attributes':[{'id':'ContactForm_name...", "hash": "d60b9939", "source": "2015-02-08 23:29:09, GET/AJAX, url = /test_yii/index.php?r=site/contact, referrer = http://test.dev/test_yii/index.php?r=site/ajaxForm" } }
类似扩展
为了防止脚本重新加载,可以使用 nlsclientscript
然而,有一些不同之处
-
扩展使用不同的算法:在AJAX请求中,在接收内容后(而不是在服务器上,如我们的实现),客户端删除重复的资源文件。我们假设我们的方法是概念上更正确的。
-
我们的实现不会删除(故意)直接包含在HTML代码中(未通过ClientScript注册)的资源。在这种情况下,我们假设这些资源非常必要。
-
nlsclientscript不会删除重复的CSS文件。但这没有什么不正确的,因为大多数浏览器不会重新加载这些文件。
-
nlsclientscript不能防止内联的js/css块重新加载。
变更日志
v0.2-beta (2015-02-08)
- 修改了扩展的基本算法
- 代码重构和优化
- 添加了防止JS内联块重新加载
- 添加了防止CSS内联块重新加载
- 扩展了资源禁用配置的可能性
v0.1-beta (2015-02-04)
- 初始发布
- 防止JS文件重新加载
- 防止CSS文件(以及像*.ico这样的其他链接文件)重新加载