When panel is enabled, only `/index.html` can be directly visited
ereslibre opened this issue · comments
Describe the bug
When the panel is enabled --enable-panel
, the logic that handles the panel endpoints will only serve /index.html
and any assets that are discovered under client/dist
when building.
The logic that handles this is at:
wasm-workers-server/crates/panel/src/handlers/panel.rs
Lines 18 to 29 in 549387b
There are other virtual paths such as /_panel/workers
that are not covered by any of this cases.
Ideally, to avoid duplicating routing in multiple places, this logic should:
- If path is empty, default to
index.html
, as per the current logic. - If an asset exists, serve it, as per the current logic.
- Otherwise, serve a static page, that will perform an AJAX request to the current path and will forward the response to the client.
This way, we keep the current Single Page Application (SPA) approach, while making arbitrary paths directly visitable (e.g. reload endpoint, write path in location bar and hit enter...)
Reproduction steps
Run wws with --enable-panel
. Visit /_panel/workers
. The server will return a 404 HTTP status code.
Expected behavior
Visiting /_panel/workers
directly should be equivalent to visiting /
and navigating through the UI to the list of workers.
Additional context
No response
I reproduced the problem, except I don't know what it is now.
But it looks like the odds are that the front-end file isn't receiving the route forwarding.
Because I tried to change the path to /_panel/workers
and I can't access it either.
It only works if you visit /panel
first, go to the UI
and then click on the sidebar.
I tried to fix this bug and was able to access /_panel/workers
through my browser.
I mainly utilized the fact that the pages I couldn't find were using the front end to forward requests.
I'm not sure if this is the desired result, can you help me out?
/// Find the static assets of the administration panel
#[actix_web::get("/_panel{_:.*}")]
pub async fn handle_static_panel(path: web::Path<String>) -> impl Responder {
let path = if path.is_empty() {
"index.html"
} else {
path.as_str().strip_prefix('/').unwrap()
};
let (content_type, content_data) = Asset::get(path)
.map(|content| {
(
from_path(path).first_or_octet_stream().to_string(),
content.data.into_owned(),
)
})
.unwrap_or_else(|| {
let default_content = Asset::get("index.html").unwrap();
(
"text/html; charset=utf-8".to_string(),
default_content.data.into_owned(),
)
});
HttpResponse::Ok()
.content_type(content_type)
.body(content_data)
}
Hello @dierbei,
Thank you for taking the issue and submit the PR to fix it! 👏
I tried to fix this bug and was able to access /_panel/workers through my browser.
I mainly utilized the fact that the pages I couldn't find were using the front end to forward requests.
I'm not sure if this is the desired result, can you help me out?
Yes, this is the expected result. The administration panel is a Single Page Application (SPA), so the routing happens in the browser. If you access the panel on /
and then navigate to /workers
through a link, it works. However, if you reload the site, it will return a 404 error as the admin panel dist
package doesn't include any workers.html
file.
The solution you proposed fix this error by returning the index.html
file when there's no file in the admin panel dist
folder that matches it.
But it looks like the odds are that the front-end file isn't receiving the route forwarding.
Because I tried to change the path to /_panel/workers and I can't access it either.
This is the reason why you were getting these errors. The server didn't find any workers.html
file.
Thank you!!