Detailed explanation of using Minify to compress css and js files
How to use Minify to compress css and js files
Minify is an application developed with PHP5, by following some Yahoo Optimization rules to improve website performance. It merges multiple CSS or JavaScript files, removes unnecessary whitespace and comments, performs gzip compression, and sets browser cache headers. Minify is very similar in design to Yahoo's Combo Handler Service, but Minify can merge any JavaScript and CSS files you want.
Generally, the bottleneck of website speed is at the front end, and the most critical thing is the loading speed of resources. However, most browsers have a limit on the number of concurrent requests for a single domain name, so if there are many requests on a page, Resources, such as CSS and JavaScript files, will obviously reduce the loading speed of the website. A better way to handle it is to access multiple files through one request. This will not affect the previous file maintenance and reduce the clear number of resources. Minify is made for this. The following are some Yahoo! optimization guidelines adopted by Minify:
Minify compresses and reduces CSS and JS (Minify: removes spaces, carriage returns, etc.), and integrates multiple CSS and JS files into one file . Don't think that your large bandwidth doesn't warrant this type of optimization. The more important reason for using it is file merging, not compression, but file consolidation, which can reduce the browser side from constantly issuing new connection requests. Just like an FTP server, multiple small files and one large file are time-consuming. Different.
Minify is written in PHP, project address http://code.google.com/p/minify/
1. Download the latest Minify and unzip it to the minify directory.
2. Copy the "min" directory to your DOCUMENT_ROOT, which is the root directory of the website.
Basic usage
For example: http://localhost/a.js, http://localhost/b.js two files. So now, you can use http://localhost/min/?f=a.js,b.js and see if the result returned by the browser is the content of the two js files of minify?
Attachment README.txt
citation in the min directory
The files in this directory contain default Minify settings designed to simplify integrating your website . Minify will merge minified JavaScript or CSS files and perform HTTP compression and caching headers.
Recommendation
It is recommended to modify config.php to set $min_cachePath to a PHP writable directory. This will improve performance.
GETTING STARTED
The fastest way to get started with Minify is to visit your website using the URI of the Minify Builder application:
http://liqingbo.cn /min/builder/Compress a single file
For example, you want to serve this file:
http://liqingbo.cn/ wp-content/themes/default/default.cssThe following is the "Minify URL" file:
In other words, the "f" parameter is set to the target file from the WEB root path (no path / is required). Since the CSS file May contain relative URIs, Minify will automatically find them through the rewriting mechanism.
Merge multiple files into one file download
Separate each file name of the f parameter with ','.
For example, there is the following CSS file:
http://liqingbo.cn/scripts/jquery-1.2.6.jsYou can combine it through Minify:
If the files you merge share the same parent directory, you You can use the basic directory of the f parameter set by the b parameter (also excluding leading or suffix/characters).
For example, the following two writing methods have the same effect:
http:// liqingbo.cn/min/?f=scripts/jquery-1.2.6.js,scripts/site.js,scripts/home.jsUse MINIFY
in Html in (X)HTML files, do not Forgot to replace & with &
Specify allowed directories
By default, Minify will not have any *.css/*.js files in the DOCUMENT_ROOT scope. If you wish to restrict Minify's access to certain directories, set the
$min_serveOptions ['minApp'] ['allowDirs'] array in config.php. For example: to limit to the /js and /themes/default directories, use:
"Groups": Faster performance and better URLs
For best performance, edit the pre-specified file groups in groupsConfig.php
The following is an example configuration:
return array( 'js' => array('//js/Class.js', '//js/email.js') );
The above pre-specified js will result in merging the following files:
http://example.com/js/Class. js Now, you can simplify the URL like this:
Group: Specify files outside the document_root directory
在groupsConfig.php
阵中,//是指向DOCUMENT_ROOT
,但你也可以指定从系统的绝对目录路径或相对于document_root的相对目录:
return array( 'js' => array( '//js/file.js' // file within DOC_ROOT ,'//../file.js' // file in parent directory of DOC_ROOT ,'C:/Users/Steve/file.js' // file anywhere on filesystem ) );
未来过期HTTP头
Minify可以发送未来(一年)过期HTTP头。要启用该功能,您必须添加一个数字到URIs(例如/min/?g=js&1234 or /min/f=file.js&1234),每当源文件修改时改变该数字。如果你使用SVN/CVS,你可以考虑使用修订版号作为该数字。
如果使用"组"来合并压缩你的文件,可以使用工具函数Minify_groupUri()来得到一个“版本”的URI。例如:
// 之前确保min/lib目录设置到include_path // add /min/lib to your include_path first! require $_SERVER['DOCUMENT_ROOT'] . '/min/utils.php'; $jsUri = Minify_groupUri('js'); echo " ";
调试模式
在调试模式下,Minify不压缩文件,而是发送合并后的带有行号的文件。要启用该模式,在config.php中设置为$min_allowDebugFlag为true,并增加"&debug=1" 到你的URIs.
例如:/min/?f=script1.js,script2.js&debug=1
注:对于该模式,注释风格的字符串正则表达式可能会导致问题。
把min目录上传根目录,根目录打开http://example.com/min/
Note: Please set $min_cachePath in /min/config.php to improve performance.
设置/min/config.php文件 ,$min_cachePath 有3个选择。
//$min_cachePath = ‘c://WINDOWS//Temp’; //$min_cachePath = ‘/tmp’; //$min_cachePath = preg_replace(‘/^//d+;/’, ”, session_save_path());
选择第2个,去除// .设置tmp属性777
在显示的界面中加入你想合并压缩的 js/css 路径,点击 ‘Update’ 之后会为你生成一个 url
如:http://localhost/min/b=googletesting/js& f=mootools.js,iAction.js,iAjax.js,global.js
,css
和JS
分别合并,2个地址。
如果需要组合的文件很多,url 就会变得很长,Minify 支持 group,可以将这些文件分组,这样 url 中只需指定 g=group名字 就可以了。
安装完毕后删除min/builder/index.php
文件。防止其他人登陆!后期如需编辑再次上传!
如有不明白的地方,可以给我留言,我们可以一起探讨一下。
推荐教程:《JS基础教程》
The above is the detailed content of Detailed explanation of using Minify to compress css and js files. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
