Files
meteor/packages/spiderable/phantom_script.js
Andrew Wilcox 5f1c8f2eeb Add spiderable support for hash fragments
Since the browser application cache appears not to support URL path
routes in a non-buggy way (see
https://github.com/meteor/meteor/pull/2926), applications using the
appcache package will want to use hash fragment routes instead.

This PR adds support to the spiderable package for hash fragment
routes.  An original URL such as `http://example.com/#!a=1&b=2` will
be encoded by a search engine as an escaped fragment, decoded by the
spiderable package, passed through to the phantomjs process, and
appear to the phantom client as `#!a=1&b=2` in `window.location.hash`
(the same as when the original URL is opened in a regular browser).
2015-01-26 15:54:47 -08:00

40 lines
1013 B
JavaScript

// 'url' is assigned to in a statement before this.
var page = require('webpage').create();
var isReady = function () {
return page.evaluate(function () {
if (typeof Meteor === 'undefined'
|| Meteor.status === undefined
|| !Meteor.status().connected) {
return false;
}
if (typeof Package === 'undefined'
|| Package.spiderable === undefined
|| Package.spiderable.Spiderable === undefined
|| !Package.spiderable.Spiderable._initialSubscriptionsStarted) {
return false;
}
Tracker.flush();
return DDP._allSubscriptionsReady();
});
};
var dumpPageContent = function () {
var out = page.content;
out = out.replace(/<script[^>]+>(.|\n|\r)*?<\/script\s*>/ig, '');
out = out.replace('<meta name="fragment" content="!">', '');
console.log(out);
};
page.open(url, function(status) {
if (status === 'fail')
phantom.exit();
});
setInterval(function() {
if (isReady()) {
dumpPageContent();
phantom.exit();
}
}, 100);