Skip to content

Commit

Permalink
Inline critical path styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbohacek committed Nov 4, 2020
1 parent 321f737 commit ba00891
Show file tree
Hide file tree
Showing 5 changed files with 787 additions and 40 deletions.
1 change: 1 addition & 0 deletions css-critical/styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 71 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,80 @@ var gulp = require('gulp'),
notify = require('gulp-notify'),
browserSync = require('browser-sync'),
reload = browserSync.reload,
sourcemaps = require('gulp-sourcemaps');
criticalCss = require('gulp-penthouse'),
penthouse = require('penthouse-pages');

function swallow_error (error) {
console.log(error.toString());
this.emit('end');
}


gulp.task('critical-css', function () {
return gulp.src('./css/styles.min.css')
.pipe(criticalCss({
out: './styles.css',
url: 'http://botwiki.local/bot/emoji__polls',
width: 1300,
height: 900,
strict: true,
userAgent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
}))
.pipe(minifycss())
.on('error', swallow_error)
.pipe(gulp.dest('./css-critical/'));
});

// gulp.task('critical-css', function() {
// return penthouse({
// pages: [
// {
// name: 'home',
// url: '',
// },
// {
// name: 'bot',
// url: '/bot/emoji__polls/',
// },
// ],
// baseUrl: 'http://botwiki.local/',
// dest: './css-critical/',
// css: './css/styles.min.css',
// width: 1300,
// height: 900,
// strict: true,
// userAgent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
// });
// });


// gulp.task('critical-css', function () {
// return gulp.src('./css/styles.min.css')
// .pipe(criticalCss({
// pages: [
// {
// name: 'home',
// url: 'http://botwiki.local/',
// },
// {
// name: 'bot',
// url: 'http://botwiki.local/bot/emoji__polls/',
// },
// ],
// baseUrl: 'http://botwiki.local/',
// dest: './css-critical/',
// css: './css/styles.min.css',
// width: 1300,
// height: 900,
// strict: true,
// userAgent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
// }))
// // .pipe(cssNano({
// // safe:true // this isn't required, but I've included cssNano to minify the output file
// // }))
// .pipe(gulp.dest('./css-critical/')); // destination folder for the output file
// } );

gulp.task('browser-sync', function () {
var files = [
'css/*.css',
Expand Down Expand Up @@ -116,12 +183,12 @@ gulp.task('clean', function() {
});

gulp.task('watch', function() {
gulp.watch('src/admin-styles/**/*.*', ['admin-styles']);
gulp.watch('src/styles/**/*.*', ['styles']);
gulp.watch('src/admin-styles/**/*.*', ['admin-styles','critical-css']);
gulp.watch('src/styles/**/*.*', ['styles','critical-css']);
gulp.watch('src/admin-scripts/**/*.*', ['jslint', 'admin-scripts']);
gulp.watch('src/scripts/**/*.*', ['jslint', 'scripts']);
});

gulp.task('default', ['clean'], function() {
gulp.start('styles', 'admin-styles', 'jslint', 'admin-scripts', 'scripts', 'browser-sync', 'watch');
gulp.start('styles', 'critical-css', 'admin-styles', 'jslint', 'admin-scripts', 'scripts', 'browser-sync', 'watch');
});
2 changes: 2 additions & 0 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
global $wp_query;
global $page_title;

echo '<style id="critical-css">' . file_get_contents( get_template_directory() . '/css-critical/styles.css' ) . '</style>';

if ( isset( $wp_query->query['post_type'] ) ){
$post_type = $wp_query->query['post_type'];
} else {
Expand Down
Loading

0 comments on commit ba00891

Please sign in to comment.