PHPackages                             istranger/yii2-resource-smart-load - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. istranger/yii2-resource-smart-load

ActiveYii2-extension

istranger/yii2-resource-smart-load
==================================

The extension for prevent reload (on AJAX request) resources, which already exist on client

v1.1.2(10y ago)24.9k—0%2MITPHP

Since Feb 14Pushed 10y ago3 watchersCompare

[ Source](https://github.com/IStranger/yii2-resource-smart-load)[ Packagist](https://packagist.org/packages/istranger/yii2-resource-smart-load)[ RSS](/packages/istranger-yii2-resource-smart-load/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

Extension to prevent reloading JS/CSS resources (on AJAX request)
=================================================================

[](#extension-to-prevent-reloading-jscss-resources-on-ajax-request)

This extension for Yii 2 prevent reload resources (on AJAX request), which already exist on client. Similar extension for [yii 1.1.x](https://github.com/IStranger/yii-resource-smart-load). This extension has more functionality than the Yii 2 native resource filter, [see detail](#similar-extensions-native-resource-filtration-script)

Requirements
------------

[](#requirements)

- PHP 5.4.0+.
- YiiFramework 2.0.2+

Features
--------

[](#features)

- Prevent reload of JS files
- Prevent reload of JS inline blocks
- Prevent reload of CSS files
- Prevent reload of CSS inline blocks
- Flexible configuration of resources disabling
- Can disable native filtration of JS/CSS files

Limitations
-----------

[](#limitations)

- Increases incoming traffic (from client to server), because extension uses cookie and http headers. This is especially important for sites with a large number of included resource files. This can be adjusted by changing the hash method (see options of extension). In addition it should be remembered that the size of the cookie is limited (in browser).
- Extension does not work if the user has enabled filtering of http headers (for example, on the corporate proxy), and browser not to accept cookies. However, we assume the probability of such events is low.

Installation
------------

[](#installation)

- **Installation via composer:** add to your composer.json file ("require" section) the following line `"istranger/yii2-resource-smart-load": "*"`(see [packagist page](https://packagist.org/packages/istranger/yii2-resource-smart-load))
- Add in config file (by default /config/web.php):

```
'components' => [
    // ...
    'view' => [
        'class'           => 'istranger\rSmartLoad\View',
        'smartLoadConfig' => [
            // Hashing method for resource names (if string value),
            // see possible values: http://php.net/manual/en/function.hash.php#104987
            // Can be assigned "callable", for example: function ($str) { return hash('sha256', $str); }
            // 'hashMethod'               => 'md5', // default = 'crc32b'

            // Types of resources, that will be tracked by current extension
            // If =null, include all resource types: ['jsFile', 'cssFile', 'jsInline', 'cssInline']
            // 'resourceTypes'            => ['jsFile', 'jsInline'],  // default = ['jsFile']

            // 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' => ['bootstrap.js'],  // default = []

            // Disable native script filter
            // (only for resource types specified in 'resourceTypes')
            // 'disableNativeScriptFilter' => false, // default = true
        ]
    ],
    // ...
],
```

Usage
-----

[](#usage)

### Typical use case

[](#typical-use-case)

By default, this extension disables reloading of js files only, css files and js/css inline blocks not tracked (see option **resourceTypes**). But you can (carefully!) disable other resources types.

That is, each "tracked" resource on the page will be loaded **only once**, even if it will later be removed from this page. Therefore, all JS callbacks **on first load** should be bind to the global containers (for example, document) using jQuery-method **.on()**.

At the subsequent AJAX requests already loaded CSS inline blocks (or CSS files) may be replaced by new content, therefore, in case you have problems with several CSS styles you configure exclusions (see below).

### Advanced use case

[](#advanced-use-case)

For the analysis of disabled/loaded scripts is convenient to use an option **enableLog**, that output useful debug information in browser console:

```
    'enableLog' => true, // default = false
```

You can more flexible manage resource loading on certain pages using methods (see [examples](#examples)):

- `Yii::$app->view->getRSmartLoad()->disableLoadedResources(array $types = null);`Disables loading of resources, which **already loaded on client**. Calling this method disables loading "client" resources, even if they will registered after calling this method.
- `Yii::$app->view->getRSmartLoad()->disableResources(array $resourceList, array $types = null);`Disables loading of **given** resources. Calling this method disables loading given resources, even if they will registered after calling this method. Resource list can contain:
    - for JS/CSS files: full URL, basename, or hash
    - for JS/CSS inline blocks: full content of block, or hash
    - `array('*')` - disables all resources

These methods can be invoked in any actions. The argument **$types** is an array of tracked resource types, that should be excluded from the page. By default (=null), tracked all types.
Array can be defined using constants:

```
    array(
        RSmartLoad::RESOURCE_TYPE_JS_FILE,      // = 'jsFile'
        RSmartLoad::RESOURCE_TYPE_JS_INLINE,    // = 'jsInline'
        RSmartLoad::RESOURCE_TYPE_CSS_FILE,     // = 'cssFile'
        RSmartLoad::RESOURCE_TYPE_CSS_INLINE,   // = 'cssInline'
    )
```

This restriction has higher priority than $resourceList.

In addition, you can set **activateOnAllPages = false**, and extension will be disabled on all pages. You will need to manually configure disabling of resources on certain pages (with the help of these methods).

Alternatively, you can configure exclusion list of resources:

```
    'alwaysReloadableResources' => ['bootstrap.js']  // default = []
```

These resources always will be loaded on client. Each resource can be presented:

- resource file: as **hash**, or **full URL**, or **basename**.
- resource inline block: as **hash**, or **resource content**.

The hash of specific resource can be get through browser console in the global object of extension (enableLog == true). For example:

```
yii.resourceSmartLoad.resources = {
    "89cb8371": {
        "resource": "/test_yii2/basic/web/assets/a5ccee6b/jquery.js",
        "hash": "89cb8371",
        "source": "2015-02-14 12:27:08,
            GET,
            url = http://test.dev/test_yii2/basic/web/index.php?r=site%2Fabout,
            referrer = http://test.dev/test_yii2/basic/web/index.php"
    },
    "4cc53fc1": {
        "resource": "/test_yii2/basic/web/assets/94203850/css/bootstrap.css",
        "hash": "4cc53fc1",
        "source": "2015-02-14 12:27:08,
            GET,
            url = http://test.dev/test_yii2/basic/web/index.php?r=site%2Fabout,
            referrer = http://test.dev/test_yii2/basic/web/index.php"
    },
    "b66631ac": {
        "resource": "jQuery('#w1').yiiGridView({\"filterUrl\":\"/test_yii2/basic/web/index.php?r=user%2Findex\",\"filterSelect...",
        "hash": "b66631ac",
        "source": "2015-02-14 12:27:12,
            GET/AJAX,
            url = http://test.dev/test_yii2/basic/web/index.php?r=user%2Findex,
            referrer = http://test.dev/test_yii2/basic/web/index.php?r=site%2Fabout"
    },
    "125a821d": {
        "resource": "body {font-size: 110%;}",
        "hash": "125a821d",
        "source": "2015-02-14 12:27:11,
            GET/AJAX,
            url = http://test.dev/test_yii2/basic/web/index.php?r=site%2Fcontact,
            referrer = http://test.dev/test_yii2/basic/web/index.php?r=site%2Fabout"
    }
}
```

### Examples

[](#examples)

Examples of usage in controller actions (it is assumed that **activateOnAllPages = false**).

#### Disable load of all CSS inline blocks:

[](#disable-load-of-all-css-inline-blocks)

```
Yii::$app->view->getRSmartLoad()->disableResources(['*'], [RSmartLoad::RESOURCE_TYPE_CSS_INLINE]);
```

Note: specified resources will be excluded from the page for all requests (not only AJAX).

#### Disable load of certain resources files (for all AJAX requests):

[](#disable-load-of-certain-resources-files-for-all-ajax-requests)

```
if(Yii::$app->request->isAjax){
    Yii::$app->view->getRSmartLoad()->disableResources(['yii.gridView.js', 'bootstrap.css']);
}
```

Note: at normal request (not AJAX) these files will be included into page.

#### Disable load of certain resources files with restriction by type:

[](#disable-load-of-certain-resources-files-with-restriction-by-type)

```
Yii::$app->view->getRSmartLoad()->disableResources(['yii.gridView.js', 'bootstrap.css'],[
    RSmartLoad::RESOURCE_TYPE_CSS_INLINE,
    RSmartLoad::RESOURCE_TYPE_JS_FILE
]);
```

Note: will be disabled only `'yii.gridView.js'`, because restriction by type has higher priority than $resourceList.

#### Disable load of JS inline blocks and JS files, which already exist on client:

[](#disable-load-of-js-inline-blocks-and-js-files-which-already-exist-on-client)

```
$view->getRSmartLoad()->disableLoadedResources(['*'], [
    RSmartLoad::RESOURCE_TYPE_JS_INLINE,
    RSmartLoad::RESOURCE_TYPE_JS_FILE
]);
```

Note: resources can be disabled only on AJAX request.

#### Disable load of all resources, which already exist on client:

[](#disable-load-of-all-resources-which-already-exist-on-client)

```
$view->getRSmartLoad()->disableLoadedResources(['*']);
```

Tests
-----

[](#tests)

Tests will be later.

Similar extensions. Native resource filtration script
-----------------------------------------------------

[](#similar-extensions-native-resource-filtration-script)

To prevent reloading scripts for Yii 1.1.x you can use [nlsclientscript](https://github.com/nlac/nlsclientscript).

Similar approach used in [Yii 2 core](https://github.com/yiisoft/yii2/blob/master/framework/assets/yii.js#L50).

However, there are a few differences:

- Extension used different algorithm: at AJAX request duplicated resource files are deleted on the client **after**receiving the content (not on the server, as in our realization). We assume that our approach is conceptually more correct.
- Our realization don't deletes (intentionally) the resources, which included directly in html code (without registering through View). In this case, we assume that these resources are very necessary.
- Native filter don't deletes duplicate CSS files. But there is nothing wrong, because most browsers will not reload the files.
- Native filter cannot prevent reload of JS/CSS inline blocks.

Our extension does not interfere with native filter. They can be used together or separately. In our extension option **disableNativeScriptFilter** can partial disable native filter (if necessary).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Total

4

Last Release

4010d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dcd37b520cfbc3ee7c48cebf5734a273b333db60466708b4e589e6597409b712?d=identicon)[IStranger](/maintainers/IStranger)

---

Tags

clientcssJSyii2resourcesinlinescriptspreventreload

### Embed Badge

![Health badge](/badges/istranger-yii2-resource-smart-load/health.svg)

```
[![Health](https://phpackages.com/badges/istranger-yii2-resource-smart-load/health.svg)](https://phpackages.com/packages/istranger-yii2-resource-smart-load)
```

###  Alternatives

[rmrevin/yii2-minify-view

Yii2 View component with auto minification css &amp; js in runtime

194302.3k7](/packages/rmrevin-yii2-minify-view)[widmogrod/zf2-assetic-module

Zend Framework 2 module that provides complete integration of Assetic library

144348.5k8](/packages/widmogrod-zf2-assetic-module)[lajax/yii2-asset-minifier

Runtime minification and combination of asset files.

1010.4k](/packages/lajax-yii2-asset-minifier)[skeeks/yii2-google-api

Component for work with google api based on google/apiclient

1243.1k1](/packages/skeeks-yii2-google-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
