From 060b159cc0f426983df503dcd4745ca2fc70802e Mon Sep 17 00:00:00 2001 From: tim neutkens Date: Sat, 25 Feb 2017 20:00:13 +0100 Subject: [PATCH 1/3] Throw Error when url.parse without true is parsed This is a bit more descriptive when this mistake is made by the user. --- server/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/index.js b/server/index.js index a11c5046091a..c7c8fa47c894 100644 --- a/server/index.js +++ b/server/index.js @@ -37,6 +37,10 @@ export default class Server { parsedUrl = parse(req.url, true) } + if (parsedUrl.href && !parsedUrl.query) { + throw new Error('Please add `true` as second argument to `url.parse`. Example: url.parse(req, true). See https://github.com/zeit/next.js#custom-server-and-routing for an example.') + } + if (!parsedUrl.query) { 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.') } From 80bdfa27dfc2235d3ac8e808676d8025fe460dd9 Mon Sep 17 00:00:00 2001 From: tim neutkens Date: Sun, 26 Feb 2017 15:01:58 +0100 Subject: [PATCH 2/3] Parse when needed --- server/index.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/server/index.js b/server/index.js index c7c8fa47c894..f3866d8773dc 100644 --- a/server/index.js +++ b/server/index.js @@ -33,18 +33,10 @@ export default class Server { getRequestHandler () { return (req, res, parsedUrl) => { - if (!parsedUrl) { + if (!parsedUrl || parsedUrl && !parsedUrl.query) { parsedUrl = parse(req.url, true) } - if (parsedUrl.href && !parsedUrl.query) { - throw new Error('Please add `true` as second argument to `url.parse`. Example: url.parse(req, true). See https://github.com/zeit/next.js#custom-server-and-routing for an example.') - } - - if (!parsedUrl.query) { - 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.') - } - return this.run(req, res, parsedUrl) .catch((err) => { if (!this.quiet) console.error(err) From 0202a407c3df3ff26354b58040786dc13ba60f21 Mon Sep 17 00:00:00 2001 From: tim neutkens Date: Tue, 28 Feb 2017 20:12:54 +0100 Subject: [PATCH 3/3] Parse querystring if it is not provided --- server/index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/server/index.js b/server/index.js index 3332464c3cf4..27978aa6e6d0 100644 --- a/server/index.js +++ b/server/index.js @@ -1,5 +1,6 @@ import { resolve, join } from 'path' -import { parse } from 'url' +import { parse as parseUrl } from 'url' +import { parse as parseQs } from 'querystring' import fs from 'mz/fs' import http, { STATUS_CODES } from 'http' import { @@ -33,8 +34,14 @@ export default class Server { getRequestHandler () { return (req, res, parsedUrl) => { - if (!parsedUrl || parsedUrl && !parsedUrl.query) { - parsedUrl = parse(req.url, true) + // Parse url if parsedUrl not provided + if (!parsedUrl) { + parsedUrl = parseUrl(req.url, true) + } + + // Parse the querystring ourselves if the user doesn't handle querystring parsing + if (typeof parsedUrl.query === 'string') { + parsedUrl.query = parseQs(parsedUrl.query) } return this.run(req, res, parsedUrl) @@ -218,7 +225,7 @@ export default class Server { } } - async render404 (req, res, parsedUrl = parse(req.url, true)) { + async render404 (req, res, parsedUrl = parseUrl(req.url, true)) { const { pathname, query } = parsedUrl res.statusCode = 404 return this.renderError(null, req, res, pathname, query)