Skip to content

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

@SergKam

Description

@SergKam

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 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions