Skip to content

Instantly share code, notes, and snippets.

@SamuelMarks
Created July 26, 2013 04:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamuelMarks/6086271 to your computer and use it in GitHub Desktop.
Save SamuelMarks/6086271 to your computer and use it in GitHub Desktop.
Google AngularJS SEO talk from July, 2013
/* render website with JS and return its rendered output
Syntax: node nodejs_phantomjs_render.js [url] [output {default=stdout}] */
var system = require('system'),
fs = require('fs'),
page = new WebPage(),
url = system.args[1],
output = system.args[2],
result;
page.open(url, function (status) {
if (status !== 'success') {
console.log('FAILED to load the url');
phantom.exit();
} else {
result = page.evaluate(function () {
var html, doc;
html = document.querySelector('html');
return html.outerHTML;
});
if (output) {
var rendered = fs.open(output, 'w');
rendered.write(result);
rendered.flush();
rendered.close();
} else {
console.log(result);
}
}
phantom.exit();
});
/* pure phantomjs version
Syntax: phantomjs phantomjs_render.js
Easy to use with CLI args and/or node also, just copy syntax from nodejs_phantomjs_render.js */
var page = require('webpage').create();
page.open('http://localhost:8000/app/index.html', function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var p = page.evaluate(function () {
return document.getElementsByTagName('html')[0].innerHTML
});
console.log(p); // Can output to file instead, just copy syntax from nodejs_phantomjs_render.js
}
phantom.exit();
});
/* Renders sitemap.xml in syntax of http://www.sitemaps.org/protocol.html
See final line for syntax use.
Easy to use with CLI args also, just copy syntax from nodejs_phantomjs_render.js */
var fs = require('fs'),
jsdom = require('jsdom');
var dir_name = require('path').dirname(require.main.filename);
console.log('Working dir: ' + dir_name);
var static_index = fs.readFileSync('static_index.html', 'utf-8');
var document = jsdom.jsdom(static_index);
// Building sitemap from the `<a href=""></a>` links.
// Trivial to extend recursively through subpages.
var all_href = [], l = document.links;
for (var i = 0; i < l.length; i++) {
all_href.push(l[i].href);
}
function generate_xml_sitemap(root, url_array) {
var urls = url_array;
// the root of your website - the protocol and the domain name with a trailing slash
var root_path = root;
// XML sitemap generation starts here
var priority = 0.5;
var freq = 'monthly';
var xml = '<?xml version="1.0" encoding="UTF-8"?>';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
for (var i in urls) {
xml += '<url>';
xml += '<loc>' + root_path + urls[i] + '</loc>';
xml += '<changefreq>' + freq + '</changefreq>';
xml += '<priority>' + priority + '</priority>';
xml += '</url>';
i++;
}
xml += '</urlset>';
return xml;
}
console.log(generate_xml_sitemap('http://localhost:8000/', all_href));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment