Skip to content

Commit 10a63d0

Browse files
authored
Fix base path (#4276)
* fix base path * remove log * fix clippy
1 parent e086d67 commit 10a63d0

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

packages/cli/src/build/request.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3841,7 +3841,7 @@ impl BuildRequest {
38413841
};
38423842

38433843
// Inject any resources from the config into the html
3844-
self.inject_resources(assets, &mut html)?;
3844+
self.inject_resources(assets, wasm_path, &mut html)?;
38453845

38463846
// Inject loading scripts if they are not already present
38473847
self.inject_loading_scripts(&mut html);
@@ -3860,7 +3860,12 @@ impl BuildRequest {
38603860
}
38613861

38623862
// Inject any resources from the config into the html
3863-
fn inject_resources(&self, assets: &AssetManifest, html: &mut String) -> Result<()> {
3863+
fn inject_resources(
3864+
&self,
3865+
assets: &AssetManifest,
3866+
wasm_path: &str,
3867+
html: &mut String,
3868+
) -> Result<()> {
38643869
use std::fmt::Write;
38653870

38663871
// Collect all resources into a list of styles and scripts
@@ -3930,19 +3935,10 @@ impl BuildRequest {
39303935
}
39313936

39323937
// Manually inject the wasm file for preloading. WASM currently doesn't support preloading in the manganis asset system
3933-
let wasm_source_path = self.wasm_bindgen_wasm_output_file();
3934-
if let Some(wasm_assets) = assets.get_assets_for_source(&wasm_source_path) {
3935-
let wasm_path = wasm_assets
3936-
.iter()
3937-
.next()
3938-
.expect("There should be exactly one optimized wasm asset");
3939-
let wasm_path = wasm_path.bundled_path();
3940-
head_resources.push_str(&format!(
3941-
"<link rel=\"preload\" as=\"fetch\" type=\"application/wasm\" href=\"/{{base_path}}/assets/{wasm_path}\" crossorigin>"
3942-
));
3943-
3944-
Self::replace_or_insert_before("{style_include}", "</head", &head_resources, html);
3945-
}
3938+
head_resources.push_str(&format!(
3939+
"<link rel=\"preload\" as=\"fetch\" type=\"application/wasm\" href=\"/{{base_path}}/assets/{wasm_path}\" crossorigin>"
3940+
));
3941+
Self::replace_or_insert_before("{style_include}", "</head", &head_resources, html);
39463942

39473943
Ok(())
39483944
}

packages/router/src/components/link.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ pub fn Link(props: LinkProps) -> Element {
163163
NavigationTarget::Internal(url) => url.clone(),
164164
NavigationTarget::External(route) => route.clone(),
165165
};
166-
// Add the history's prefix to the href for use in the rsx
167-
let full_href = router.prefix().unwrap_or_default() + &href;
166+
// Add the history's prefix to internal hrefs for use in the rsx
167+
let full_href = match &to {
168+
NavigationTarget::Internal(url) => router.prefix().unwrap_or_default() + url,
169+
NavigationTarget::External(route) => route.clone(),
170+
};
168171

169172
let mut class_ = String::new();
170173
if let Some(c) = class {

packages/router/tests/via_ssr/link.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ fn prepare<R: Routable>() -> String {
88
}
99

1010
fn prepare_at<R: Routable>(at: impl ToString) -> String {
11+
prepare_at_with_base_path::<R>(at, "")
12+
}
13+
14+
fn prepare_at_with_base_path<R: Routable>(at: impl ToString, base_path: impl ToString) -> String {
1115
let mut vdom = VirtualDom::new_with_props(
1216
App,
1317
AppProps::<R> {
1418
at: at.to_string(),
19+
base_path: base_path.to_string(),
1520
phantom: std::marker::PhantomData,
1621
},
1722
);
@@ -21,13 +26,15 @@ fn prepare_at<R: Routable>(at: impl ToString) -> String {
2126
#[derive(Props)]
2227
struct AppProps<R: Routable> {
2328
at: String,
29+
base_path: String,
2430
phantom: std::marker::PhantomData<R>,
2531
}
2632

2733
impl<R: Routable> Clone for AppProps<R> {
2834
fn clone(&self) -> Self {
2935
Self {
3036
at: self.at.clone(),
37+
base_path: self.base_path.clone(),
3138
phantom: std::marker::PhantomData,
3239
}
3340
}
@@ -44,7 +51,7 @@ fn prepare_at<R: Routable>(at: impl ToString) -> String {
4451
rsx! {
4552
h1 { "App" }
4653
HistoryProvider {
47-
history: move |_| Rc::new(MemoryHistory::with_initial_path(props.at.clone())) as Rc<dyn History>,
54+
history: move |_| Rc::new(MemoryHistory::with_initial_path(props.at.clone()).with_prefix(props.base_path.clone())) as Rc<dyn History>,
4855
Router::<R> {}
4956
}
5057
}
@@ -79,6 +86,15 @@ fn href_internal() {
7986
let expected = format!("<h1>App</h1><a {href}>Link</a>", href = r#"href="/test""#,);
8087

8188
assert_eq!(prepare::<Route>(), expected);
89+
90+
// The base path should be added to the front of internal links
91+
let base_path = "/deeply/nested/path";
92+
let expected = format!(
93+
"<h1>App</h1><a {href}>Link</a>",
94+
href = r#"href="/deeply/nested/path/test""#,
95+
);
96+
97+
assert_eq!(prepare_at_with_base_path::<Route>("/", base_path), expected);
8298
}
8399

84100
#[test]
@@ -113,6 +129,11 @@ fn href_external() {
113129
);
114130

115131
assert_eq!(prepare::<Route>(), expected);
132+
// The base path should not effect external links
133+
assert_eq!(
134+
prepare_at_with_base_path::<Route>("/", "/deeply/nested/path"),
135+
expected
136+
);
116137
}
117138

118139
#[test]

0 commit comments

Comments
 (0)