google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Nén tập tin JavaScript tự động trên Apache Nén (thu gọn) kích thước tập tin JavaScript là một trong những kĩ thuật quan trọng hàng đầu để tối ưu hóa hiệu suất hoạt động của một trang web. Và để thực hiện việc nén mã nguồn JavaScript, cách dễ dàng nhất là chúng ta sử dụng các thư viện được giới thiệu trong bài viết Hiệu suất của các thư viện nén JavaScript.

Tuy nhiên giải pháp này có mặt hạn chế là mỗi khi muốn thay đổi mã nguồn JavaScript, chúng ta cần phải dùng các công cụ này để biên dịch lại sau đó mới đăng tải lên website trực tuyến. Để khắc phục khuyết điểm này, chúng ta có thể dùng một giải pháp khác: thực hiện việc nén mã nguồn JavaScript ngay trên máy chủ và trả về kết quả mỗi khi được yêu cầu.

Đó chính là chủ đề của bài viết hướng dẫn JavaScript hôm nay: hướng dẫn xây dựng giải pháp nén mã nguồn JavaScript/tệp tin JavaScript ngay trên máy chủ Apache.


Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

One of the most recommended (and easiest to implement) techniques for web pages optimization is compressing the CSS and JavaScript files used.

While this is not a particularly difficult thing to do manually (for example, using the YUI compressor found here), when releasing a new version on the live server, it would be much easier to use an automated method that allows you to write the JavaScript in your own development style (using comments and a more loose formatting), and serve it directly compressed to the browser, on request.

Well... wonder no more! We're going to present an easy and bullet-proof method of doing just that, on an Apache web server. For this purpose, we're going to use Dean Edwards JavaScript's Packer class, more precisely its PHP version, "translated" by Nicolas Martin.

You're going to have to add it to your JavaScript folder (assuming you have such a folder in your project, otherwise... add it in your common folder, or... wherever you want). In the same folder, you have to add an index.php file that will load this PHP class and use it to dynamically compress your JS files.

The content of the JS folder's index.php file should be something like

if(!empty($_GET['script']) && is_file($_GET['script'] . '.js')){
require_once '/relative/path/to/JavaScriptPacker.class.php';

$script = file_get_contents($_GET['script'] . '.js');

$packer = new JavaScriptPacker($script, 62, true, false); // Default configuration
$script = $packer->pack();

header('content-type:application/javascript');
echo $script;
}

This, as you may notice, will receive a script parameter (type GET) and, if it will fit a file in your JS folder named as its value (plus the .js extension), will compress and output it.

In order to automatically call the index.php page with the JS file's name (without the .js extension), you need some .htaccess commands. These are:

Options All -Indexes # Disabled direct access to your JS folder's content

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/your/JavaScript/folder/

RewriteRule ^(.*)\.js$ ?script=$1 [L] # Here you instruct the server to pass any JS file called directly, to index.php as its script parameter
</IfModule>

As you may see, you must have the mod_rewrite module installed and active for this method to work.

If you have followed and understood all of the above... you now should access ONLY compressed JS files when calling them directly from your address bar (for example, try accessing http://your.domain.name/path/to/your/js/file.js - you should see something beginning with

eval(function(p,a,c,k,e,d){ ...

So... that's it! You now serve compressed (and somewhat "encoded") JavaScript files, thus minimizing the traffic and the time needed for the browsers to load your scripts.

One way of seeing the effects of this change is using either Page Speed or YSlow Firebug plug-ins. Try testing your web page without this compression method, and then, with it enabled. Depending on the size of your original script(s), you should be able to increase your ratings between 2-3 and up to 10 points.

Of course, you could go one step further, and try concatenating all of your scripts (in case you use more than one - and this is usually the case), and THEN compress it - you should gain up to 15 points in the performance rankings. But... I'll let you do that yourselves.

iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web