Skip to content

Commit ee6ffd6

Browse files
author
Stujo
committed
Load critical CSS into page header of static page
Noticed a pop in the page when the client side renders it's because the main.css is excluded from the page render on the server side and then added This change allows for the main.css file to be included in the body of the page on the server side
1 parent b26a78c commit ee6ffd6

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

common/components/TodoApp.jsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import React, { Component, PropTypes } from 'react';
22
import DevTools from './DevTools';
33

4-
if ( 'undefined' !== typeof window ) {
5-
require( '../../client/assets/css/main.css' );
6-
}
7-
84
export default class TodoApp extends Component {
95

106
static contextTypes = {

server/server.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ app.use( ( req, res, next ) => {
6565

6666
.then( () => {
6767

68+
return new Promise((resolve, reject) => {
69+
fs.readFile('./client/assets/css/main.css', 'utf8', (err, data) => {
70+
if (err) {
71+
reject(err);
72+
return;
73+
}
74+
resolve(data);
75+
});
76+
});
77+
78+
})
79+
80+
.then( (criticalCSS) => {
81+
6882
const initView = renderToString((
6983
<Provider store={store}>
7084
<RouterContext {...renderProps} />
@@ -76,7 +90,9 @@ app.use( ( req, res, next ) => {
7690
let state = JSON.stringify( store.getState() );
7791
// console.log( '\nstate: ', state )
7892

79-
let page = renderFullPage( initView, state )
93+
94+
95+
let page = renderFullPage( initView, state, criticalCSS )
8096
// console.log( '\npage:\n', page );
8197

8298
return page;
@@ -89,15 +105,15 @@ app.use( ( req, res, next ) => {
89105
})
90106
})
91107

92-
93-
function renderFullPage(html, initialState) {
108+
function renderFullPage(html, initialState, criticalCSS) {
94109
return `
95110
<!doctype html>
96111
<html lang="utf-8">
97112
<head>
98113
<title>Universal Redux Example</title>
99114
<link rel="shortcut icon" type="image/png" href="assets/images/react.png">
100115
<link rel="stylesheet" href="/assets/css/uikit.almost-flat.min.css">
116+
<style type="text/css">${criticalCSS}</style>
101117
</head>
102118
<body>
103119
<div class="container">${html}</div>

0 commit comments

Comments
 (0)