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

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

Closed
ereslibre opened this issue Aug 2, 2023 · 3 comments · Fixed by #193
Closed

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

ereslibre opened this issue Aug 2, 2023 · 3 comments · Fixed by #193
Assignees
Labels
🐛 bug Something isn't working 👋 good first issue Good for newcomers
Milestone

Comments

@ereslibre
Copy link
Member

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

@ereslibre ereslibre added 🐛 bug Something isn't working 👋 good first issue Good for newcomers labels Aug 2, 2023
@dierbei
Copy link
Contributor

dierbei commented Aug 9, 2023

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.

@dierbei
Copy link
Contributor

dierbei commented Aug 10, 2023

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)
}

dierbei added a commit to dierbei/wasm-workers-server that referenced this issue Aug 10, 2023
@dierbei dierbei mentioned this issue Aug 10, 2023
@Angelmmiguel
Copy link
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!!

@Angelmmiguel Angelmmiguel added this to the v1.5.0 milestone Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 👋 good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants