Skip to content

When panel is enabled, only /index.html can be directly visited #192

Closed

Description

@ereslibre

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:

let path = if path.len() == 0 {
"index.html"
} else {
path.as_str().strip_prefix('/').unwrap()
};
match Asset::get(path) {
Some(content) => HttpResponse::Ok()
.content_type(from_path(path).first_or_octet_stream().as_ref())
.body(content.data.into_owned()),
None => HttpResponse::NotFound().body("404 Not Found"),
}

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:

  1. If path is empty, default to index.html, as per the current logic.
  2. If an asset exists, serve it, as per the current logic.
  3. 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

Activity

  1. dierbei commented on Aug 9, 2023

    @dierbei
    Contributor

    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.

  2. dierbei commented on Aug 10, 2023

    @dierbei
    Contributor

    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)
    }
  3. added a commit that references this issue on Aug 10, 2023
    8ba35bb
  4. added a commit that references this issue on Aug 10, 2023
    42dfc27
  5. Angelmmiguel commented on Aug 10, 2023

    @Angelmmiguel
    Contributor

    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!!

  6. added this to the v1.5.0 milestone on Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions