Skip to content

Commit bddabdc

Browse files
Fix inlining critical css (#448)
#414 inserts the server rendered app at the very end of the transform chain. The critical CSS inline transform ran _before_ the server rendered app was inserted, so it did not catch any selectors that were used in the app. This patch moves the critical CSS transform to the end so that it works on the entire server rendered app.
1 parent 08a125e commit bddabdc

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

lib/graph-document.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,17 @@ function node (state, createEdge) {
125125

126126
d.transform(addToHead, header.join(''))
127127

128+
d.transform(insertApp, {
129+
selector: selector,
130+
body: body
131+
})
132+
128133
if (state.styles.bundle.buffer.length) {
129134
d.transform(criticalTransform, { css: state.styles.bundle.buffer })
130135
}
131136

132137
d.transform(addToHead, styleTag({ hash: state.styles.bundle.hash, base: base }))
133138

134-
d.transform(insertApp, {
135-
selector: selector,
136-
body: body
137-
})
138-
139139
function complete (buf) { done(null, buf) }
140140

141141
pump(d.bundle(), concat({ encoding: 'buffer' }, complete), function (err) {

test/document.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,41 @@ tape('mount choo app into given selector', function (assert) {
289289
assert.notEqual(body.indexOf('meow'), -1, 'inserted the rendered app')
290290
})
291291
})
292+
293+
tape('inlines critical css', function (assert) {
294+
assert.on('end', cleanup)
295+
assert.plan(3)
296+
297+
var file = `
298+
var css = require('sheetify')
299+
var html = require('choo/html')
300+
var choo = require('choo')
301+
302+
css\`
303+
.classA { color: red }
304+
.classB { color: blue }
305+
\`
306+
307+
var app = choo()
308+
app.route('/', function () {
309+
return html\`<body class="classA"></body>\`
310+
})
311+
312+
// classB
313+
314+
module.exports = app.mount('body')
315+
`
316+
317+
var dirname = 'document-pipeline-' + (Math.random() * 1e4).toFixed()
318+
tmpDirname = path.join(__dirname, '../tmp', dirname)
319+
mkdirp.sync(tmpDirname)
320+
fs.writeFileSync(path.join(tmpDirname, 'index.js'), file)
321+
322+
var compiler = bankai(tmpDirname, { watch: false })
323+
compiler.documents('/', function (err, res) {
324+
assert.error(err, 'no error writing document')
325+
var body = res.buffer.toString('utf8')
326+
assert.notEqual(body.indexOf('.classA{color:red;}'), -1, 'inlined the .classA selector')
327+
assert.equal(body.indexOf('.classB{color:blue;}'), -1, 'did not inline the .classB selector')
328+
})
329+
})

0 commit comments

Comments
 (0)