diff --git a/components/protocols/src/rest.c b/components/protocols/src/rest.c index 07ab98c..ac81708 100755 --- a/components/protocols/src/rest.c +++ b/components/protocols/src/rest.c @@ -118,10 +118,16 @@ static esp_err_t rest_common_get_handler(httpd_req_t *req) } int fd = open(filepath, O_RDONLY, 0); if (fd == -1) { - ESP_LOGE(REST_TAG, "Failed to open file : %s", filepath); - /* Respond with 500 Internal Server Error */ - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file"); - return ESP_FAIL; + ESP_LOGW(REST_TAG, "Failed to open file : %s, redirecting to index", filepath); + /* Try to serve index.html for SPA routing */ + strlcpy(filepath, rest_context->base_path, sizeof(filepath)); + strlcat(filepath, "/index.html", sizeof(filepath)); + fd = open(filepath, O_RDONLY, 0); + if (fd == -1) { + ESP_LOGE(REST_TAG, "Failed to open index file : %s", filepath); + httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File not found"); + return ESP_FAIL; + } } set_content_type_from_file(req, filepath);