Skip to content

Commit 59281ad

Browse files
timneutkensarunoda
authored andcommitted
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
1 parent 24edfbd commit 59281ad

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,15 @@ const handle = app.getRequestHandler()
382382

383383
app.prepare().then(() => {
384384
createServer((req, res) => {
385-
const { pathname, query } = parse(req.url, true)
385+
const parsedUrl = parse(req.url, true)
386+
const { pathname, query } = parsedUrl
386387

387388
if (pathname === '/a') {
388389
app.render(req, res, '/b', query)
389390
} else if (pathname === '/b') {
390391
app.render(req, res, '/a', query)
391392
} else {
392-
handle(req, res)
393+
handle(req, res, parsedUrl)
393394
}
394395
})
395396
.listen(3000, (err) => {

examples/custom-server/server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ const handle = app.getRequestHandler()
99
app.prepare()
1010
.then(() => {
1111
createServer((req, res) => {
12-
const { pathname, query } = parse(req.url, true)
12+
const parsedUrl = parse(req.url, true)
13+
const { pathname, query } = parsedUrl
1314

1415
if (pathname === '/a') {
1516
app.render(req, res, '/b', query)
1617
} else if (pathname === '/b') {
1718
app.render(req, res, '/a', query)
1819
} else {
19-
handle(req, res)
20+
handle(req, res, parsedUrl)
2021
}
2122
})
2223
.listen(3000, (err) => {

server/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ export default class Server {
3333
}
3434

3535
getRequestHandler () {
36-
return (req, res) => {
37-
this.run(req, res)
36+
return (req, res, parsedUrl) => {
37+
if (!parsedUrl || parsedUrl.query === null) {
38+
parsedUrl = parse(req.url, true)
39+
}
40+
41+
this.run(req, res, parsedUrl)
3842
.catch((err) => {
3943
if (!this.quiet) console.error(err)
4044
res.statusCode = 500
@@ -102,8 +106,8 @@ export default class Server {
102106
await this.serveStatic(req, res, p)
103107
},
104108

105-
'/:path*': async (req, res) => {
106-
const { pathname, query } = parse(req.url, true)
109+
'/:path*': async (req, res, params, parsedUrl) => {
110+
const { pathname, query } = parsedUrl
107111
await this.render(req, res, pathname, query)
108112
}
109113
}
@@ -126,19 +130,19 @@ export default class Server {
126130
})
127131
}
128132

129-
async run (req, res) {
133+
async run (req, res, parsedUrl) {
130134
if (this.hotReloader) {
131135
await this.hotReloader.run(req, res)
132136
}
133137

134-
const fn = this.router.match(req, res)
138+
const fn = this.router.match(req, res, parsedUrl)
135139
if (fn) {
136140
await fn()
137141
return
138142
}
139143

140144
if (req.method === 'GET' || req.method === 'HEAD') {
141-
await this.render404(req, res)
145+
await this.render404(req, res, parsedUrl)
142146
} else {
143147
res.statusCode = 501
144148
res.end(STATUS_CODES[501])
@@ -203,8 +207,8 @@ export default class Server {
203207
}
204208
}
205209

206-
async render404 (req, res) {
207-
const { pathname, query } = parse(req.url, true)
210+
async render404 (req, res, parsedUrl = parse(req.url, true)) {
211+
const { pathname, query } = parsedUrl
208212
res.statusCode = 404
209213
this.renderError(null, req, res, pathname, query)
210214
}

server/router.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { parse } from 'url'
21
import pathMatch from 'path-match'
32

43
const route = pathMatch()
@@ -14,16 +13,16 @@ export default class Router {
1413
this.routes.set(method, routes)
1514
}
1615

17-
match (req, res) {
16+
match (req, res, parsedUrl) {
1817
const routes = this.routes.get(req.method)
1918
if (!routes) return
2019

21-
const { pathname } = parse(req.url)
20+
const { pathname } = parsedUrl
2221
for (const r of routes) {
2322
const params = r.match(pathname)
2423
if (params) {
2524
return async () => {
26-
return r.fn(req, res, params)
25+
return r.fn(req, res, params, parsedUrl)
2726
}
2827
}
2928
}

0 commit comments

Comments
 (0)