Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit 39c5525

Browse files
committed
Add simplistic inlining of critical CSS
1 parent 7c8ebb8 commit 39c5525

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "app-shell",
3-
"version": "0.1.120",
3+
"version": "0.1.141",
44
"private": true,
55
"license": "Apache",
66
"engines": {

server/controllers/static-page-controller.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
11
'use strict';
22

3+
var fs = require('fs');
4+
var path = require('path');
5+
36
function StaticPageController() {
47

58
}
69

10+
function prepareData(config) {
11+
// Concat inline styles for document <head>
12+
var flattenedStyles = '';
13+
var pathPrefix = '/../../dist/';
14+
config.inlineStyles.forEach(function(file) {
15+
flattenedStyles += fs.readFileSync(path.resolve(__dirname) + pathPrefix + file);
16+
});
17+
// Replace array with flattened string of content
18+
config.inlineStyles = flattenedStyles;
19+
return config;
20+
};
21+
722
StaticPageController.prototype.onRequest = function(req, res) {
823
switch (req.path) {
924
case '/':
10-
res.render('index', {
25+
res.render('index', prepareData({
1126
inlineStyles: ['/styles/core.css'],
1227
remoteStyles: [],
1328
inlineScripts: [],
1429
remoteScripts: ['/scripts/static-page.js']
15-
});
30+
}));
1631
break;
1732
case '/url-1':
18-
res.render('url-1', {
33+
res.render('url-1', prepareData({
1934
inlineStyles: ['/styles/core.css'],
2035
remoteStyles: [],
2136
inlineScripts: [],
2237
remoteScripts: ['/scripts/static-page.js']
23-
});
38+
}));
2439
break;
2540
case '/url-2':
26-
res.render('url-2', {
41+
res.render('url-2', prepareData({
2742
inlineStyles: ['/styles/core.css'],
2843
remoteStyles: [],
2944
inlineScripts: [],
3045
remoteScripts: ['/scripts/static-page.js']
31-
});
46+
}));
3247
break;
3348
default:
3449
res.status(404).send();

server/layouts/default.handlebars

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
<link rel="manifest" href="/manifest.json">
1212
<link rel="icon" href="/images/chrome-touch-icon-192x192.png" sizes="192x192" type="image/png">
1313

14-
{{#each inlineStyles}}
15-
<!-- TODO: Inline these styles -->
16-
<link rel="stylesheet" type="text/css" href="{{this}}">
17-
{{~/each}}
14+
<style>{{{inlineStyles}}}</style>
1815
</head>
1916
<body>
2017

0 commit comments

Comments
 (0)