Explore. Grow & Outshine

Laravel

Tối ưu Laravel bằng Laravel Page Speed package

Giới thiệu

renatomarinho/laravel-page-speed giúp tối ưu HTML/Blade lên đến 35%. Theo lộ trình phát triển được tác giả đăng trên Github trong tương lai sẽ có thêm các chức năng tối ưu CSS, Javascript, Cache …

Trước khi tối ưu

Sau khi tối ưu

Cài đặt

Yêu cầu hệ thống

  • PHP 7.2.5+
  • Laravel 6.0+

Cài đặc thông qua Composer:

composer require renatomarinho/laravel-page-speed

Package hỗ trợ Package Discovery

php artisan vendor:publish --provider="RenatoMarinho\LaravelPageSpeed\ServiceProvider"

Cấu hình

Bật / tắt chức năng

Vì package này có nhiều chức năng, nên muốn sử dụng chức năng nào chỉ cần thêm nó vào Kernel

//app/Http/Kernel.php

protected $middleware = [
    ...
    \RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class,
    \RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class,
    \RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class,
    \RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class,
    //\RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class, 
    //\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class,
    \RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class, // Note: This middleware invokes "RemoveComments::class" before it runs.
    \RenatoMarinho\LaravelPageSpeed\Middleware\DeferJavascript::class,
]

Remove Comments

\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class

RemoveComments::class sẽ xóa các HTML, JS và CSS comment. Tùy thuộc vào tệp HTML, bộ lọc này có thể giảm đáng kể số lượng byte được truyền trên mạng.

Collapse Whitespace

\RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class

CollapseWhitespace::class giảm các byte được truyền trong tệp HTML bằng cách loại bỏ khoảng trắng không cần thiết.

Remove Quotes

\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class
// Trước
<img src="hotantien.png" align='left' alt="" border="0" width='70' height='30' >

// Sau khi tối ưu
<img src=hotantien.png align=left alt="" border=0 width=70 height=30 >

Elide Attributes

Insert DNSPrefetch

Trim Urls

Inline Css

\RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class

InlineCss::class chuyển inline “style” của các thẻ thành các class bằng cách di chuyển CSS đến header.

Defer Javascript

\RenatoMarinho\LaravelPageSpeed\Middleware\DeferJavascript::class

DeferJavascript::class thêm thuộc tính defers cho các Javascript <script> trong tệp HTML.

Bật / tắt package

//config/laravel-page-speed.php

//Set this field to false to disable the laravel page speed service.
'enable' => env('LARAVEL_PAGE_SPEED_ENABLE', true),

Bỏ qua các Routes không cần thiết

//config/laravel-page-speed.php

//You can use * as wildcard.
'skip' => [
    '*.pdf', //Ignore all routes with final .pdf
    '*/downloads/*',//Ignore all routes that contain 'downloads'
    'assets/*', // Ignore all routes with the 'assets' prefix
];
Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận
0
Rất thích suy nghĩ của bạn, hãy bình luận.x