campaigningbureau / critical-laravel-routes
Automatically create and link Critical CSS for static routes in Laravel projects
Installs: 1 884
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
- illuminate/support: ^10.0||^11.0
This package is auto-updated.
Last update: 2024-11-23 08:36:56 UTC
README
Automatically create and link Critical CSS for static routes.
Supports Laravel 7.*
Installation
Just require the package via composer:
composer require campaigningbureau/critical-laravel-routes
The package gets automatically discovered by Laravel.
Usage
-
All static routes, that should use critical css, need to be named and have a
critical
attribute set totrue
:Route::get('/', ['as' => 'index', 'uses' => 'ViewController@index', 'critical' => true]);
-
Generate the JSON that contains all critical routes:
php artisan critical-urls:generate
-
The generated file needs to be required in the webpack.mix.js and set as
urls
when creating the critical css:// urls that need to be passed for critical css const criticalUrls = require('./critical-routes.json'); ... mix.criticalCss({ enabled: mix.inProduction(), paths: { base: 'http://your-website.com', templates: 'public/css/' }, urls: criticalUrls, options: { minify: true, }, });
When generating the css, one css file per defined route is created and saved.
-
Automatically import the content of the critical css file (if it exists for the route) in the blade template as inline styles:
{!! \CriticalLaravelRoutes::inlineCriticalCss() !!}
Alternatively, the critical css file can be linked with:
{!! \CriticalLaravelRoutes::linkCriticalCss() !!}
This can be used safely vor every existing route, so it is best to put it in the base layout. If no critical css file is found for a route, nothing is displayed.
Configuration
The config can be changed by publishing the configuration file:
$ php artisan vendor:publish --provider="CampaigningBureau\CriticalLaravelRoutes\CriticalLaravelRoutesServiceProvider"
It is possible to configure the name of the generated JSON file and the suffix for the critical css filess.