Skip to content

Instantly share code, notes, and snippets.

@likr
Created January 4, 2018 04:32
Show Gist options
  • Select an option

  • Save likr/1e452c10395d7ad2bc993e10cfdeb613 to your computer and use it in GitHub Desktop.

Select an option

Save likr/1e452c10395d7ad2bc993e10cfdeb613 to your computer and use it in GitHub Desktop.
プリレンダリングのテスト
/* global phantom */
var url = 'http://localhost:8080/posts'
function ready () {
console.log(window.fetch)
return window.prerenderReady
}
var page = require('webpage').create()
page.onResourceRequested = function (request) {
console.log('Request ' + JSON.stringify(request, undefined, 4))
}
page.onResourceReceived = function (response) {
console.log('Receive ' + JSON.stringify(response, undefined, 4))
}
page.open(url, function (status) {
console.log('hoge', status)
if (status === 'success') {
page.render('example.png')
}
setInterval(function () {
var finished = page.evaluate(ready)
console.log(finished)
if (finished) {
phantom.exit()
}
page.render('hoge.png')
}, 1000)
})
page.onError = function (msg, trace) {
var msgStack = ['ERROR: ' + msg]
if (trace && trace.length) {
msgStack.push('TRACE:')
trace.forEach(function (t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''))
})
}
console.log(msgStack.join('\n'))
return true
}
phantom.onError = function (msg, trace) {
var msgStack = ['ERROR: ' + msg]
if (trace && trace.length) {
msgStack.push('TRACE:')
trace.forEach(function (t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''))
})
}
console.log(msgStack.join('\n'))
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment