From 59281adef30a8f55e258127314bff94a50b17c4d Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 2 Feb 2017 07:51:08 +0100 Subject: [PATCH] Allow parsed url to be passed down (#950) * Allow parsed url to be passed down * Update example to reflect url passing * Check if passed url.query is empty * Rename url to parsedUrl --- README.md | 5 +++-- examples/custom-server/server.js | 5 +++-- server/index.js | 22 +++++++++++++--------- server/router.js | 7 +++---- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 028fd846..67a85866 100644 --- a/README.md +++ b/README.md @@ -382,14 +382,15 @@ const handle = app.getRequestHandler() app.prepare().then(() => { createServer((req, res) => { - const { pathname, query } = parse(req.url, true) + const parsedUrl = parse(req.url, true) + const { pathname, query } = parsedUrl if (pathname === '/a') { app.render(req, res, '/b', query) } else if (pathname === '/b') { app.render(req, res, '/a', query) } else { - handle(req, res) + handle(req, res, parsedUrl) } }) .listen(3000, (err) => { diff --git a/examples/custom-server/server.js b/examples/custom-server/server.js index 46d7dda2..87ceba6b 100644 --- a/examples/custom-server/server.js +++ b/examples/custom-server/server.js @@ -9,14 +9,15 @@ const handle = app.getRequestHandler() app.prepare() .then(() => { createServer((req, res) => { - const { pathname, query } = parse(req.url, true) + const parsedUrl = parse(req.url, true) + const { pathname, query } = parsedUrl if (pathname === '/a') { app.render(req, res, '/b', query) } else if (pathname === '/b') { app.render(req, res, '/a', query) } else { - handle(req, res) + handle(req, res, parsedUrl) } }) .listen(3000, (err) => { diff --git a/server/index.js b/server/index.js index f08504ef..fe812442 100644 --- a/server/index.js +++ b/server/index.js @@ -33,8 +33,12 @@ export default class Server { } getRequestHandler () { - return (req, res) => { - this.run(req, res) + return (req, res, parsedUrl) => { + if (!parsedUrl || parsedUrl.query === null) { + parsedUrl = parse(req.url, true) + } + + this.run(req, res, parsedUrl) .catch((err) => { if (!this.quiet) console.error(err) res.statusCode = 500 @@ -102,8 +106,8 @@ export default class Server { await this.serveStatic(req, res, p) }, - '/:path*': async (req, res) => { - const { pathname, query } = parse(req.url, true) + '/:path*': async (req, res, params, parsedUrl) => { + const { pathname, query } = parsedUrl await this.render(req, res, pathname, query) } } @@ -126,19 +130,19 @@ export default class Server { }) } - async run (req, res) { + async run (req, res, parsedUrl) { if (this.hotReloader) { await this.hotReloader.run(req, res) } - const fn = this.router.match(req, res) + const fn = this.router.match(req, res, parsedUrl) if (fn) { await fn() return } if (req.method === 'GET' || req.method === 'HEAD') { - await this.render404(req, res) + await this.render404(req, res, parsedUrl) } else { res.statusCode = 501 res.end(STATUS_CODES[501]) @@ -203,8 +207,8 @@ export default class Server { } } - async render404 (req, res) { - const { pathname, query } = parse(req.url, true) + async render404 (req, res, parsedUrl = parse(req.url, true)) { + const { pathname, query } = parsedUrl res.statusCode = 404 this.renderError(null, req, res, pathname, query) } diff --git a/server/router.js b/server/router.js index f15b9e4d..b393a114 100644 --- a/server/router.js +++ b/server/router.js @@ -1,4 +1,3 @@ -import { parse } from 'url' import pathMatch from 'path-match' const route = pathMatch() @@ -14,16 +13,16 @@ export default class Router { this.routes.set(method, routes) } - match (req, res) { + match (req, res, parsedUrl) { const routes = this.routes.get(req.method) if (!routes) return - const { pathname } = parse(req.url) + const { pathname } = parsedUrl for (const r of routes) { const params = r.match(pathname) if (params) { return async () => { - return r.fn(req, res, params) + return r.fn(req, res, params, parsedUrl) } } }