Giter VIP home page Giter VIP logo

critical-css's Introduction

critical-css Build Status

A Laravel package for generating and using inline critical-path CSS.

CriticalCss

Why?

For best performance, you may want to consider inlining the critical CSS directly into the HTML document. This eliminates additional roundtrips in the critical path and if done correctly can be used to deliver a “one roundtrip” critical path length where only the HTML is a blocking resource.

More information:

Table of Contents

Installation

1) Install the Critical npm package

This package is used to extract critical-path CSS from an HTML document.

From your project's base path, run:

$ npm install critical --save

Alternatively, install it globally:

$ npm install -g critical

2) Require the package

Next, you'll need to require the package using Composer:

From your project's base path, run:

$ composer require krisawzm/critical-css

3) Configure Laravel

Service Provider

Add the following to the providers key in config/app.php:

'providers' => [
    Alfheim\CriticalCss\CriticalCssServiceProvider::class,
];

Console

To get access to the criticalcss:clear and criticalcss:make commands, add the following to the $commands property in app/Console/Kernel.php:

protected $commands = [
    \Alfheim\CriticalCss\Console\CriticalCssMake::class,
    \Alfheim\CriticalCss\Console\CriticalCssClear::class,
];

Config

Generate a template for the config/criticalcss.php file by running:

$ php artisan vendor:publish

Note: Descriptions for the config options are only present in the config file, not in this readme. Click here to open the config file on GitHub.

Usage

Before getting started, I highly recommend reading through the config/criticalcss.php file. That will give you a good idea of how this all works.

Generating critical-path CSS

Providing everything is set up and configured properly, all you need to do in order to generate a fresh set of critical-path CSS files, is running the following command:

$ php artisan criticalcss:make

This will generate a unique file for each of the URIs (routes) provided.

See this commit for a diff of the implementation.

Using critical-path CSS with Blade templates

The service provider provides a new Blade directive named @criticalCss.

Simply call that directive, passing a route as the only argument, like so:

<html>
<head>
  ...
  @criticalCss('some/route')
</head>
</html>

If no argument is passed, the current route will be used, however, I highly recommend always passing a specific route.

And of course, make sure to asynchronously load the full CSS for the page using something like loadCSS (https://github.com/filamentgroup/loadCSS).

Full example (using Elixir to generate the URL for the CSS file, which or course is optional):

<html>
<head>
  ...
  @criticalCss('some/route')
  <script>
    !function(a){"use strict";var b=function(b,c,d){var g,e=a.document,f=e.createElement("link");if(c)g=c;else{var h=(e.body||e.getElementsByTagName("head")[0]).childNodes;g=h[h.length-1]}var i=e.styleSheets;f.rel="stylesheet",f.href=b,f.media="only x",g.parentNode.insertBefore(f,c?g:g.nextSibling);var j=function(a){for(var b=f.href,c=i.length;c--;)if(i[c].href===b)return a();setTimeout(function(){j(a)})};return f.onloadcssdefined=j,j(function(){f.media=d||"all"}),f};"undefined"!=typeof module?module.exports=b:a.loadCSS=b}("undefined"!=typeof global?global:this);
    loadCSS('{{ elixir('css/app.css') }}');
  </script>
</head>
</html>

For multiple views, you may wrap @criticalCss in a @section, then @yield the section in a master view.

A demo

I made a simple demo using this Bootstrap theme. It's a fairly simple theme, and it does not have any major performance issues, but yet, implementing inline critical-path CSS did improve performance.

Demo repo: https://github.com/kalfheim/critical-css-demo

See this commit for a diff of the implementation.

          | Mobile        | Desktop

------------- | ------------- | ------------- Before | | After | |

A note on Laravel 5.0 compatibility

On Laravel 5.0, you must set 'blade_directive' => false in the config. This is not recommended, but because Custom Directives were introduced in 5.1, it has to be done.

This will require adding the following to the aliases key in config/app.php:

'aliases' => [
    'Critical' => Alfheim\CriticalCss\Facades\Critical::class,
];

In your Blade views, you'll now be able to do the following instead of @criticalCss('some/route'):

{!! Critical::css('some/route') !!}

critical-css's People

Contributors

antonbormotov avatar exodusanto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

critical-css's Issues

Auth check and other if statements

Hi,

I just found that an issue I was having was because of the generator not being authorized. Because of this my user drop down menu was not showing properly when authorized. I can imagine other @if, @foreach statements will create this issue.

Is there solution for this in your package? I think implementing the include option would be of great service already.

Processing Error

Is it possible to give possible pointers on what can cause this error:

Error processing URI [/]. This is probably caused by the Critical npm package. Checklist: 1)critical_binis c orrect, 2)csspaths are correct 3) runnpm installagain.

I been breaking my head trying to figure this out. I have uninstalled, re-installed, followed the "check list" and no luck... Any Suggestions?

Laravel 5.5 Support

Amazing package! Any chance of getting this updated for Laravel 5.5 support?

