1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Return a promise from getRequestHandler execution, await _serveStatic in serveStaticWithGzip, update custom-server-hapi to complete request lifecycle (#1099)

This commit is contained in:
sfhardman 2017-02-13 15:12:32 +13:00 committed by Guillermo Rauch
parent b220193167
commit dbc2ceefde
4 changed files with 51 additions and 27 deletions

View file

@ -2,7 +2,12 @@ const pathWrapper = (app, pathName, opts) => ({ raw, query }, hapiReply) =>
app.renderToHTML(raw.req, raw.res, pathName, query, opts)
.then(hapiReply)
const defaultHandlerWrapper = app => ({ raw, url }, hapiReply) =>
app.run(raw.req, raw.res, url)
const defaultHandlerWrapper = app => {
const handler = app.getRequestHandler()
return ({ raw, url }, hapiReply) =>
handler(raw.req, raw.res, url)
.then(() => {
hapiReply.close(false)
})
}
module.exports = { pathWrapper, defaultHandlerWrapper }

View file

@ -8,6 +8,8 @@
"hapi": "^16.1.0",
"next": "^2.0.0-beta",
"react": "^15.4.2",
"react-dom": "^15.4.2"
"react-dom": "^15.4.2",
"good": "^7.1.0",
"good-console": "^6.2.0"
}
}

View file

@ -1,37 +1,54 @@
const next = require('next')
const Hapi = require('hapi')
const Good = require('good')
const { pathWrapper, defaultHandlerWrapper } = require('./next-wrapper')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const server = new Hapi.Server()
// add request logging (optional)
const pluginOptions = [
{
register: Good,
options: {
reporters: {
console: [{
module: 'good-console'
}, 'stdout']
}
}
}
]
app.prepare()
.then(() => {
server.connection({ port: 3000 })
server.register(pluginOptions)
.then(() => {
server.route({
method: 'GET',
path: '/a',
handler: pathWrapper(app, '/a')
})
server.route({
method: 'GET',
path: '/a',
handler: pathWrapper(app, '/a')
})
server.route({
method: 'GET',
path: '/b',
handler: pathWrapper(app, '/b')
})
server.route({
method: 'GET',
path: '/b',
handler: pathWrapper(app, '/b')
})
server.route({
method: 'GET',
path: '/{p*}', /* catch all route */
handler: defaultHandlerWrapper(app)
})
server.route({
method: 'GET',
path: '/{p*}', /* catch all route */
handler: defaultHandlerWrapper(app)
})
server.start().catch(error => {
console.log('Error starting server')
console.log(error)
}).then(() => {
console.log('> Ready on http://localhost:3000')
server.start().catch(error => {
console.log('Error starting server')
console.log(error)
}).then(() => {
console.log('> Ready on http://localhost:3000')
})
})
})

View file

@ -42,7 +42,7 @@ export default class Server {
throw new Error('Please provide a parsed url to `handle` as third parameter. See https://github.com/zeit/next.js#custom-server-and-routing for an example.')
}
this.run(req, res, parsedUrl)
return this.run(req, res, parsedUrl)
.catch((err) => {
if (!this.quiet) console.error(err)
res.statusCode = 500
@ -252,7 +252,7 @@ export default class Server {
}
async serveStaticWithGzip (req, res, path) {
this._serveStatic(req, res, () => {
await this._serveStatic(req, res, () => {
return serveStaticWithGzip(req, res, path)
})
}