From dbc2ceefde844856d40370aa38c90fdb96daab23 Mon Sep 17 00:00:00 2001 From: sfhardman Date: Mon, 13 Feb 2017 15:12:32 +1300 Subject: [PATCH] Return a promise from getRequestHandler execution, await _serveStatic in serveStaticWithGzip, update custom-server-hapi to complete request lifecycle (#1099) --- examples/custom-server-hapi/next-wrapper.js | 11 ++-- examples/custom-server-hapi/package.json | 4 +- examples/custom-server-hapi/server.js | 59 +++++++++++++-------- server/index.js | 4 +- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/examples/custom-server-hapi/next-wrapper.js b/examples/custom-server-hapi/next-wrapper.js index 682d03fd..d0238520 100644 --- a/examples/custom-server-hapi/next-wrapper.js +++ b/examples/custom-server-hapi/next-wrapper.js @@ -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 } diff --git a/examples/custom-server-hapi/package.json b/examples/custom-server-hapi/package.json index a3814be7..60b78bc8 100644 --- a/examples/custom-server-hapi/package.json +++ b/examples/custom-server-hapi/package.json @@ -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" } } diff --git a/examples/custom-server-hapi/server.js b/examples/custom-server-hapi/server.js index 03fe36db..b8f10389 100644 --- a/examples/custom-server-hapi/server.js +++ b/examples/custom-server-hapi/server.js @@ -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') + }) }) }) diff --git a/server/index.js b/server/index.js index e14aefaa..9c2b34b8 100644 --- a/server/index.js +++ b/server/index.js @@ -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) }) }