url-parser icon indicating copy to clipboard operation
url-parser copied to clipboard

Ignore the real query parameters when using hash, expect #whatever?key=value instead

Open SergKam opened this issue 8 years ago • 10 comments

parseHash parses query params from the main URL not from the hash part For example this common use-case http://example.com/app/#items?filter=cats It cannot be implemented using this module.

In my case parseLocation always returns Nothing if the hash query used.

urlToRoute : Parser (Route -> a) a
urlToRoute =
    oneOf
        [ map MainRoute top
        , map ItemListRoute (s "items" <?> stringParam "filter")
        , map ItemRoute (s "items" </> string)
        , map AboutRoute (s "about")
        ]

parseLocation : Location -> Route
parseLocation location =
    parseHash urlToRoute  location

I expect the function with the name "parseHash" parses hash only, but instead, it parses the "location.search" as I can see here https://github.com/evancz/url-parser/blob/master/src/UrlParser.elm#L353

The workaround I use is this (not very elegant, sorry)

fixLocationQuery : Location -> Location
fixLocationQuery location =
    let
        hash =
            String.split "?" location.hash
                |> List.head
                |> Maybe.withDefault ""

        search =
            String.split "?" location.hash
                |> List.drop 1
                |> String.join "?"
                |> String.append "?"
    in
        { location | hash = hash, search = search }

SergKam avatar Dec 19 '16 11:12 SergKam

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot avatar Dec 19 '16 11:12 process-bot

Thanks @SergKam, ran into the same thing.

Could we possibly update the documentation?

amilner42 avatar Feb 05 '17 22:02 amilner42

I just ran into the same problem. Thanks @SergKam for quick workaround!

j-panasiuk avatar Feb 07 '17 20:02 j-panasiuk

You're a life saver, thanks @SergKam !

mauropalsgraaf avatar Jul 12 '17 12:07 mauropalsgraaf

I ran into the same problem. Rewriting the code using path params because of this issue.

andys8 avatar Nov 10 '17 11:11 andys8

Just spent a couple hours trying to figure out why query parameter parsing always returned Nothing before finding this. I use similar URLs: https://domain/app/#/courses?q=asdf

kspeakman avatar Jan 30 '18 00:01 kspeakman

I've lost my whole day tinkering this issue & never thought it might be that the browser processes hash & search in this manner.

This is my function for normalizing the Navigation.Location, it will give you the correct and same location with both cases #hello?q=world & ?q=world#hello:

    fixLocation : Navigation.Location -> Navigation.Location
    fixLocation location =
        case String.split "?" location.hash of
            [ hash, search ] ->
                { location
                    | search = ("?" ++ search)
                    , hash = hash
                } 
        
            _ ->
                location

This actually in my opinion messes a lot with people diving into making SPA/PWA's in Elm. Since libraries such as elm-lang/navigation actually favor the approach of hashed routing, this behaviour should be either overriden or documented, here or in elm-lang/navigation, since developers consistently face this issue here & in elm-lang/navigation#33 .

kexoth avatar Mar 16 '18 00:03 kexoth

@SergKam thanks a lot, this was making me crazy

3v0k4 avatar Jul 06 '18 16:07 3v0k4

Also took several hours for me to figure out this was the problem, glad this issue was here...

cmditch avatar Sep 01 '18 06:09 cmditch

I think this repository is replaced with https://github.com/elm/url. The issues have to be tested, and new issues have to be created if they're still existing.

andys8 avatar Sep 01 '18 19:09 andys8