choojs / bankai

:station: - friendly web compiler
Apache License 2.0
1.09k stars 102 forks source link

🐛 `.documents`: Documented API missing #526

Open perguth opened 5 years ago

perguth commented 5 years ago

This a 🐛 bug report.

.documents is not accepting an object as second parameter even though it's documented in the README.

Expected Behavior

var someState = { render: 'me' }

// Documented API:
// `compiler.documents(routename, [opts], done(err, { buffer, hash }))`
compiler.documents('/', {
  state: someState
}, noop)

// Render the document passing `state` to the render function.

Current Behavior

uncaughtException { AssertionError [ERR_ASSERTION]: 'object' == 'function'
    at Bankai.documents (/application/node_modules/bankai/index.js:172:10)
    at Server.http.createServer (/application/server.js:107:17)
    at Server.emit (events.js:189:13)
    at parserOnIncoming (_http_server.js:676:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
  generatedMessage: true,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: 'object',
  expected: 'function',
  operator: '==' }

Possible Solution

Maybe build on https://github.com/choojs/bankai/pull/446?

Context

I would like to just prerender the document instead of letting the client wait to receive it's initial state via WebSockets.

Code Sample

const server = http.createServer((request, response) => {
  if (request.url === '/') {
    compilerCli.documents('/', { state }, (error, node) => {
      response.setHeader('content-type', 'text/html')
      gzip(node.buffer, request, response)
    })
  }
  compilerHtml(request, response, function () {
    response.statusCode = 404
    response.end('not found')
  })
})

Your Environment

Software Version(s)
Choo 6.13.3
perguth commented 5 years ago

Maybe @yoshuawuyts ? 🎈