diff --git a/README.md b/README.md index 028fd8460e830..67a858661b31b 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 46d7dda2ae7f6..87ceba6b95efc 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 f08504ef6f883..fe81244233711 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 f15b9e4d93c54..b393a114a2469 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) } } }