evancz / url-parser

Parse URLs into nicely structured data
http://package.elm-lang.org/packages/evancz/url-parser/latest/
BSD 3-Clause "New" or "Revised" License
114 stars 29 forks source link

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

Open SergKam opened 7 years ago

SergKam commented 7 years ago

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 }
process-bot commented 7 years ago

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.

amilner42 commented 7 years ago

Thanks @SergKam, ran into the same thing.

Could we possibly update the documentation?

j-panasiuk commented 7 years ago

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

mauropalsgraaf commented 7 years ago

You're a life saver, thanks @SergKam !

andys8 commented 6 years ago

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

kspeakman commented 6 years ago

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

kexoth commented 6 years ago

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 .

3v0k4 commented 6 years ago

@SergKam thanks a lot, this was making me crazy

cmditch commented 6 years ago

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

andys8 commented 6 years ago

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.