laravel-page-speed icon indicating copy to clipboard operation
laravel-page-speed copied to clipboard

Uncaught SyntaxError: unexpected token: keyword 'if'

Open ShapesGraphicStudio opened this issue 3 years ago • 2 comments

When using RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class I get a JS related error: Uncaught SyntaxError: unexpected token: keyword 'if'

Looks like this part is causing it:

document.onreadystatechange = function () {
        var state = document.readyState
        if (state == 'interactive') {
            document.getElementById('contents').style.visibility="hidden";
        } else if (state == 'complete') {
            setTimeout(function(){
                $('#loading').fadeOut(1600);
                document.getElementById('contents').style.visibility="visible";
            }, 800);
        }
    }

If I change the code to:

document.onreadystatechange = function () {
        var state = document.readyState;
        if (state == 'interactive') {
            document.getElementById('contents').style.visibility="hidden";
        } else if (state == 'complete') {
            setTimeout(function(){
                $('#loading').fadeOut(1600);
                document.getElementById('contents').style.visibility="visible";
            }, 800);
        }
    }

I get Uncaught SyntaxError: unexpected token: keyword 'var'

Everything is working fine when I comment in Kernel.php: RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class,

Environment: PHP 7.4.12 Laravel 8.17.2

Any idea on how to get this fixed please?

ShapesGraphicStudio avatar Mar 24 '21 13:03 ShapesGraphicStudio

I solved this correcting my js to:

document.onreadystatechange = function () {
        if (document.readyState == 'interactive') {
            document.getElementById('contents').style.visibility="hidden";
        } else if (document.readyState == 'complete') {
            setTimeout(function(){
                $('#loading').fadeOut(1600);
                document.getElementById('contents').style.visibility="visible";
            }, 800);
        }
    };

ShapesGraphicStudio avatar Mar 24 '21 14:03 ShapesGraphicStudio

Hi @ShapesGraphicStudio Thanks for reporting! I'll check it

lucasMesquitaBorges avatar Mar 25 '21 00:03 lucasMesquitaBorges