sycamore-rs / sycamore

A library for creating reactive web apps in Rust and WebAssembly
https://sycamore-rs.netlify.app
MIT License
2.79k stars 148 forks source link

Navigate and a href fail to pass on query parameters #562

Closed stopbystudent closed 1 year ago

stopbystudent commented 1 year ago

Describe the bug

In my application, I have a URL that takes query parameters. They are extracted by the router using JavaScript which works well. Now if I call my app with /?filter=text I expect it to receive the query parameter. That indeed works if called directly from the browser's URL bar (typing it manually). Coming from sycamore, however, this has two interesting variations:

Losing the query parameters while navigating or having to manually reload is a bug, in my opinion.

To Reproduce

pub fn get_query_parameter(key: &str) -> Option<String> {
    web_sys::UrlSearchParams::new_with_str(&web_sys::window()?.location().search().ok()?).ok()?.get(key)
}

pub enum Action {
    View(ViewParameters),
}

#[derive(Clone, Debug, Default)]
pub struct ViewParameters {
    pub filter: Option<String>,
}

impl TryFromSegments for Action {
    fn try_from_segments(param: &[&str]) -> Option<Self> {
        match param {
            [] => Some(Action::View(ViewParameters {
                filter: get_query_parameter("filter"),
            })),
            _ => None,
        }
    }
}

Expected behavior

I expect sycamore-router to be query-parameter-agnostic, i.e. even while #130 is unresolved I expect sycamore-router to pass on query parameters as is, neither ignoring them (the a(href case) nor leading to 404 pages (the navigate case).

Furthermore, navigate should not behave differently from what the href interceptor does on a tags in this regard.

Environment

lukechu10 commented 1 year ago

Yup definitely a bug. I think it's because we try to parse the URL but don't succeed because the parser has no knowledge of query params yet. A simple fix should just be to ignore everything after a ? character in the URL parser.