Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the base path in the CLI #2756

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions packages/cli/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,7 @@ pub(crate) fn process_assets(
manifest: &AssetManifest,
progress: &mut UnboundedSender<UpdateBuildProgress>,
) -> anyhow::Result<()> {
let static_asset_output_dir = PathBuf::from(
config
.dioxus_config
.web
.app
.base_path
.clone()
.unwrap_or_default(),
);
let static_asset_output_dir = config.out_dir().join(static_asset_output_dir);
let static_asset_output_dir = config.out_dir();

std::fs::create_dir_all(&static_asset_output_dir)
.context("Failed to create static asset output directory")?;
Expand Down Expand Up @@ -96,10 +87,14 @@ pub(crate) fn process_assets(
pub(crate) struct AssetConfigDropGuard;

impl AssetConfigDropGuard {
pub fn new() -> Self {
pub fn new(base_path: Option<&str>) -> Self {
// Set up the collect asset config
let base = match base_path {
Some(base) => format!("/{}/", base.trim_matches('/')),
None => "/".to_string(),
};
manganis_cli_support::Config::default()
.with_assets_serve_location("/")
.with_assets_serve_location(&base)
.save();
Self {}
}
Expand Down
23 changes: 16 additions & 7 deletions packages/cli/src/builder/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ impl BuildRequest {
&dioxus_version,
);
let _manganis_support = ManganisSupportGuard::default();
let _asset_guard = AssetConfigDropGuard::new();
let _asset_guard =
AssetConfigDropGuard::new(self.dioxus_crate.dioxus_config.web.app.base_path.as_deref());

// If this is a web, build make sure we have the web build tooling set up
if self.web {
Expand Down Expand Up @@ -163,7 +164,7 @@ impl BuildRequest {
// Start Manganis linker intercept.
let linker_args = vec![format!("{}", self.dioxus_crate.out_dir().display())];

// Don't block the main thread - magnanis should not be running its own std process but it's
// Don't block the main thread - manganis should not be running its own std process but it's
// fine to wrap it here at the top
tokio::task::spawn_blocking(move || {
manganis_cli_support::start_linker_intercept(
Expand Down Expand Up @@ -196,11 +197,19 @@ impl BuildRequest {

let assets = if !self.build_arguments.skip_assets {
let assets = asset_manifest(&self.dioxus_crate);
// Collect assets
process_assets(&self.dioxus_crate, &assets, progress)?;
// Create the __assets_head.html file for bundling
create_assets_head(&self.dioxus_crate, &assets)?;
Some(assets)
let dioxus_crate = self.dioxus_crate.clone();
let mut progress = progress.clone();
tokio::task::spawn_blocking(
move || -> Result<Option<manganis_cli_support::AssetManifest>> {
// Collect assets
process_assets(&dioxus_crate, &assets, &mut progress)?;
// Create the __assets_head.html file for bundling
create_assets_head(&dioxus_crate, &assets)?;
Ok(Some(assets))
},
)
.await
.unwrap()?
} else {
None
};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/serve/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Builder {
if let Err(err) = &res {
let _ = tx.start_send(UpdateBuildProgress {
stage: crate::builder::Stage::Finished,
update: crate::builder::UpdateStage::Failed(err.to_string()),
update: crate::builder::UpdateStage::Failed(format!("{err}")),
});
}

Expand Down
24 changes: 13 additions & 11 deletions packages/cli/src/serve/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,23 @@ fn setup_router(
router = super::proxy::add_proxy(router, proxy_config)?;
}

// Setup base path redirection
if let Some(base_path) = config.dioxus_config.web.app.base_path.clone() {
let base_path = format!("/{}", base_path.trim_matches('/'));
router = Router::new()
.nest(&base_path, router)
.fallback(get(move || async move {
format!("Outside of the base path: {}", base_path)
}));
}

// server the dir if it's web, otherwise let the fullstack server itself handle it
match platform {
Platform::Web => {
// Route file service to output the .wasm and assets if this is a web build
router = router.nest_service("/", build_serve_dir(serve, config));
let base_path = format!(
"/{}",
config
.dioxus_config
.web
.app
.base_path
.as_deref()
.unwrap_or_default()
.trim_matches('/')
);

router = router.nest_service(&base_path, build_serve_dir(serve, config));
}
Platform::Fullstack | Platform::StaticGeneration => {
// For fullstack and static generation, forward all requests to the server
Expand Down
Loading