elm / url

Build and parse URLs. Useful for HTTP and "routing" in single-page apps (SPAs)
https://package.elm-lang.org/packages/elm/url/latest/
BSD 3-Clause "New" or "Revised" License
75 stars 43 forks source link

add alternative methods to handle a basepath like "some/path/" #14

Open sporto opened 6 years ago

sporto commented 6 years ago

Consider an application that is not in the root of a site e.g.

example.com/some/path/

In this case we would pass the basepath to the app via flags. e.g

flags =
    {
    basePath = "some/path/"
    }

I attempted to parse this like:

matchers : Parser (Route -> a) a
matchers =
    Parser.s flags.basePath  </> Parser.oneOf
        [ Parser.map Home Parser.top
        , Parser.map About (Parser.s "about")
        ]

But Parser.s won't parse the /.

Example here https://ellie-app.com/3grXTSD4Bkxa1

It would be great to have something to parse a basepath e.g.

matchers : Parser (Route -> a) a
matchers =
    Parser.base flags.basePath  </> Parser.oneOf
        [ Parser.map Home Parser.top
        , Parser.map About (Parser.s "about")
        ]

Thanks

sporto commented 6 years ago

One workaround is to remove the basepath manually before parsing e.g.

urlWithoutBasePath =
   { url |
      path = String.replace flags.basePath "" url.path
   }
malaire commented 5 years ago

That workaround completely ignores the problem of percent-encoding.

ashishsc commented 5 years ago

I'm experiencing this issue as well on a github-pages site

Erudition commented 5 years ago

That workaround completely ignores the problem of percent-encoding.

...Then percent-encode the base path first.