rgrempel / elm-route-url

Router for single-page-apps in Elm
http://package.elm-lang.org/packages/rgrempel/elm-route-url/latest
MIT License
196 stars 16 forks source link

How to handle sub-directory base URLs #15

Closed amitaibu closed 8 years ago

amitaibu commented 8 years ago

Assuming:

delta2url : Model -> Model -> Maybe UrlChange
delta2url previous current =
    case current.activePage of
        Login ->
            Just <| UrlChange NewEntry "/#login"

And my app's path is https://example.com/subdirectory then the above code will change the URL to https://example.com/#login.

However the desired/typical outcome should be https://example.com/subdirectory/#login

rgrempel commented 8 years ago

I suppose you've basically got two options, either:

Just <| UrlChange NewEntry "#login"

or

Just <| UrlChange NewEntry "/subdirectory/#login"

I find the former to be quite convenient if you've decided you are just using the hash, as you don't have to know what the subdirectory actually is.

amitaibu commented 8 years ago

Yeah, I tried removing the leading / , but this results with an error:

Uncaught SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'http:/#login' cannot be created in a document with origin 'http://localhost:3000' and URL 'http://localhost:3000/'

It seems to remove the base url.

rgrempel commented 8 years ago

Ah, that is interesting.

Now, it does work in some cases, but what it is relying on (when it works) is that the browser will interpret a pushState which starts with a "#" in a certain way ... that is, just changing the hash and leaving everything else alone.

So, one clue to the problem is that the browser you're using is interpreting the input to pushState as having been "http:/#login". That is, when given "#login", your browser is normalizing that to "http:/#login" (rather than to "http://localhost:3000/#login", which presumably is what would work).

In any event, I suppose that I could fix this by pre-normalizing what I supply to pushState. That is, if the new URL starts with a "#", I do know the current URL, so I can do the normalization I want manually. Presumably that's what I'll have to do, if some browsers don't behave as I expected.

BTW, which browser/OS combination are you seeing this with? I should try to reproduce this before I fix it :-)

amitaibu commented 8 years ago

which browser/OS combination are you seeing this with?

Mac (10.10) & Chrome 50

amitaibu commented 8 years ago

btw, for now I just cheated and add a CNAME so there's no more a subdirectory ;) http://elm-spa-example.gizra.com/

rgrempel commented 8 years ago

I'm actually having trouble reproducing the problem, using Chrome 51 and Mac OS X 10.11.

And, it turns out that it's a bit of a pain to re-install Chrome 50.

I'll mull this one over ... it really ought to work ... I suppose it's just barely possible that it was a bug fixed in Chrome 51.

If you can set up the original, offending code again, do you see any different results in Safari?

amitaibu commented 8 years ago

I've updated to Chrome 51 and it doesn't work there. Nor in Safari.

You can see this in http://elm-spa-example.gizra.com/ if you change the Router

rgrempel commented 8 years ago

I forked the elm-spa-example, and made the following changes to try to reproduce the problem:

https://github.com/rgrempel/elm-spa-example/commit/a4e8eb577f7cdd3e3b3ae95e72d10d5cadd14b4f

With these changes, I need to go http://localhost:3000/serve/ in order to see the app -- that is, when I run gulp, I've now got the app mounted in a subdirectory.

This seems to work for me, both in Safari and Chrome.

So, I'm thinking that I must not be accurately reproducing what failed for you?

amitaibu commented 8 years ago

You should see the app under http://localhost:3000 (the gulpfile change shouldn't be needed)

elm_and_router_elm_ __library_webserver_documents_elm-learn_elm-spa-example

rgrempel commented 8 years ago

Ah, I am seeing the problem now -- not sure why I wasn't before.

I shall investigate!

rgrempel commented 8 years ago

This should be fixed in version 2.0.1 -- thanks for reporting it -- and my apologies for not reproducing it sooner!

amitaibu commented 8 years ago

Thank you! 👍