solid-router icon indicating copy to clipboard operation
solid-router copied to clipboard

query param auto remove special chracter

Open dnguyenfs opened this issue 3 years ago • 1 comments

Describe the bug

domain:verification?token=U2FsdGVkX1+Pn+CqDMQBEdFWaMVnn/sLZBPTE2V2weZxTGYMa+njTIHBBElcpgZhOyhCKjKGM16qW71gzDTu2w==&[email protected]

const [searchParams, setSearchParams] = useSearchParams();

the searchParams.token itself will auto remove + and other special characters.

dnguyenfs avatar Oct 01 '22 09:10 dnguyenfs

I just encountered something that I suspect might be similar.

Here's a minimal example:

https://codesandbox.io/s/sharp-nash-bu49bj?file=/src/main.tsx

In short, I have an <A> like this:

<A href={`/login?returnTo=${encodeURIComponent(returnTo)}`}>Log in</A>

If returnTo is set to /, I'd expect to be sent to /login?returnTo=%2F, and that's the value I can see in the bottom left corner of the browser when I hover over the link.

However, When I click that <A>, I actually get send to login?returnTo= (last character missing). I get the feeling the two issues might be related?

RAnders00 avatar Oct 09 '22 11:10 RAnders00

the searchParams.token itself will auto remove + and other special characters.

setSearchParams doesn't look like it attempts to encode the value. If your value could have characters that are illegal in the url you'd need to do something like:

setSearchParams( {token: encodeURIComponent(token)} )

Brendan-csel avatar Oct 21 '22 06:10 Brendan-csel

@RAnders00 That does look like a bug (so different issue).

I can see there is an internal utility (normalize) called to strip leading and trailing "/" characters ...but I don't think it was considered having a trailing slash on the end of a query string (eg /login?returnTo=/).

I'm not sure but the function below may need to split the query string off the path, then normalize the path, and finally append the query string again.

https://github.com/solidjs/solid-router/blob/528a2dcaaf1fe69737ae753d6a75f66dea6d2c7f/src/utils.ts#L12-L27

Brendan-csel avatar Oct 21 '22 07:10 Brendan-csel

@dnguyenlearning Sorry - I see what you mean.

I can see the router decodes the query string - then writes the decoded version back into the address bar (with invalid characters).

@RAnders00, yes that does seem sort of related. Like, if the router left the query string encoded then the last character wouldn't have been a slash and therefore wouldn't have been trimmed.

Brendan-csel avatar Oct 22 '22 07:10 Brendan-csel