ESPAsyncWebServer
ESPAsyncWebServer copied to clipboard
API Latency/Slowness/Delays When Using ServeStatic (Documentation Issue)
I was running into an issue where extremely simple GET handlers were taking 400ms+ to execute. As it turns out, the cause was that my SPIFFS ServeStatic
handler was added before my API handlers, causing SPIFFS to be searched (a slow operation) before any API route was even checked. The solution was to simply move the API handlers above the ServeStatic handler.
While logical, this problem isn't at all obvious unless you're specifically thinking about it. It's also nowhere in the documentation from what I can see. It's a pretty silly problem that - at this point - has probably affected 4-5 of my projects, simply because I always attributed the poor performance to the fact that the code was running on a microcontroller.
This code is wrong, and the API endpoints (/state
and /config
) will have huge latency (In my case, 400ms).
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
server.on("/state", HTTP_GET, handle_state);
server.on("/config", HTTP_GET, handle_config);
This is correct!! /state
and /config
execute in ~6ms now.
server.on("/state", HTTP_GET, handle_state);
server.on("/config", HTTP_GET, handle_config);
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
The caveat that API routes should be added before static routes should be clearly highlighted in the README documentation.