I'm not sure if I'm doing the routing correctly but I'm having trouble getting the previous button in the browser to work properly.
Here's the example
In order:
previous page in the browser, the url changes to the root path, but the page still renders "two" (Here I expect it to render page one)I tried following the example from the guide. It basically made sense, but I'm not able to get the behavior I expect. I may have just messed up something.
Thanks!
I think the browser automatically pushes initial url into history without data => the initial url isn't associated with the seed::Url (aka data / state) so once you get back to initial url, history returns empty state and the handler isn't called because of this line:
https://github.com/seed-rs/seed/blob/e29041f431c361dad7636fb03a41df3985a56435/src/routing.rs#L210
As an ugly workaround, try to add this into your example:
use serde_json;
// ...
#[wasm_bindgen(start)]
pub fn render() {
seed::App::build(
|url: Url, _| {
let title = match &url.title {
Some(t) => t,
None => "",
};
let data =
JsValue::from_serde(&serde_json::to_string(&url).expect("Problem serializing route data"))
.expect("Problem converting route data to JsValue");
history().replace_state(&data, title).expect("replace history state");
Init::new(Model::new())
},
update,
view,
)
.routes(routes)
.build_and_start();
}
So I think it's a bug and we should resolve it properly once PR https://github.com/seed-rs/seed/pull/290 is merged to prevent merge conflicts.
I see, after I did some digging your explanation makes sense. I鈥檒l continue to look around a bit, I think I don鈥檛 fully understand yet why you use data for the path instead of path.
I don鈥檛 fully understand yet why you use data
@David-OConnor Can we just parse current URL to seed::Url and don't use data at all? Or is it slow or is the current URL not always synchronized with data in the associated history item?
A little bit OT:
Do we have to use clean_url?
https://github.com/seed-rs/seed/blob/e29041f431c361dad7636fb03a41df3985a56435/src/routing.rs#L151
So looking at this line:
let data = JsValue::from_serde(&serde_json::to_string(&url).expect("Problem serializing route data"))
.expect("Problem converting route data to JsValue");
I was able to replace from_serde with from_str, because it looks like you're just using the string representation of Url. Not using from_serde might save some time https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen/struct.JsValue.html#method.from_serde .
Oh, right, that was another confusing thing; I finally realized that https://github.com/seed-rs/seed/blob/e29041f431c361dad7636fb03a41df3985a56435/src/routing.rs#L210 returns None if the data is not a string. So when I tried making data a json object (while I was poking around), I was able to put it in the history's data, but would get silently bypassed at line 210.
Yeah, let's wait for @David-OConnor answers and merging of that PR and then we should refactor it.
Can we just parse current URL to seed::Url and don't use data at all?
Sure; they should be equivalent
I was able to replace from_serde with from_str
Nice - in the PR
Do we have to use clean_url?
After some experimenting, it seems like we don't. I don't remember the cases the required it, and if they're still relevant. Can add it back if we run into problems with prepended /.
returns None if the data is not a string.
I think this was the root cause: https://github.com/seed-rs/seed/pull/300/files#diff-af10b599a7b9d05771dc19b6c6482cebR203
Merged
Most helpful comment
Yeah, let's wait for @David-OConnor answers and merging of that PR and then we should refactor it.