Skip to content

Commit

Permalink
Render app shell with inlined critical CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 27, 2017
1 parent b97281d commit 8075ee8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 59 deletions.
8 changes: 7 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="<?php bloginfo( 'charset' ); ?>">
<style><?php @include( dirname( __FILE__ ) . '/dist/critical.css.php' ); ?></style>
<?php wp_head() ?>
</head>
<body <?php body_class(); ?>>
<div id="app"></div>
<div id="app">
<!-- App Shell -->
<main class="page">
<header class="sidebar"></header>
</main>
</div>
<?php wp_footer(); ?>
</body>
</html>
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"license": "GPL-2.0+",
"scripts": {
"clean": "rimraf dist",
"clean": "rimraf dist && mkdir -p dist",
"prebuild": "npm run clean",
"build": "cross-env NODE_ENV=production webpack",
"build-theme": "./bin/build-theme.sh",
Expand Down Expand Up @@ -63,6 +63,7 @@
"flatpickr": "^2.6.0",
"lodash": "^4.17.4",
"path-to-regexp": "^1.7.0",
"postcss-critical-css": "^2.1.3",
"preact": "^8.1.0",
"preact-redux": "^2.0.1",
"querystringify": "^1.0.0",
Expand Down
9 changes: 9 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
plugins: [
require( 'autoprefixer' ),
require( 'postcss-critical-css' )( {
outputPath: 'dist',
outputDest: 'critical.css.php'
} )
]
};
38 changes: 20 additions & 18 deletions src/assets/stylesheets/_elements.scss
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
*,
*:before,
*:after {
box-sizing: border-box;
}
@critical {
*,
*:before,
*:after {
box-sizing: border-box;
}

html {
font-size: $font-size;
background-color: $background-color;
}
html {
font-size: $font-size;
background-color: $background-color;
}

body {
@extend %sans;
margin: 0;
padding: 0 0 3rem;
font-size: 100%;
line-height: 1.5;
color: $font-color-body;
-webkit-font-smoothing: antialiased;
touch-action: manipulation;
body {
@extend %sans;
margin: 0;
padding: 0 0 3rem;
font-size: 100%;
line-height: 1.5;
color: $font-color-body;
-webkit-font-smoothing: antialiased;
touch-action: manipulation;
}
}

h1,
Expand Down
56 changes: 29 additions & 27 deletions src/components/sidebar/_style.scss
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
.sidebar {
display: flex;
height: $sidebar-width;
background-color: #fff;
border: 0 solid $border-color;
border-bottom-width: 1px;
@critical {
.sidebar {
display: flex;
height: $sidebar-width;
background-color: #fff;
border: 0 solid $border-color;
border-bottom-width: 1px;

@include medium() {
position: fixed;
left: 0;
top: 0;
right: 0;
z-index: 1000;
height: $sidebar-width-medium;
}
@include medium() {
position: fixed;
left: 0;
top: 0;
right: 0;
z-index: 1000;
height: $sidebar-width-medium;
}

@include desktop() {
right: auto;
bottom: 0;
flex-direction: column;
width: $sidebar-width-medium;
height: auto;
border-right-width: 1px;
border-bottom-width: 0;
}
@include desktop() {
right: auto;
bottom: 0;
flex-direction: column;
width: $sidebar-width-medium;
height: auto;
border-right-width: 1px;
border-bottom-width: 0;
}

body.admin-bar & {
top: 46px;
body.admin-bar & {
top: 46px;

@include desktop() {
top: 32px;
@include desktop() {
top: 32px;
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ const path = window.location.href.substr( SITE_URL.length );
store.dispatch( replaceRoute( path ) );

// Render
const target = document.getElementById( 'app' );
render(
<Provider store={ store }>
<Root />
</Provider>,
document.getElementById( 'app' )
target,
target.children[ 0 ]
);

// Preloaded responses are only valid for first render
Expand Down
16 changes: 5 additions & 11 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ if ( 'production' === process.env.NODE_ENV ) {
test: /\.s?css$/,
loader: ExtractTextPlugin.extract( {
use: [
{
loader: 'postcss-loader',
options: {
plugins: [
require( 'autoprefixer' )
]
}
},
'postcss-loader',
{
loader: 'sass-loader',
query: {
Expand All @@ -92,9 +85,10 @@ if ( 'production' === process.env.NODE_ENV ) {
config.module.rules.push( {
test: /\.s?css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
{ loader: 'sass-loader' }
'style-loader',
'css-loader',
'postcss-loader',
'sass-loader'
]
} );
}

0 comments on commit 8075ee8

Please sign in to comment.