urlon icon indicating copy to clipboard operation
urlon copied to clipboard

urlon.stringify() not encoding '/' (slash)

Open apal21 opened this issue 3 years ago • 6 comments

While using this library to update url in my browser I faced an issue while converting it back to JSON.

For example:

urlon.stringify({ foo: 'ba/r' }); // OUTPUT: $foo=ba//r

But in URLs / is a valid character.

Temporarily, I'm using this to solve the problem.

encodeURIComponent(urlon.stringify({ foo: 'ba/r' })); // OUTPUT: %24foo%3Dba%2F%2Fr

It would be great if this issue can be fixed!

apal21 avatar Mar 02 '22 21:03 apal21

This library has 2 major goals:

  • for any JSON value J, JSON.parse(JSON.stringify(J)) is deep equal to urlon.parse(urlon.stringify(J))
  • urlon.stringify - to use only characters that could be shown in url without forced encoding.

As you have stated / is a valid character to be used in url.

I could try to guess an actual issue you having is that / is also a common character that is used as path delimeter. Most times I've used urlon it was for query part, that goes after ? character. And there is no rule that disallows use of / there. I only used urlon with primitives (number, boolean, string) when applied it to path segment part. Though it seems still can be an issue for urlon.stringify('ba/r').

@apal21 Could you please clarify your use case in more details. I had some improvements in mind for a next breaking change release, so potentially could consider your case if find it valid.

Guria avatar Mar 03 '22 07:03 Guria

Thanks for the quick response...

So in my web app (React.js), I'm generating a table using the params passed from the URL. And I'm facing the exact same issue that you just mentioned. While fetching those params, it only fetches till /. It is considering the other part as some different path.

apal21 avatar Mar 03 '22 10:03 apal21

@apal21 do you work with url part on Frontend only side? Do you use any router library? Could you share exact snippet how do you integrate urlon in your app?

Guria avatar Mar 03 '22 10:03 Guria

There is nothing special about the code.

I am using it like this for now

<Route exact path="/:fields/:conditions" component={HomePage} />

But as you mentioned I'll probably convert it to a query. Like ?fields=something&conditions=something

You can close this issue. And thanks again for the quick responses.

apal21 avatar Mar 03 '22 14:03 apal21

with ?fields=something&conditions=something you still may have issues. I'd suggest to use urlon for whole query parsing parsing. so use result of urlon.stringify({ fields: [''], conditions: {}, other_query_param: 'something' })

Guria avatar Mar 03 '22 18:03 Guria

Ohh okay got it!

apal21 avatar Mar 03 '22 19:03 apal21