壓縮 HTML 加速網(wǎng)頁響應(yīng)—— renatomarinho/laravel-page-speed,擴(kuò)展包可以自動的優(yōu)化網(wǎng)站,將HTML加速輸出顯示在客戶端瀏覽器。
項目地址:https://github.com/renatomarinho/laravel-page-speed
安裝命令:
$ composer require renatomarinho/laravel-page-speed
擴(kuò)展包中使用了中間件
//app/Http/Kernel.php
protected $middleware = [
...
\RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class, //轉(zhuǎn)變html中的行內(nèi)樣式
\RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class, //去除html中多余的屬性
\RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class, //去除html中所有的協(xié)議部分http或https,
\RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class, //檢測html中所有的鏈接,并進(jìn)行預(yù)加載
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class, //去除html中所有的注釋信息
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class, //去除html標(biāo)簽中的引號
\RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class, //去除html中所有的換行、空格等信息
]
注意:響應(yīng)式中間件的讀取順序是從下向上。
我們將中間件注冊在web,所有通過web訪問的數(shù)據(jù)都可以進(jìn)行壓縮
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
//壓縮 HTML 加速網(wǎng)頁響應(yīng)—— renatomarinho/laravel-page-speed
\RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\TrimUrls::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\RemoveQuotes::class,
\RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];