Skip to content
Browse files

First version.

  • Loading branch information...
1 parent 6dbf874 commit 76834ee80c683731c20048dcadb5cbc6f815a65b @steeve committed Dec 25, 2012
Showing with 77 additions and 0 deletions.
  1. +57 −0 angular-seo-server.js
  2. +20 −0 angular-seo.js
View
57 angular-seo-server.js
@@ -0,0 +1,57 @@
+var system = require('system');
+
+if (system.args.length < 3) {
+ console.log("Missing arguments.");
+ phantom.exit();
+}
+
+var server = require('webserver').create();
+var port = parseInt(system.args[1]);
+var urlPrefix = system.args[2];
+
+var parse_qs = function(s) {
+ var queryString = {};
+ var a = document.createElement("a");
+ a.href = s;
+ a.search.replace(
+ new RegExp("([^?=&]+)(=([^&]*))?", "g"),
+ function($0, $1, $2, $3) { queryString[$1] = $3; }
+ );
+ return queryString;
+};
+
+var renderHtml = function(url, cb) {
+ var page = require('webpage').create();
+ page.settings.loadImages = false;
+ page.settings.localToRemoteUrlAccessEnabled = true;
+ page.onCallback = function() {
+ cb(page.content);
+ page.close();
+ };
+// page.onConsoleMessage = function(msg, lineNum, sourceId) {
+// console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
+// };
+ page.onInitialized = function() {
+ page.evaluate(function() {
+ document.addEventListener('__htmlReady__', function() {
+ window.callPhantom();
+ }, false);
+ });
+ };
+ page.open(url);
+};
+
+server.listen(port, function (request, response) {
+ var route = parse_qs(request.url)._escaped_fragment_;
+ var url = urlPrefix
+ + request.url.slice(1, request.url.indexOf('?'))
+ + '#!' + route;
+ renderHtml(url, function(html) {
+ response.statusCode = 200;
+ response.write(html);
+ response.close();
+ });
+});
+
+console.log('Listening on ' + port + '...');
+console.log('Press Ctrl+C to stop.');
View
20 angular-seo.js
@@ -0,0 +1,20 @@
+!function(window, document, undefined) {
+ var getModule = function(angular) {
+ return angular.module('seo', [])
+ .run(function($rootScope) {
+ $rootScope.htmlReady = function() {
+ this.$evalAsync(function() { // fire after $digest
+ setTimeout(function() { // fire after DOM rendering
+ var evt = document.createEvent('Event');
+ evt.initEvent('__htmlReady__', true, true);
+ document.dispatchEvent(evt);
+ }, 0);
+ });
+ };
+ });
+ };
+ if (typeof define == 'function' && define.amd)
+ define(['angular'], getModule);
+ else
+ getModule(angular);
+}(window, document);

0 comments on commit 76834ee

Please sign in to comment.
Something went wrong with that request. Please try again.