1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 网站SEO优化之CSS JS代码合并

网站SEO优化之CSS JS代码合并

时间:2019-01-12 04:14:34

相关推荐

网站SEO优化之CSS JS代码合并

php教程|php手册

网站,SEO,优化,CSS,代码,合并,优化,站过程中,

php教程-php手册

shopxx 5.0源码,ubuntu坏道修复软件,tomcat什么情况挂掉,爬虫陈磊,php弹屏,鹤壁如何做好seo关键词优化lzw

在优化网站的过程中,其中一个规则就是尽量减少HTTP的请求数量,比如图片的合并,JS,CSS代码的合并等,本文介绍了一款PHP的自动合并脚本,可以在网站中使用,只要将原来需要引用JS和CSS的地方,换成该脚本的函数调用,那么脚本就会自动将过个请求合并成一个

淘宝自动发货源码,Ubuntu没IP地址,如何改变tomcat的位置,爬虫报告下载,在线php视频,url三层对seo有影响吗lzw

卷宝网网站源码,vscode快速运行插件,ubuntu搜狗输入法安装,tomcat 进程消失,soci sqlite,学生网页设计代做,sql server数据库购买,南宁云服务器,飞飞2.9采集插件什么用,前端双向绑定的框架结构,339爬虫,菜鸟教程php,长春seo外包,springboot 教程例子,织梦二级导航标签,app企业网站模板,网页样式表,科技产品模板,dede后台路径破解,导航页面结构,easyui 管理系统 java,无线遥控编码C程序lzw

在优化网站的过程中,其中一个规则就是尽量减少HTTP的请求数量,比如图片的合并,JS,CSS代码的合并等,本文介绍了一款PHP的自动合并脚本,可以在网站中使用,只要将原来需要引用JS和CSS的地方,换成该脚本的函数调用,那么脚本就会自动将过个请求合并成一个请求,重而提高网站的打开速度。

Combining Assets

One of the basic tenets of making a web-page load quickly is reducing the number of HTTP requests that your page makes. One very common way of doing this, is assuring that all of your CSS files are combined into one request (ideally in the tag), and all of your javscript files are combined into one request (ideally at the bottom of the tag).

The Solution

I’ve worked with several large sites which each had their own solutions, but recently I found myself needing to speed up a fairly simple site:?Burndown for Trello.

To make things simple (and so that I’d never have to write this again), I made a?StaticResourcessystem which will allow me to put just one PHP script into the root directory of the site, and use a few simple calls to add files to the header or footer of the page.?The script requires no setup, installation, or configuration to run correctly. However, it has some optional advanced settings (which we’ll discuss at the end).

Usage

Usage is very simple. Just make a call to add files to the system, so that they’re included in the header or footer. Both local and external files are added with the same kind of call.

StaticResources – how to add files

PHP

StaticResources::addHeaderFile("css/GoogleWebfont_Tauri_Latin.css");StaticResources::addFooterFile("jquery-ui.js");StaticResources::addFooterFile("/ui/1.9.1/jquery-ui.js");

After you’ve added the files, make sure that somewhere in your code you print the HTML which will contain all of the files that were added

StaticResources – printing the script and style tags

PHP

// put this at the bottom of the HEAD tagprint StaticResources::getHeaderHtml();// put this at the bottom of the BODY tagprint StaticResources::getFooterHtml();

Here’s an example file of how you’d include a bunch of JS / CSS using this?StaticResources?system. It obviously won’t work if you don’t create JS/CSS files in the locations referenced, but this is very simple code to show the system in action.

StaticResources – EXAMPLE USAGE PAGE

PHP

????????????????????StaticResources example page????????????????????????????????????Created for Burndown for Trello.????????????????????????????StaticResources example page????????????This page is an example of loading static resources in combined files.????????????????????????????? - Sean Colombo????????????????????

The Code

Without further delay, this is the code of?static.php?that you should put in the main (eg: public_html) directory of your app.

static.php – THE ACTUAL CODE

PHP

