DioxusLabs / DioxusLabs/dioxus

The server cannot map a directory while running.

Open
#4,945 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

## Feature Request

The reason I need this functionality is that I'm creating a full-stack app using Dioxus. It should have a function to upload images to the server's file system, but I've found that the Dioxus server cannot route files uploaded at runtime; it can only see files that existed in the `public` directory before runtime. After some investigation, I discovered that the Dioxus server only routes files in the `public` directory one by one at runtime. Is this caching or hot reloading?

I found a way to achieve the functionality I need, but it's a bit roundabout. I think it would be more elegant and correct to solve it within Dioxus itself (maybe).

Currently, `serve_dioxus_application` only calls `register_server_functions` to collect server functions and `serve_static_assets` to cache assets in the `public` directory collected during program startup (possibly?).

## Implement Suggestion

Currently, `serve_dioxus_application` only calls `register_server_functions` to collect server functions and `serve_static_assets` to cache assets in the `public` directory collected at application startup (maybe?).

However, when a new file appears in `public` at runtime, it reports a 404 error..

The relevant code is as follows:

### server.rs > DioxusRouterExt > line 168
```
fn serve_dioxus_application(
self,
cfg: ServeConfig,
app: impl ComponentFunction<(), M> + Send + Sync,
) -> Router<()> {
self.register_server_functions()
.serve_static_assets()
.fallback(get(FullstackState::render_handler))
.with_state(FullstackState::new(cfg, app))
}
```

### My solution is as follows:
```
#[cfg(feature = "server")]
#[tokio::main]
async fn main() {
use axum::routing::get;
use dioxus::server::ServeConfig;
use dioxus::server::{DioxusRouterExt, FullstackState};

trait MyRouterExt {
fn register_static_routes(self) -> Self;
}

impl MyRouterExt for axum::Router {
fn register_static_routes(self) -> Self {
self.nest_service("/static", tower_http::services::ServeDir::new("static"))
}
}

let addr = dioxus::cli_config::fullstack_address_or_localhost();
let router = axum::Router::new()
.register_server_functions()
.serve_static_assets()
.register_static_routes()
.fallback(get(FullstackState::render_handler))
.with_state(FullstackState::new(ServeConfig::new(), App))
.into_make_service();

let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, router).await.unwrap();
}
```

It does run correctly, but I'm unsure if it breaks a part of Dioxus. I've only noticed that the `info` log output by the Dioxus server collection function is not output correctly. This is probably because I haven't called `dioxus_logger::initialize_default()` which exists in `dioxus::launch`. Therefore, it's foreseeable that my implementation is destined to lack some functionality present in Dioxus. I hope that Dioxus will provide a more correct, elegant, and extensible implementation.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.