Skip to content

Commit

Permalink
use jsdom
Browse files Browse the repository at this point in the history
  • Loading branch information
cqr committed May 28, 2014
1 parent 5bb14b9 commit 67b9fa7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ bin
.DS_Store
.powder
src/common/.*
log
1 change: 1 addition & 0 deletions app.js
31 changes: 0 additions & 31 deletions lib/phantom-render.js

This file was deleted.

48 changes: 31 additions & 17 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
var express = require('express'),
spawn = require('child_process').spawn,
jsdom = require('jsdom'),
http = require('http'),
app = express(),
phantom;
app = express();

exports.listen = function (listenSpec, folder) {
phantom = spawn('phantomjs', [__dirname + '/phantom-render.js', 8081]);
app.use(express.static(folder));
app.use(function (req, res, next) {
if (typeof req.query._escaped_fragment_ !== 'undefined') {
var newReq = http.request({hostname: 'localhost', port: 8081, path: req.url}, function (pres) {
res.status(pres.statusCode);
res.writeHead(pres.headers);
pres.on('data', function (data) {
res.write(data);
});
pres.on('end', function () {
res.end();
});
jsdom.env({
file: folder + '/index.html',
url: 'https://m-staging.prx.org' + req.url,
src: ['window.callPhantom = function () { window.phantomCalled = true; }'],
features: {
FetchExternalResources: ["script"],
ProcessExternalResources: true
},
done: function (errors, window) {
if(window.phantomCalled) {
respond();
} else {
window.callPhantom = function () {
respond();
}
}

function respond() {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(window.document.documentElement.innerHTML);
res.end();
}
}
});
newReq.end();
} else {
next();
}
});
app.use(function (req, res) {
res.sendfile('index.html', {root: folder});
});
return app.listen(listenSpec).on('close', function () {
phantom.kill();
});

return app.listen(listenSpec);
};

if (!module.parent || module.parent.exports.PhusionPassenger) {
exports.listen(3000, __dirname + '/../public');
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"dependencies": {
"gzippo": "~0.2.0",
"express": "~3.3.7",
"phantomjs": "~1.9.7-1",
"bower": "^1.3.2"
},
"scripts": {
Expand Down Expand Up @@ -66,6 +65,7 @@
"gulp-gzip": "0.0.5",
"gulp-cachebust": "0.0.2",
"karma": "^0.12.9",
"bower": "^1.3.3"
"bower": "^1.3.3",
"jsdom": "^0.10.5"
}
}
1 change: 1 addition & 0 deletions src/common/prx-navigation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
angular.module('prxNavigation', ['ui.router'])
.directive('prxLoadingBar', function ($state, $stateParams, $injector, $q, $timeout, $rootScope, $animate) {
var modalKey = 'modal@';
console.log(window.callPhantom);

if (typeof window.callPhantom !== 'undefined') {
$animate.enabled(false);
Expand Down

0 comments on commit 67b9fa7

Please sign in to comment.