esp8266-react icon indicating copy to clipboard operation
esp8266-react copied to clipboard

Enable cache on static entries

Open raomin opened this issue 3 years ago • 3 comments

Hello @rjwats,

I wanted to suggest to add some cache on all serveStatic entries. .setCacheControl("max-age=86400") does get rid of the 7 sec (in my case) for the initial loading of the application.

raomin avatar Mar 20 '21 23:03 raomin

Sounds good to me

On Sat, 20 Mar 2021, 23:05 Raomin, @.***> wrote:

Hello @rjwats https://github.com/rjwats,

I wanted to suggest to add some cache on all serveStatic entries. .setCacheControl("max-age=86400") does get rid of the 7 sec (in my case) for the initial loading of the application.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rjwats/esp8266-react/issues/234, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKE4VBTFRPWNBFT3AY2RILTEUS43ANCNFSM4ZQ4ZS4A .

rjwats avatar Mar 20 '21 23:03 rjwats

@raomin I'll get to this this weekend, it's a sensible suggestion.

Do we think it's worth omitting index.html from the cache though? Otherwise after a deployment you will be forced to do a shift-refresh to pick up the new JavaScript artefacts.

rjwats avatar Mar 24 '21 08:03 rjwats

index.html is very small so yes I would go for omitting it.

Ah because it's next to it, I wanted to suggest something else related to the following issue: When I connect to the captive portal my phone or computer does a lot of requests. Currently it ends up in index.html which, for some services or phone apps, would be parsed and would query the JS files. All this is a massive load for a tiny esp32 on spiffs and often triggers a watchdog timeout.

So I changed a bit the way the index.html is returned to make sure only the browser redirect test get's it:

  server->serveStatic("/favicon.ico", ESPFS, "/www/favicon.ico","max-age=600000");
  server->serveStatic("/index.html", ESPFS, "/www/index.html");
  // Serving all other get requests with "/www/index.htm"
  // OPTIONS get a straight up 200 response
    server->onNotFound([](AsyncWebServerRequest* request) {
    Serial.printf("got 404 request for %s/%s\n", request->host().c_str(), request->url().c_str());
    if (request->method() == HTTP_GET) {
      if (request->host().indexOf("connect") >= 0 || request->host().indexOf("msft") >= 0 ||
          request->host().startsWith("192.168") || request->url().equals("/") ||
          request->url().startsWith("/project") || request->hasHeader("Referer")) {
        request->redirect("/index.html");
      }
      // request->send(ESPFS, "/www/index.html");
    } else if (request->method() == HTTP_OPTIONS) {
      request->send(200);
    } else {
      request->send(404);
    }
  });

192.168 should be replaced by the current AP IP of course...

This limit greatly the amount of requests served.

raomin avatar Apr 04 '21 21:04 raomin