Use --public-url value in router example (#1597)

* Use --public-url value for router example

* some documentation
This commit is contained in:
Simon 2020-10-02 01:11:55 +02:00 committed by GitHub
parent 7f8b0397ff
commit 5217ed9347
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 18 deletions

View File

@ -53,8 +53,7 @@ jobs:
cd "$path" cd "$path"
dist_dir="$output/$example" dist_dir="$output/$example"
export PUBLIC_URL="$PUBLIC_URL_PREFIX/$example" trunk build --release --dist "$dist_dir" --public-url "$PUBLIC_URL_PREFIX/$example"
trunk build --release --dist "$dist_dir" --public-url "$PUBLIC_URL"
) )
done done

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ orig.*
/.idea /.idea
/.vscode /.vscode
/cmake-build-debug /cmake-build-debug
/dist
/website/yarn.lock /website/yarn.lock
/website/node_modules /website/node_modules
/website/package-lock.json /website/package-lock.json

View File

@ -19,6 +19,7 @@ members = [
"yew-dsl", "yew-dsl",
# Examples # Examples
"examples/boids",
"examples/counter", "examples/counter",
"examples/crm", "examples/crm",
"examples/dashboard", "examples/dashboard",
@ -39,5 +40,4 @@ members = [
"examples/todomvc", "examples/todomvc",
"examples/two_apps", "examples/two_apps",
"examples/webgl", "examples/webgl",
"examples/boids",
] ]

1
examples/.gitignore vendored
View File

@ -1 +0,0 @@
*/dist/

View File

@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
default-env = "0.1"
instant = { version = "0.1", features = ["wasm-bindgen"] } instant = { version = "0.1", features = ["wasm-bindgen"] }
lipsum = "0.7" lipsum = "0.7"
log = "0.4" log = "0.4"

View File

@ -23,6 +23,14 @@ This example involves many different parts, here are just the Yew specific thing
- Uses [`yew-router`] to render and switch between multiple pages. - Uses [`yew-router`] to render and switch between multiple pages.
- Uses [`IntervalService`] for the [`ProgressDelay`](src/components/progress_delay.rs) component. - Uses [`IntervalService`] for the [`ProgressDelay`](src/components/progress_delay.rs) component.
The example automatically adapts to the `--public-url` value passed to Trunk.
This allows it to be hosted on any path, not just at the root.
For example, our demo is hosted at [/router](https://examples.yew.rs/router).
This is achieved by adding `<base data-trunk-public-url />` to the [index.html](index.html) file.
Trunk rewrites this tag to contain the value passed to `--public-url` which can then be retrieved at runtime.
Take a look at [`PublicUrlSwitch`](src/switch.rs) for the implementation.
## Improvements ## Improvements
- Use a special image component which shows a progress bar until the image is loaded. - Use a special image component which shows a progress bar until the image is loaded.

View File

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Yew • Router</title> <title>Yew • Router</title>
<base data-trunk-public-url />
<link <link
rel="stylesheet" rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bulma@0.9.0/css/bulma.min.css" href="https://cdn.jsdelivr.net/npm/bulma@0.9.0/css/bulma.min.css"

View File

@ -1,5 +1,7 @@
use default_env::default_env; use yew::{
use yew::virtual_dom::{Transformer, VComp}; virtual_dom::{Transformer, VComp},
web_sys::Url,
};
use yew_router::{components::RouterAnchor, prelude::*, switch::Permissive}; use yew_router::{components::RouterAnchor, prelude::*, switch::Permissive};
#[derive(Clone, Debug, Switch)] #[derive(Clone, Debug, Switch)]
@ -35,16 +37,24 @@ impl AppRoute {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct PublicUrlSwitch(AppRoute); pub struct PublicUrlSwitch(AppRoute);
impl PublicUrlSwitch { impl PublicUrlSwitch {
// this variable is set by the build script fn base_url() -> Url {
const PUBLIC_URL: &'static str = default_env!("PUBLIC_URL", "/"); if let Ok(Some(href)) = yew::utils::document().base_uri() {
// since this always returns an absolute URL we turn it into `Url`
// so we can more easily get the path.
Url::new(&href).unwrap()
} else {
Url::new("/").unwrap()
}
}
/// Return `PUBLIC_URL` without a trailing slash. fn base_path() -> String {
/// This is required because `AppRoute` still expects paths to let mut path = Self::base_url().pathname();
/// start with a slash so we only want to strip away the parts before that. if path.ends_with('/') {
fn public_url_no_trailing_slash() -> &'static str { // pop the trailing slash because AppRoute already accounts for it
Self::PUBLIC_URL path.pop();
.strip_suffix('/') }
.unwrap_or(Self::PUBLIC_URL)
path
} }
pub fn route(self) -> AppRoute { pub fn route(self) -> AppRoute {
@ -53,7 +63,7 @@ impl PublicUrlSwitch {
} }
impl Switch for PublicUrlSwitch { impl Switch for PublicUrlSwitch {
fn from_route_part<STATE>(part: String, state: Option<STATE>) -> (Option<Self>, Option<STATE>) { fn from_route_part<STATE>(part: String, state: Option<STATE>) -> (Option<Self>, Option<STATE>) {
if let Some(part) = part.strip_prefix(Self::public_url_no_trailing_slash()) { if let Some(part) = part.strip_prefix(&Self::base_path()) {
let (route, state) = AppRoute::from_route_part(part.to_owned(), state); let (route, state) = AppRoute::from_route_part(part.to_owned(), state);
(route.map(Self), state) (route.map(Self), state)
} else { } else {
@ -62,7 +72,7 @@ impl Switch for PublicUrlSwitch {
} }
fn build_route_section<STATE>(self, route: &mut String) -> Option<STATE> { fn build_route_section<STATE>(self, route: &mut String) -> Option<STATE> {
route.push_str(Self::public_url_no_trailing_slash()); route.push_str(&Self::base_path());
self.0.build_route_section(route) self.0.build_route_section(route)
} }
} }