1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/// Gets the path prefix to apply on the server. This uses the `PERSEUS_BASE_PATH` environment variable, which avoids hardcoding
/// something as changeable as this into the final binary. Hence however, that variable must be the same as what's set in `<base>` (done
/// automatically).
/// Trailing forward slashes will be trimmed automatically.
#[cfg(not(target_arch = "wasm32"))]
pub fn get_path_prefix_server() -> String {
    use std::env;

    let base_path = env::var("PERSEUS_BASE_PATH").unwrap_or_else(|_| "".to_string());
    base_path
        .strip_suffix('/')
        .unwrap_or(&base_path)
        .to_string()
}

/// Gets the path prefix to apply in the browser. This uses the HTML `<base>` element, which would be required anyway to make Sycamore's
/// router co-operate with a relative path hosting.
#[cfg(target_arch = "wasm32")]
pub fn get_path_prefix_client() -> String {
    use wasm_bindgen::JsCast;
    use web_sys::{HtmlBaseElement, Url};

    let base_path = match web_sys::window()
        .unwrap()
        .document()
        .unwrap()
        .query_selector("base[href]")
    {
        Ok(Some(base)) => {
            let base = base.unchecked_into::<HtmlBaseElement>().href();

            let url = Url::new(&base).unwrap();
            url.pathname()
        }
        _ => "".to_string(),
    };
    // Strip any trailing slashes, a `//` makes the browser query `https://.perseus/...`
    base_path
        .strip_suffix('/')
        .unwrap_or(&base_path)
        .to_string()
}