-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routing): ✨ added support for relative path hosting with `PERSEU…
…S_BASE_PATH` environment variable Closes #48. BREAKING CHANGE: multiple *internal* function signatures accept exxtra parameter for path prefix
- Loading branch information
1 parent
45a0f6c
commit b7d6eb6
Showing
16 changed files
with
114 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::env; | ||
use wasm_bindgen::JsCast; | ||
use web_sys::{HtmlBaseElement, Url}; | ||
|
||
/// 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 wht's set in `<base>`. | ||
/// Trailing forward slashes will be trimmed automatically. | ||
pub fn get_path_prefix_server() -> String { | ||
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. | ||
pub fn get_path_prefix_client() -> String { | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,6 @@ define_app! { | |
locales: { | ||
default: "en-US", | ||
other: [] | ||
} | ||
}// , | ||
// path_prefix: "/perseus" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.