Page Reloading Error

This is more of an "alert" since I found out the problem, but I wanted to post and let people know if they ran across this.

While using Firebug I noticed that my pages kept loading twice and I had a message in the console that said:

The character encoding declaration of the HTML document

was not found when prescanning the first 1024 bytes of

the file. When viewed in a differently-configured browser,

this page will reload automatically. The encoding

declaration needs to be moved to be within the first

1024 bytes of the file.

The issue is that Yii, because of the way it publishes JavaScript asset files, was putting in so many lines that the Meta line for my content type was pushed too far down. The HTML View Source for my pages showed:

<!DOCTYPE html>

<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->

<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->

<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->

<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->


    <link rel="stylesheet" type="text/css" href="/assets/16572020/css/bootstrap.css" />

<link rel="stylesheet" type="text/css" href="/assets/16572020/css/bootstrap-yii.css" />

<link rel="stylesheet" type="text/css" href="/assets/e984feb5/pager.css" />

<link rel="stylesheet" type="text/css" href="/assets/a94942fc/jui/css/base/jquery-ui.css" />

<script type="text/javascript" src="/assets/a94942fc/jquery.js"></script>

<script type="text/javascript" src="/assets/a94942fc/jquery.yii.js"></script>

<script type="text/javascript" src="/assets/a94942fc/jquery.yiiactiveform.js"></script>

<script type="text/javascript" src="/assets/16572020/js/bootstrap.js"></script>

<script type="text/javascript" src="/assets/824053d9/custom.js"></script>

<title>My Test Web Site</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <meta name="language" content="en" />

    <link rel="stylesheet" type="text/css" href="/css/base.css">

    <link rel="stylesheet" type="text/css" href="/css/skeleton.css">

The top lines are from but mostly they’re all the Yii asset publishing. My original main.php file, however, just was:

<!DOCTYPE html>

<!--[if lt IE 7 ]><html class="ie ie6" lang="<?= _xls_get_conf('LANG_CODE', 'en') ?>"> <![endif]-->

<!--[if IE 7 ]><html class="ie ie7" lang="<?= _xls_get_conf('LANG_CODE', 'en') ?>"> <![endif]-->

<!--[if IE 8 ]><html class="ie ie8" lang="<?= _xls_get_conf('LANG_CODE', 'en') ?>"> <![endif]-->

<!--[if (gte IE 9)|!(IE)]><!--><html lang="<?= _xls_get_conf('LANG_CODE', 'en') ?>"> <!--<![endif]-->


    <title><?php echo CHtml::encode($this->pageTitle); ?></title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <meta name="language" content="en" />


    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/base.css">

What I discovered was that Yii is hunting for the <title> header command and using that as an anchor for all the inserted asset JS lines. Because my title was above my own meta line, that’s why it pushed everything down. By moving title down in main.php below ContentType, it solved the issue.

This also might be an issue with CSS files in general if you’re loading conflicting elements where order matters (say to override an element). Basically just remember wherever you put your title is where Yii will put all its autopublished lines.

Thanks for posting this!

I was encountering the same page reloading issue (in Firefox only), but Firebug did not give me the warning you mention. I was digging through the CGridView Javascript and getting more and more confused – it only happened on pages with CGridViews. This turns out to be because they resulted in more Javascript includes up in the header, but I don’t think I would have ever figured it out on my own.

Your posting is very much appreciated!