Skip to content

Commit 40738c6

Browse files
timneutkensrauchg
authored andcommitted
Throw Error when url.parse without true is parsed (vercel#1282)
* Throw Error when url.parse without true is parsed This is a bit more descriptive when this mistake is made by the user. * Parse when needed * Parse querystring if it is not provided
1 parent 7c8cd4b commit 40738c6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

server/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve, join } from 'path'
2-
import { parse } from 'url'
2+
import { parse as parseUrl } from 'url'
3+
import { parse as parseQs } from 'querystring'
34
import fs from 'mz/fs'
45
import http, { STATUS_CODES } from 'http'
56
import {
@@ -33,12 +34,14 @@ export default class Server {
3334

3435
getRequestHandler () {
3536
return (req, res, parsedUrl) => {
37+
// Parse url if parsedUrl not provided
3638
if (!parsedUrl) {
37-
parsedUrl = parse(req.url, true)
39+
parsedUrl = parseUrl(req.url, true)
3840
}
3941

40-
if (!parsedUrl.query) {
41-
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.')
42+
// Parse the querystring ourselves if the user doesn't handle querystring parsing
43+
if (typeof parsedUrl.query === 'string') {
44+
parsedUrl.query = parseQs(parsedUrl.query)
4245
}
4346

4447
return this.run(req, res, parsedUrl)
@@ -222,7 +225,7 @@ export default class Server {
222225
}
223226
}
224227

225-
async render404 (req, res, parsedUrl = parse(req.url, true)) {
228+
async render404 (req, res, parsedUrl = parseUrl(req.url, true)) {
226229
const { pathname, query } = parsedUrl
227230
res.statusCode = 404
228231
return this.renderError(null, req, res, pathname, query)

0 commit comments

Comments
 (0)