rmrevin / yii2-minify-view

Yii2 View component with minification css & js
MIT License
191 stars 67 forks source link

JS is missing on certain page loads #29

Closed synackSA closed 8 years ago

synackSA commented 8 years ago

If I load page A on a clean @webroot\minify directory, everything works fine. If I load page B after that it seems to have problems loading some of the JS with parts of it missing, generating JS errors in the console. If I wipe the @webroot\minify directory and then load page B, it works fine, but then when I try load page A, it breaks in the same way.

I will try do some more investigation when I get time and add more details to this issue report.

rmrevin commented 8 years ago

If on page B registred one other js file - will be created a new minified bundle. But mistakes should not be. Show your bundles and configuration.

synackSA commented 8 years ago

My view config for minify is:

    'view' => [
        'class' => '\rmrevin\yii\minify\View',
        'enableMinify' => true,
        'web_path' => '@web', // path alias to web base
        'base_path' => '@webroot', // path alias to web base
        'minify_path' => '@webroot/minify', // path alias to save minify result
        'js_position' => [ \yii\web\View::POS_END ], // positions of js files to be minified
        'force_charset' => 'UTF-8', // charset forcibly assign, otherwise will use all of the files found charset
        'expand_imports' => true, // whether to change @import on content
        'compress_output' => true, // compress result html page
        'minifyJs' => true,
        'minifyCss' => true
    ]

I have a admin dashboard page, which draws some charts with stats, etc. Doesn't register a Asset bundle, other than what is registered in the main layout, namely AppAsset::register($this);

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css'
    ];
    public $js = [
        'custom/jscookies/js.cookie.js',
        'js/main.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\jui\JuiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

Inside the dashboard template, I do however generate JS variables and then register the JS vars and a js file:

<?php
    $jsVars = "";
    $jsVars .= "var leadsPerDay=[{$leadsPerDay}];";
    $jsVars .= "var leadsPerMonth=[{$leadsPerMonth}];";
    $jsVars .= "var unclaimedLeadersPerMonth=[{$unclaimedLeadersPerMonth}];";
    $jsVars .= "var claimedLeadersPerMonth=[{$claimedLeadersPerMonth}];";
    $this->registerJs($jsVars, View::POS_HEAD, 'lead-charts');
    $this->registerJsFile('/js/lead-chart.js', [
        'depends' => ['\app\assets\ChartsAsset'],
        'position' => View::POS_END
    ]);
?>

Charts asset is below:

class ChartsAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [

    ];
    public $js = [
        'https://www.google.com/jsapi'
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

I then have another admin search page that has a single input box, with 2 drops downs. The only javascript on the page is the JS generated by Yii for a ActiveForm, which is actually the feedback ActiveForm which is in the footer (which is on every page, including the admin page mentioned above).

If I clean @webroot\minify and then load admin/dashboard, everything loads fine. If I then go to admin/mls-search I get the following error in the console:

Uncaught TypeError: jQuery(...).yiiActiveForm is not a function

If I then clear @webroot\minify and load admin/mls-search, I get no error, but then when I load admin/dashboard I get the following error:

Uncaught Error: Slider was already initialized.
Uncaught TypeError: Cannot read property 'replaceChild' of null

EDIT Added ChartsAsset

synackSA commented 8 years ago

Sorry, I missed some stuff. In the main layout, we also load the _searchblock.php template, which registers the SearchAsset asset.

class SearchAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'custom/noUiSlider.7.0.8/slider.css'
    ];
    public $js = [
        'js/search-block.js',
        'js/save-searches.js',
        'custom/noUiSlider.7.0.8/jquery.nouislider.all.min.js',
        'custom/noUiSlider.7.0.8/sliders_new.js'
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

The variables in the second error, are from the sliders_new.js file. To be clear, both bits of javascript are loaded on every single page. One because it is registered in the main layout, the other is generated by Yii because it is a ActiveForm in the footer.

synackSA commented 8 years ago

Sorry, the above statement isn't quite true. The javascript that is loaded on admin/mls-search is loaded on the admin/dashboard page, but the admin/dashboard has the extra chart JS loaded. Sorry for the confusion.

rmrevin commented 8 years ago

So everything works correctly?

synackSA commented 8 years ago

No, but I think I found the problem. It's the same problem that I committed a fix for. In the getAbsoluteFilePath function in View.php, we need to be stripping the ?=<version> from the $file string.

synackSA commented 8 years ago

Is there a reason that you're using the getAbsoluteFilePath function in the processMinifyJs function, but not the processMinifyCss function?

rmrevin commented 8 years ago

Try now dev-master version. Your problem resolve a new fix?

synackSA commented 8 years ago

Yeah, that seems to work nicely.

rmrevin commented 8 years ago

Okay, released version 1.9.3