Conclusion: don't install krisawzm/critical-css v2.1.1

When trying to install this package I got the following error, am I doing something wrong?

`[0] % composer require krisawzm/critical-css
Using version ^2.1 for krisawzm/critical-css
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Conclusion: don't install krisawzm/critical-css v2.1.1
- Conclusion: remove laravel/framework v5.7.14
- Installation request for krisawzm/critical-css ^2.1 -> satisfiable by krisawzm/critical-css[v2.1.0, v2.1.1].
- Conclusion: don't install laravel/framework v5.7.14
- krisawzm/critical-css v2.1.0 requires illuminate/view 5.0.|5.1.|5.2.|5.3.|5.4.* -> satisfiable by illuminate/view[5.0.x-dev, 5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, v5.0.0, v5.0.22, v5.0.25, v5.0.26, v5.0.28, v5.0.33, v5.0.4, v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8, v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7, v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9].
- don't install illuminate/view 5.0.x-dev|don't install laravel/framework v5.7.14
- don't install illuminate/view 5.1.x-dev|don't install laravel/framework v5.7.14
- don't install illuminate/view 5.2.x-dev|don't install laravel/framework v5.7.14
- don't install illuminate/view 5.3.x-dev|don't install laravel/framework v5.7.14
- don't install illuminate/view 5.4.x-dev|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.0.0|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.0.22|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.0.25|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.0.26|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.0.28|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.0.33|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.0.4|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.1|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.13|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.16|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.2|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.20|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.22|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.25|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.28|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.30|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.31|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.41|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.6|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.1.8|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.0|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.19|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.21|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.24|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.25|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.26|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.27|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.28|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.31|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.32|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.37|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.43|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.45|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.6|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.2.7|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.3.0|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.3.16|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.3.23|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.3.4|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.4.0|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.4.13|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.4.17|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.4.19|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.4.27|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.4.36|don't install laravel/framework v5.7.14
- don't install illuminate/view v5.4.9|don't install laravel/framework v5.7.14
- Installation request for laravel/framework (locked at v5.7.14, required as 5.7.*) -> satisfiable by laravel/framework[v5.7.14].

Installation failed, reverting ./composer.json to its original content.
`

I'm using laravel 5.7

Laravel 5.6 Support

Problem 1
- Conclusion: don't install krisawzm/critical-css v2.1.1
- Conclusion: remove laravel/framework v5.6.12

Any chance to have this package for Laravel 5.6 soon?

Thanks you.

Dynamically critical css

Hi,

good package you have created. I would self develop this feature. But your are now done. But one question what i have is.

How i can generate dynamically critical css?
I mean if you have a blog and you create a article. Than you have a new url like this: /blog/my-blog-entry . For this URL me need a now a critical css.

I hope it is clear what i mean.

Thanks

Update for Laravel 5.3

I assume the library will just work fine with Laravel 5.3 if it works fine with Laravel 5.2, however running composer require krisawzm/critical-css returned an error:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove laravel/framework v5.3.26
    - Conclusion: don't install laravel/framework v5.3.26
    - krisawzm/critical-css v2.0.0 requires illuminate/view 5.0.*|5.1.* -> satisfiable by illuminate/view[v5.0.0, v5.0.22, v5.0.25, v5.0.26, v5.0.28, v5.0.33, v5.0.4, v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8].
    - krisawzm/critical-css v2.0.1 requires illuminate/view 5.0.*|5.1.*|5.2.* -> satisfiable by illuminate/view[v5.0.0, v5.0.22, v5.0.25, v5.0.26, v5.0.28, v5.0.33, v5.0.4, v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8, v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7].
    - don't install illuminate/view v5.2.0|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.19|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.21|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.24|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.25|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.26|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.27|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.28|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.31|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.32|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.37|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.43|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.45|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.6|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.2.7|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.0.0|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.0.22|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.0.25|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.0.26|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.0.28|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.0.33|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.0.4|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.1|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.13|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.16|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.2|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.20|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.22|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.25|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.28|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.30|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.31|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.41|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.6|don't install laravel/framework v5.3.26
    - don't install illuminate/view v5.1.8|don't install laravel/framework v5.3.26
    - Installation request for laravel/framework (locked at v5.3.26, required as 5.3.*) -> satisfiable by laravel/framework[v5.3.26].
    - Installation request for krisawzm/critical-css ^2.0 -> satisfiable by krisawzm/critical-css[v2.0.0, v2.0.1].


Installation failed, reverting ./composer.json to its original content.

It looks like since 5.3 isn't listed as compatible in the composer.json file it gets a bit angry at me

Storage Folder

Hi,

How can I storage the generated files outside of the storage folder?

I'm asking that because I'd like to create the files in my localhost and send them to the production server, but because on the server the folder is a symlink so I can't send the css files in the storage folder.

Thanks,

DO NOT USE THIS PACKAGE

PSA for anyone who stumbles upon this:

Do not use this. It depends on vulnerable versions of NPM libraries.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.