Fix query params behaviour difference between SSR and Hydrate

This commit is contained in:
Clemente 2023-01-20 09:13:18 -03:00
parent b54a60213b
commit e4c9109278
3 changed files with 21 additions and 50 deletions

View file

@ -56,7 +56,7 @@ features = [
[features]
default = []
csr = ["leptos/csr"]
hydrate = ["leptos/hydrate"]
hydrate = ["leptos/hydrate", "dep:url"]
ssr = ["leptos/ssr", "dep:url", "dep:regex"]
stable = ["leptos/stable"]

View file

@ -19,7 +19,7 @@ pub fn create_location(cx: Scope, path: ReadSignal<String>, state: ReadSignal<St
let pathname = create_memo(cx, move |_| url.with(|url| url.pathname.clone()));
let search = create_memo(cx, move |_| url.with(|url| url.search.clone()));
let hash = create_memo(cx, move |_| url.with(|url| url.hash.clone()));
let query = create_memo(cx, move |_| url.with(|url| url.search_params()));
let query = create_memo(cx, move |_| url.with(|url| url.search_params.clone()));
Location {
pathname,

View file

@ -1,41 +1,15 @@
use crate::ParamsMap;
use std::borrow::Cow;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Url {
pub origin: String,
pub pathname: String,
pub search: String,
pub search_params: ParamsMap,
pub hash: String,
}
impl Url {
pub fn search_params(&self) -> ParamsMap {
let map = self
.search
.trim_start_matches('?')
.split('&')
.filter_map(|piece| {
let mut parts = piece.split('=');
let (k, v) = (parts.next(), parts.next());
match k {
Some(k) if !k.is_empty() => {
Some((unescape(k), unescape(v.unwrap_or_default())))
}
_ => None,
}
})
.collect::<linear_map::LinearMap<String, String>>();
ParamsMap(map)
}
}
#[cfg(feature = "ssr")]
pub fn unescape(s: &str) -> String {
urlencoding::decode(s)
.unwrap_or_else(|_| std::borrow::Cow::from(s))
.replace('+', " ")
}
#[cfg(not(feature = "ssr"))]
pub fn unescape(s: &str) -> String {
js_sys::decode_uri(s).unwrap().into()
@ -51,34 +25,31 @@ pub fn escape(s: &str) -> String {
js_sys::encode_uri(s).as_string().unwrap()
}
#[cfg(not(feature = "ssr"))]
impl TryFrom<&str> for Url {
type Error = String;
fn try_from(url: &str) -> Result<Self, Self::Error> {
let fake_host = String::from("http://leptos");
let url = web_sys::Url::new_with_base(url, &fake_host)
.map_err(|e| e.as_string().unwrap_or_default())?;
let url = url::Url::parse(&normalize_wasm_url(url)).map_err(|e| e.to_string())?;
Ok(Self {
origin: url.origin(),
pathname: url.pathname(),
search: url.search(),
hash: url.hash(),
origin: url.origin().unicode_serialization(),
pathname: url.path().to_string(),
search: url.query().unwrap_or_default().to_string(),
search_params: ParamsMap(
url.query_pairs()
.map(|(key, value)| (key.to_string(), value.to_string()))
.collect::<linear_map::LinearMap<String, String>>(),
),
hash: Default::default(),
})
}
}
#[cfg(feature = "ssr")]
impl TryFrom<&str> for Url {
type Error = String;
fn try_from(url: &str) -> Result<Self, Self::Error> {
let url = url::Url::parse(url).map_err(|e| e.to_string())?;
Ok(Self {
origin: url.origin().unicode_serialization(),
pathname: url.path().to_string(),
search: url.query().unwrap_or_default().to_string(),
hash: Default::default(),
})
}
fn normalize_wasm_url(url: &str) -> Cow<'_, str> {
Cow::Borrowed(url)
}
#[cfg(not(feature = "ssr"))]
fn normalize_wasm_url(url: &str) -> Cow<'_, str> {
Cow::Owned(format!("http://leptos{}", url))
}