Skip to content

Commit 5217ed9

Browse files
authored
Use --public-url value in router example (#1597)
* Use --public-url value for router example * some documentation
1 parent 7f8b039 commit 5217ed9

8 files changed

Lines changed: 35 additions & 18 deletions

File tree

.github/workflows/publish-examples.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ jobs:
5353
cd "$path"
5454
dist_dir="$output/$example"
5555
56-
export PUBLIC_URL="$PUBLIC_URL_PREFIX/$example"
57-
trunk build --release --dist "$dist_dir" --public-url "$PUBLIC_URL"
56+
trunk build --release --dist "$dist_dir" --public-url "$PUBLIC_URL_PREFIX/$example"
5857
)
5958
done
6059

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ orig.*
55
/.idea
66
/.vscode
77
/cmake-build-debug
8+
/dist
89
/website/yarn.lock
910
/website/node_modules
1011
/website/package-lock.json

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = [
1919
"yew-dsl",
2020

2121
# Examples
22+
"examples/boids",
2223
"examples/counter",
2324
"examples/crm",
2425
"examples/dashboard",
@@ -39,5 +40,4 @@ members = [
3940
"examples/todomvc",
4041
"examples/two_apps",
4142
"examples/webgl",
42-
"examples/boids",
4343
]

examples/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/router/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.1.0"
44
edition = "2018"
55

66
[dependencies]
7-
default-env = "0.1"
87
instant = { version = "0.1", features = ["wasm-bindgen"] }
98
lipsum = "0.7"
109
log = "0.4"

examples/router/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ This example involves many different parts, here are just the Yew specific thing
2323
- Uses [`yew-router`] to render and switch between multiple pages.
2424
- Uses [`IntervalService`] for the [`ProgressDelay`](src/components/progress_delay.rs) component.
2525

26+
The example automatically adapts to the `--public-url` value passed to Trunk.
27+
This allows it to be hosted on any path, not just at the root.
28+
For example, our demo is hosted at [/router](https://examples.yew.rs/router).
29+
30+
This is achieved by adding `<base data-trunk-public-url />` to the [index.html](index.html) file.
31+
Trunk rewrites this tag to contain the value passed to `--public-url` which can then be retrieved at runtime.
32+
Take a look at [`PublicUrlSwitch`](src/switch.rs) for the implementation.
33+
2634
## Improvements
2735

2836
- Use a special image component which shows a progress bar until the image is loaded.

examples/router/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1" />
66

77
<title>Yew • Router</title>
8+
<base data-trunk-public-url />
89
<link
910
rel="stylesheet"
1011
href="https://cdn.jsdelivr.net/npm/bulma@0.9.0/css/bulma.min.css"

examples/router/src/switch.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use default_env::default_env;
2-
use yew::virtual_dom::{Transformer, VComp};
1+
use yew::{
2+
virtual_dom::{Transformer, VComp},
3+
web_sys::Url,
4+
};
35
use yew_router::{components::RouterAnchor, prelude::*, switch::Permissive};
46

57
#[derive(Clone, Debug, Switch)]
@@ -35,16 +37,24 @@ impl AppRoute {
3537
#[derive(Clone, Debug)]
3638
pub struct PublicUrlSwitch(AppRoute);
3739
impl PublicUrlSwitch {
38-
// this variable is set by the build script
39-
const PUBLIC_URL: &'static str = default_env!("PUBLIC_URL", "/");
40+
fn base_url() -> Url {
41+
if let Ok(Some(href)) = yew::utils::document().base_uri() {
42+
// since this always returns an absolute URL we turn it into `Url`
43+
// so we can more easily get the path.
44+
Url::new(&href).unwrap()
45+
} else {
46+
Url::new("/").unwrap()
47+
}
48+
}
49+
50+
fn base_path() -> String {
51+
let mut path = Self::base_url().pathname();
52+
if path.ends_with('/') {
53+
// pop the trailing slash because AppRoute already accounts for it
54+
path.pop();
55+
}
4056

41-
/// Return `PUBLIC_URL` without a trailing slash.
42-
/// This is required because `AppRoute` still expects paths to
43-
/// start with a slash so we only want to strip away the parts before that.
44-
fn public_url_no_trailing_slash() -> &'static str {
45-
Self::PUBLIC_URL
46-
.strip_suffix('/')
47-
.unwrap_or(Self::PUBLIC_URL)
57+
path
4858
}
4959

5060
pub fn route(self) -> AppRoute {
@@ -53,7 +63,7 @@ impl PublicUrlSwitch {
5363
}
5464
impl Switch for PublicUrlSwitch {
5565
fn from_route_part<STATE>(part: String, state: Option<STATE>) -> (Option<Self>, Option<STATE>) {
56-
if let Some(part) = part.strip_prefix(Self::public_url_no_trailing_slash()) {
66+
if let Some(part) = part.strip_prefix(&Self::base_path()) {
5767
let (route, state) = AppRoute::from_route_part(part.to_owned(), state);
5868
(route.map(Self), state)
5969
} else {
@@ -62,7 +72,7 @@ impl Switch for PublicUrlSwitch {
6272
}
6373

6474
fn build_route_section<STATE>(self, route: &mut String) -> Option<STATE> {
65-
route.push_str(Self::public_url_no_trailing_slash());
75+
route.push_str(&Self::base_path());
6676
self.0.build_route_section(route)
6777
}
6878
}

0 commit comments

Comments
 (0)