Skip to content

Instantly share code, notes, and snippets.

@himangshuj
Last active March 3, 2016 08:38
Show Gist options
  • Save himangshuj/108d78671673b821c45c to your computer and use it in GitHub Desktop.
Save himangshuj/108d78671673b821c45c to your computer and use it in GitHub Desktop.
inlineCssPrerenderplugin
var uncss = require('uncss');
var CleanCSS = require('clean-css');
module.exports = {
beforeSend: function(req, res, next) {
if(!req.prerender.documentHTML) {
return next();
}
var matches = req.prerender.documentHTML.toString().match(/<link(?:.*?)rel="stylesheet"(?:.*?)>/gi);
var styleSheets = [];
for (var i = 0; matches && i < matches.length; i++) {
var style= matches[i].replace(/<link(?:.*?)href=/gi,'').replace('>','');
if(matches.indexOf("media")===-1) {
style = style.replace(/"/g, '');
style = style.replace('//', 'http://');
styleSheets.push(style);
}
}
uncss(req.prerender.documentHTML.toString(),{stylesheets:styleSheets},function(err,output){
var minified = '<style>'+new CleanCSS().minify(output).styles+'</style>';
req.prerender.documentHTML=req.prerender.documentHTML.toString().replace('</title>','</title>'+minified);
next();
});
}
};
{
"author": "Prerender.io",
"name": "prerender",
"description": "Service to prerender Javascript rendered pages for SEO",
"version": "4.0.4",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/prerender/prerender"
},
"dependencies": {
"aws-sdk": "^2.1.8",
"cache-manager": "^0.16.0",
"clean-css": "^3.4.10",
"he": "^0.5.0",
"lodash": "^3.1.0",
"phridge": "^2.0.0",
"prerender-redis-cache": "^0.1.6",
"redis": "^2.4.2",
"uncss": "^0.13.0",
"xml2js": "^0.4.16"
},
"devDependencies": {
"mocha": "~1.13.0",
"sinon": "~1.7.3"
},
"scripts": {
"test": "./node_modules/.bin/mocha"
},
"bin": {
"prerender": "server.js"
},
"engines": {
"node": ">=0.12.9"
}
}
server.use(prerender.httpHeaders());
server.use(prerender.inlineCss());
server.use(require('prerender-redis-cache'));
// server.use(prerender.s3HtmlCache());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment