Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get critical css from multiple external URLs? #215

Closed
jahvi opened this issue Jun 13, 2017 · 6 comments
Closed

How to get critical css from multiple external URLs? #215

jahvi opened this issue Jun 13, 2017 · 6 comments

Comments

@jahvi
Copy link

jahvi commented Jun 13, 2017

I currently have the following gulpfile.js that fetches the critical css from a couple URLs and creates 2 separate files:

var gulp = require('gulp');
var purge = require('gulp-css-purge');
var concatCss = require('gulp-concat-css');
var critical = require('critical');

gulp.task('critical', ['generate'], function () {
    gulp.src(['./css/critical-*.css'])
        .pipe(concatCss('./css/critical.css'))
        .pipe(purge())
        .pipe(gulp.dest('.'));
});

gulp.task('generate', function () {
    critical.generate({
        base: './',
        src: 'http://example.com/category.html',
        dimensions: [{
            width: 1280,
            height: 960
        }],
        dest: 'css/critical-category.css',
        minify: true,
        extract: false,
        pathPrefix: '/css',
        ignore: ['font-face']
    });

    critical.generate({
        base: './',
        src: 'http://example.com/',
        dimensions: [{
            width: 1280,
            height: 960
        }],
        dest: 'css/critical-home.css',
        minify: true,
        extract: false,
        pathPrefix: '/css',
        ignore: ['font-face']
    });
});

As you can see the only reason I'm creating the critical-*.css files is to later concatenate them and remove duplicate content into a single critical.css file. Is there a more efficient way to do this? I've been trying to use the generated critical css as part of a stream like on the README but I haven't been able to get it to work using external URLs.

@zomars
Copy link

zomars commented Mar 20, 2018

I'm also looking for this! Maybe src could receive an array of URLs as input.

@mainak-grmtech
Copy link

mainak-grmtech commented Aug 3, 2018

Here is the code to get critical css from multiple external URL:

var HostUrl= 'http://example.com/';

var UrlForGeneratingCriticalCss = {
    homepage: ['home'
    ],
    category-pages:['category-1','category-2'],
    blog-pages:['blog','blog-inner'],
};

gulp.task('criticalloop', function () {
    for (var key in UrlForGeneratingCriticalCss) {
        var bundle = key;

        for(var EachUrl in UrlForGeneratingCriticalCss[bundle])
        {
           var TargetUrl = HostUrl+UrlForGeneratingCriticalCss[bundle][EachUrl];
           var outputCssName = './css/critical-'+UrlForGeneratingCriticalCss[bundle][EachUrl]+'.css';
           critical.generate({
                src: TargetUrl,
                dest: outputCssName,
                minify: true,
                ignore: ['@font-face',/url\(/]
            });
        }
    }
});

@bezoerb
Copy link
Collaborator

bezoerb commented Oct 18, 2018

relates to #320

@pratham2003
Copy link

@jahvi
How are you loading your critical CSS?
Inlining it into HTML or loading as a file in HEAD?

If inlining you don't need to concatenate them, just load the critical CSS unique to that page template.

@jahvi
Copy link
Author

jahvi commented Oct 19, 2018

I'm inlining them, the problem is that I don't have that much control over what to load when (because of limitations on the CMS system) so in our case it would be a lot simpler to inline all critical path css in the head.

For now @mainak-grmtech solutions works very well as a workaround.

@bezoerb
Copy link
Collaborator

bezoerb commented Nov 30, 2018

We'll consider adding this feature to v2.1 after the rewrite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants