Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsed url to be passed down #950

Merged
merged 5 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,15 @@ const handle = app.getRequestHandler()

app.prepare().then(() => {
createServer((req, res) => {
const { pathname, query } = parse(req.url, true)
const url = parse(req.url, true)
const { pathname, query } = url

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, url)
}
})
.listen(3000, (err) => {
Expand Down
5 changes: 3 additions & 2 deletions examples/custom-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const handle = app.getRequestHandler()
app.prepare()
.then(() => {
createServer((req, res) => {
const { pathname, query } = parse(req.url, true)
const url = parse(req.url, true)
const { pathname, query } = url

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, url)
}
})
.listen(3000, (err) => {
Expand Down
22 changes: 13 additions & 9 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ export default class Server {
}

getRequestHandler () {
return (req, res) => {
this.run(req, res)
return (req, res, url) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to see url as parsedUrl here and everywhere else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

if (!url || url.query === null) {
url = parse(req.url, true)
}

this.run(req, res, url)
.catch((err) => {
if (!this.quiet) console.error(err)
res.statusCode = 500
Expand Down Expand Up @@ -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, { url }) => {
const { pathname, query } = url
await this.render(req, res, pathname, query)
}
}
Expand All @@ -126,19 +130,19 @@ export default class Server {
})
}

async run (req, res) {
async run (req, res, url) {
if (this.hotReloader) {
await this.hotReloader.run(req, res)
}

const fn = this.router.match(req, res)
const fn = this.router.match(req, res, url)
if (fn) {
await fn()
return
}

if (req.method === 'GET' || req.method === 'HEAD') {
await this.render404(req, res)
await this.render404(req, res, url)
} else {
res.statusCode = 501
res.end(STATUS_CODES[501])
Expand Down Expand Up @@ -203,8 +207,8 @@ export default class Server {
}
}

async render404 (req, res) {
const { pathname, query } = parse(req.url, true)
async render404 (req, res, url = parse(req.url, true)) {
const { pathname, query } = url
res.statusCode = 404
this.renderError(null, req, res, pathname, query)
}
Expand Down
6 changes: 3 additions & 3 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export default class Router {
this.routes.set(method, routes)
}

match (req, res) {
match (req, res, url = parse(req.url)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to parse the url here.
.match() is always called with a parsedUrl.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, removed.

const routes = this.routes.get(req.method)
if (!routes) return

const { pathname } = parse(req.url)
const { pathname } = url
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, url })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like keeping parsedUrl inside the params. Make it the 4th argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as fourth argument.

}
}
}
Expand Down