fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Optional param returning undefined as a string instead of as a type of undefined #89

Closed bliitzkrieg closed 6 years ago

bliitzkrieg commented 7 years ago

I have the following optional route routes.add('page', '/:lang?/page/:slug') and when I go to /page/some-slug, the query that I get from getInitialProps is { lang: "undefined", slug: "some-slug" }. The lang is returning a string type instead of undefined typed which I think is a bug. In path-to-regexp, the optional param returns an actual undefined typed (tested myself).

If I go to /en/page/some-slug, I get both params correctly.

Is this a known bug?

fridays commented 7 years ago

Thanks for pointing that out! Sounds like a bug indeed. I couldn't yet take the time to look into it, PR welcome!

bliitzkrieg commented 7 years ago

Hey - I looked into the issue further and it looks like when the href is created it puts undefined when an optional param doesn't exist so /page?lang=undefined&slug=some-slug.

Looks like the issue is here:

https://github.com/fridays/next-routes/blob/master/src/index.js#L145 specifically the toQuerystring function. Solution may be to use a .filter before the .map to filter out undefined.

fridays commented 6 years ago

Fixed in version 1.1.0!