array(), css => array());????private $_footerFilesByType = array(js => array(), css => array());????private $_headerAlreadyReturned = false;????private $_footerAlreadyReturned = false;????/**???? * Basic constructor - note that most of the interaction with this class will???? * be done using static functions which grab a singleton.???? */????public function __construct(){????????self::$ALLOWED_FILE_TYPES = unserialize( STATICRESOURCES_ALLOWED_FILE_TYPES );????????foreach(self::$ALLOWED_FILE_TYPES as $type){????????????$this->_headerFilesByType[$type] = array();????????????$this->_footerFilesByType[$type] = array();????????}????} // end constructor????/**???? * If your static resources are served from another domain (such as ), then set???? * that here. This string is prepended to the filename (eg: "static.php") when building the URL for the combined-file.???? */????public function mySetRootUrl($rootUrl){????????$this->staticResourcesRootUrl = $rootUrl;????}????/**???? * Adds the filename to the list of files to include in the head tag for its given???? * filetype.??If the fileName does not have an extension of one of the ALLOWED_FILE_TYPES,???? * then the file will NOT be added and a warning will be given instead.???? *???? * For these files to be used, the calling code must remember to print the results of???? * getHeaderHtml() in the tag.???? *???? * NOTE: The fileName must be structured relative to where THIS (StaticResources) file is,???? * and should not be in normal URL format, but rather in a format that this file can be read???? * from disk.??So doing "/directory/filename.ext" to get to the root of public_html will not work???? * unless the StaticResources file happens to be in public_html.??If this sounds confusing,???? * the simplest fix is to put this StaticResources file in your public_html directory, then you???? * can write file names exactly the way you would have written them in tags.???? *???? * @param fileType - if this is empty, the fileType will be detected from the filename. Sometimes???? * the filename might not have an extension (some JS libraries don have file extensions) so you may???? * need to specify this value. Should be "js" or "css".???? */????public function myAddHeaderFile($fileName, $fileType="", $warnIfAddedLate=true){????????// Make sure this file wasn added after the header files were already displayed.????????if( $warnIfAddedLate && $this->_headerAlreadyReturned){????????????$errorString = sprintf(self::ERR_ALREADY_OUTPUT_HEADER, $fileName);????????????trigger_error($errorString, E_USER_WARNING);????????}????????// If this file type is allowed, then remember it for later.????????if( $this->fileIsAllowed($fileName) ){????????????$ext = (empty($fileType) ? StaticResources::getFileExtension($fileName) : strtolower($fileType));????????????$this->headerFilesByType[ $ext ][] = $fileName;????????} else {????????????$ext = (empty($fileType) ? StaticResources::getFileExtension($fileName) : strtolower($fileType));????????????$errorString = sprintf(self::ERR_WRONG_FILETYPE, $ext, __FILE__);????????????trigger_error($errorString, E_USER_WARNING);????????}????} // end addHeaderFile()????/**???? * Adds the filename to the list of files to include at the bottom of the BODY tag.???? * If the fileName does not have an extension of one of the ALLOWED_FILE_TYPES,???? * then the file will NOT be added and a warning will be given instead.???? *???? * For these files to be used, the calling code must remember to print the results of???? * getFooterHtml() somewhere very late in the document, ideally at the very bottom of???? * the tag (right before ).???? *???? * @param fileType - if this is empty, the fileType will be detected from the filename. Sometimes???? * the filename might not have an extension (some JS libraries don have file extensions) so you may???? * need to specify this value. Should be "js" or "css".???? */????public function myAddFooterFile($fileName, $fileType="", $warnIfAddedLate=true){????????// Make sure this file wasn added after the footer files were already displayed.????????if( $warnIfAddedLate && $this->_footerAlreadyReturned){????????????$errorString = sprintf(self::ERR_ALREADY_OUTPUT_FOOTER, $fileName);????????????trigger_error($errorString, E_USER_WARNING);????????}????????// If this file type is allowed, then remember it for later.????????if( $this->fileIsAllowed($fileName) ){????????????$ext = (empty($fileType) ? StaticResources::getFileExtension($fileName) : strtolower($fileType));????????????$this->footerFilesByType[ $ext ][] = $fileName;????????} else {????????????$ext = (empty($fileType) ? StaticResources::getFileExtension($fileName) : strtolower($fileType));????????????$errorString = sprintf(self::ERR_WRONG_FILETYPE, $ext, __FILE__);????????????trigger_error($errorString, E_USER_WARNING);????????}????} // end addFooterFile()????/**???? * Returns a string which contains the HTML thats needed to???? * import the files in the HEAD tag.??This should be called exactly???? * once (and its results should be printed in the tag) on every???? * page.???? */????public function myGetHeaderHtml(){????????$html = "\n" . $this->getHtmlForArray( $this->headerFilesByType );????????$_headerAlreadyReturned = true;????????return $html;????} // end getHeaderHtml()????/**???? * Returns a string which contains the HTML thats needed to???? * import the files at the bottom of the BODY tag.??This should be called exactly???? * once (and its results should be printed at the bottom of the tag - right???? * before ) on every page.???? */????public function myGetFooterHtml(){????????$html = "\n" . $this->getHtmlForArray( $this->footerFilesByType );????????$_footerAlreadyReturned = true;????????return $html;????} // end getFooterHtml()????/**???? * Given an associative array of static resources whose keys are???? * resource types and whose values are arrays of fileNames (local???? * and/or remote), this will return a string which contains the???? * HTML thats needed to import those files.???? */????private function getHtmlForArray($filesByType){????????$html = "";????????// The URL of this script (which will act as an endpoint and serve up the actual content.????????$url = $this->staticResourcesRootUrl . basename(__FILE__);????????foreach($filesByType as $fileType => $files){????????????$localFiles = array();????????????foreach($files as $fileName){????????????????if(StaticResources::isRemoteFile($fileName)){????????????????????// Add the HTML for including the remote file.????????????????????if($fileType == "css"){????????????????????????$html .= "????????\n";????????????????????} else if($fileType == "js"){????????????????????????$html .= "????????\n";????????????????????} else {????????????????????????// Each file type needs to be included a certain way, and we don recognize this fileType.????????????????????????$errorString = sprintf(self::ERR_CANT_OUTPUT_FILETYPE, $fileType);????????????????????????trigger_error($errorString, E_USER_WARNING);????????????????????}????????????????} else {????????????????????$localFiles[] = $fileName;????????????????}????????????}????????????// Output the HTML which makes the request for the combined-file of all local files of the same fileType.????????????if(count($localFiles) > 0){????????????????// TODO: TWEAK SO THAT THE DELIMITER ISNT URL-ENCODED. MAKES IT MORE READABLE AND SHORTER.????????????????// TODO: TWEAK SO THAT THE DELIMITER ISNT URL-ENCODED. MAKES IT MORE READABLE AND SHORTER.????????????????$fullUrl = $url . "?files=".rawurlencode( implode(STATICRESOURCES_FILE_DELIMITER, $localFiles) );????????????????if($fileType == "css"){????????????????????$html .= "????????\n";????????????????} else if($fileType == "js"){????????????????????$html .= "????????\n";????????????????} else {????????????????????// Each file type needs to be included a certain way, and we don recognize this fileType.????????????????????$errorString = sprintf(self::ERR_CANT_OUTPUT_FILETYPE, $fileType);????????????????????trigger_error($errorString, E_USER_WARNING);????????????????}????????????}????????}????????return $html;????} // end getHtmlForArray()????/**???? * Returns true if the given fileName is allowed to be included, false otherwise.???? * The reason a file may not be allowed is that its of the wrong file-type. One???? * reason for this is that we don want attackers to request file types that may???? * contain password-files or source code that some users of this script might not???? * want to make public, etc..???? */????private function fileIsAllowed($fileName){????????$fileIsAllowed = true;????????if( !StaticResources::isRemoteFile($fileName)){????????????$fileExtension = strtolower( StaticResources::getFileExtension($fileName) );????????????$fileIsAllowed = in_array($fileExtension, self::$ALLOWED_FILE_TYPES);????????}????????return $fileIsAllowed;????} // end fileIsAllowed()????/**???? * Returns true if the fileName is from another site and false if it is from this site.???? */????public static function isRemoteFile($fileName){????????// If it starts with a protocol (ftp://, http://, https://, etc.) then it is remote (not local).????????return (0 myAddHeaderFile($fileName, $fileType, $warnIfLateAdded);????}????public static function addFooterFile($fileName, $fileType="", $warnIfLateAdded=true){????????$singleton = StaticResources::getSingleton();????????$singleton->myAddFooterFile($fileName, $fileType, $warnIfLateAdded);????}????public static function getHeaderHtml(){????????$singleton = StaticResources::getSingleton();????????return $singleton->myGetHeaderHtml();????}????public static function getFooterHtml(){????????$singleton = StaticResources::getSingleton();????????return $singleton->myGetFooterHtml();????}????public static function setRootUrl( $rootUrl ){????????$singleton = StaticResources::getSingleton();????????$singleton->mySetRootUrl($rootUrl);????}} // end class StaticResources

Using this script with a CDN

One drawback of serving a combined file is that your server has to read in all of the files, then print them all. This makes the call slightly slower than it has to be. One solution is to use a?CDN?to cache the files on your site. This way, even if it takes your server a second or so to generate the file the first time, it can be served to the next user instantly, from the CDN.

For?Burndown for Trello, we use?Fastly?as our CDN (disclosure: my fiance works there, but we use it because it’s free for one backend and it’s crazy fast).

The code to make this work with a CDN is very simple: one extra line below the include call:

How to serve the static assets from a CDN

PHP

include static.php;StaticResources::setRootUrl("http://b4t.global./");

Code minification

Another common performance trick is to use a?minifier?to reduce file-size. This hasn’t been added to the script yet, but may come in the future. If you have a favorite minifier, I’ve conspicuously commented in the code where it would make sense to run the minifier.

Minification takes a little while to run, so it’s highly recommended that you get a CDN if you’re using minificaiton.

The End

Hope you found the script useful. If you use it on your site, please link to it in the comments!

Thanks,

参考文章:

1.?/blog/posts/simple-single-file-php-script-for-combining-js-css-assets

The post 网站SEO优化之CSS JS代码合并 appeared first on 润物无声.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。