yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

Embeding content in head (before <title> tag) #1388

Closed DuskoV closed 11 years ago

DuskoV commented 12 years ago

I find it not SEO friendly to have styles, and other stuff included before element. Thus I made a work-around, which is adding meta stuff after <title>, but keep as is now included CSS/JS right before closing </head> tag. My changes are all in CClientScript.php file, in the function renderHead():</p> <p>Here is the code of the function, how it looks after my changes:</p> <pre><code>public function renderHead(&$output) { $html='';$mtags=''; foreach($this->metaTags as $meta) $mtags.=CHtml::metaTag($meta['content'],null,null,$meta)."\n"; foreach($this->linkTags as $link) $html.=CHtml::linkTag(null,null,null,null,$link)."\n"; foreach($this->cssFiles as $url=>$media) $html.=CHtml::cssFile($url,$media)."\n"; foreach($this->css as $css) $html.=CHtml::css($css[0],$css[1])."\n"; if($this->enableJavaScript) { if(isset($this->scriptFiles[self::POS_HEAD])) { foreach($this->scriptFiles[self::POS_HEAD] as $scriptFile) $html.=CHtml::scriptFile($scriptFile)."\n"; } if(isset($this->scripts[self::POS_HEAD])) $html.=CHtml::script(implode("\n",$this->scripts[self::POS_HEAD]))."\n"; } if (!empty($mtags)) { $count=0; $output=preg_replace('/(<\\/title\b[^>]*>)/is','$1<###title###>',$output,1,$count); if($count) $output=str_replace('<###title###>',$mtags,$output); else { $output=preg_replace('/(<\\/head\s*>)/is','<###head###>$1',$output,1,$count); if($count) $output=str_replace('<###head###>',$mtags,$output); else $output=$mtags.$output; } } if($html!=='') { $count=0; //$output=preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is','<###head###>$1',$output,1,$count); $output=preg_replace('/(<\\/head\s*>)/is','<###head###>$1',$output,1,$count); if($count) $output=str_replace('<###head###>',$html,$output); else $output=$html.$output; } } </code></pre> <p>Here is description of changes:</p> <p>1) Change at begining (line #352 and #354) $html='';$mtags=''; foreach($this->metaTags as $meta) $mtags.=CHtml::metaTag($meta['content'],null,null,$meta)."\n";</p> <p>2) Then added, right before "if($html!=='')": if (!empty($mtags)) { $count=0; $output=preg<em>replace('/(<\/title\b[^>]</em>>)/is','$1<###title###>',$output,1,$count); if($count) $output=str_replace('<###title###>',$mtags,$output); else { $output=preg<em>replace('/(<\/head\s</em>>)/is','<###head###>$1',$output,1,$count); if($count) $output=str_replace('<###head###>',$mtags,$output); else $output=$mtags.$output; } }</p> <p>3) And last, replaced line: $output=preg<em>replace('/(<title\b[^>]</em>>|<\/head\s_>)/is','<###head###>$1',$output,1,$count);</p> <p>with: $output=preg_replace('/(<\/head\s*>)/is','<###head###>$1',$output,1,$count);</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/resurtm"><img src="https://avatars.githubusercontent.com/u/100198?v=4" />resurtm</a> commented <strong> 12 years ago</strong> </div> <div class="markdown-body"> <p>@DuskoV, it would be much better for reviewers if you created pull request instead of posting code as plain text.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/cebe"><img src="https://avatars.githubusercontent.com/u/189796?v=4" />cebe</a> commented <strong> 12 years ago</strong> </div> <div class="markdown-body"> <blockquote> <p>I find it not SEO friendly to have styles, and other stuff included before </p> </blockquote> <p>Whats the problem with that?</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/DuskoV"><img src="https://avatars.githubusercontent.com/u/1567090?v=4" />DuskoV</a> commented <strong> 11 years ago</strong> </div> <div class="markdown-body"> <p>The problem is that <code><title></code> is pushed to the bottom in the <code><head></code>. Some crawlers might think of this to be strange. Also, if people have invalid HTML in the <code><head></code> , then it's also more possible that <code><title></code> isn't red correctly.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/samdark"><img src="https://avatars.githubusercontent.com/u/47294?v=4" />samdark</a> commented <strong> 11 years ago</strong> </div> <div class="markdown-body"> <p>@DuskoV all current search engine crawlers are reading titles regardless of where it's in the <code><head></code>. Also it's valid according to all HTML specs.</p> <p>Any reason to have invalid HTML in the <code><head></code> of your own pages?</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/qiangxue"><img src="https://avatars.githubusercontent.com/u/993322?v=4" />qiangxue</a> commented <strong> 11 years ago</strong> </div> <div class="markdown-body"> <p>It's unfounded that this would affect SEO. </p> <p>We cannot change the location now because it would break BC. By putting the scripts at the bottom of head, it also makes it impossible for developers to override these scripts.</p> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>