Skip to content

Routes don't behave like expected #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flosse opened this issue Oct 16, 2019 · 17 comments
Closed

Routes don't behave like expected #247

flosse opened this issue Oct 16, 2019 · 17 comments

Comments

@flosse
Copy link
Member

flosse commented Oct 16, 2019

Using seed v0.4.1 I tried to use URL changes but it does not work:

Bildschirmfoto_2019-10-16_21-07-47

Here is the code: flosse@5dee829#diff-1015f606e4bbeb299d9ce5d5d8303123

Is this a bug or a feature?

@David-OConnor
Copy link
Member

David-OConnor commented Oct 16, 2019

Could you please post your routes function? This may be due to mixing hash and path style routing.

@flosse
Copy link
Member Author

flosse commented Oct 16, 2019

Could you please post your routes function? This may be due to mixing hash and path style routing.

fn routes(url: Url) -> Msg {
    Msg::Route(url)
}

#[derive(Debug, Clone)]
enum Msg {
    Route(Url),
}

fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
    match msg {
        Msg::Route(url) => {
            model.url = Some(url);
        }
    }
    model.count += 1;
}

@flosse
Copy link
Member Author

flosse commented Oct 16, 2019

I think the problem is that I'm looking for a hashchange event that is not tracked by seed.

@MartinKavik
Copy link
Member

@flosse A little bit OT - I haven't enountered this problem because I don't use #. So I'm curious - what's your use-case?

@David-OConnor
Copy link
Member

Could you also post the code that triggers the routing? eg the update func section or listened-link.

@flosse
Copy link
Member Author

flosse commented Oct 17, 2019

So I'm curious - what's your use-case?

We consider to port the whole kartevonmorgen frontend to seed.
Within the first step we want to avoid breaking changes.
So e.g. this link should show the entry with the ID 4c209..:
https://kartevonmorgen.org/#/?entry=4c20979fe0754e74875afa4308d73ce7&zoom=15.00

@flosse
Copy link
Member Author

flosse commented Oct 17, 2019

Could you also post the code that triggers the routing? eg the update func section or listened-link.

As already mentioned, here is the complete example: flosse@5dee829#diff-1015f606e4bbeb299d9ce5d5d8303123

but there is no code that triggers the routing, I just change the hash within the address bar.

@David-OConnor
Copy link
Member

David-OConnor commented Oct 17, 2019

The issue here is that the routing system requires something to trigger the routing, like seed::push_route(), or a relative hyperlink; your example isn't causing the routes function to be called at all.

I'm trying to think if there's a way to make this work. Perhaps there's a way to listen for url changes like we listen for popstate events. I suspect onhashchange would work.

@David-OConnor
Copy link
Member

David-OConnor commented Oct 17, 2019

#252

@flosse
Copy link
Member Author

flosse commented Oct 17, 2019

#252

thanks!
The initial state is working now :)

Changing the hash leads to the following URL:

Url { path: ["", "localhost:8000", "#?foo=basry"], search: None, hash: None, title: None }

And the route function gets called twice.

code: flosse@8a6749a

@David-OConnor
Copy link
Member

David-OConnor commented Oct 18, 2019

How about now? I'm not sure how/if to handle routing being called twice.

@flosse
Copy link
Member Author

flosse commented Oct 18, 2019

How about now?

now it's working :)

I'm not sure how/if to handle routing being called twice.

the popstate gets fired first with an empty url

@flosse
Copy link
Member Author

flosse commented Oct 18, 2019

The popstate handler creates an empty url if ev.state().as_string() is None

https://github.com/David-OConnor/seed/blob/b9d0afaffbaa5f1f9299b2bc36641f091519aae4/src/routing.rs#L275-L284

@David-OConnor
Copy link
Member

Latest push should do it. Ready to merge if this is solved on your end. Popstate will no longer call the routes function if it receives a null JS value, as it does here. (Previous behavior was to call with an empty Url, as you found out.)

@AlterionX
Copy link
Contributor

@flosse Was this fixed? Just pinging for a confirmation.

@MartinKavik
Copy link
Member

@AlterionX it should be fixed - #252 (comment)

@flosse
Copy link
Member Author

flosse commented Nov 5, 2019

@flosse Was this fixed?

yes, as far as I can remember :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants