Redirect in AsyncWebServer does not reload the actual page

In my ESP32 application I use the AsyncWebServer library.
I have an index.html page where I show a table dinamically built at run-time, example:

<div>
    <table id="tableEvents" class="table">
        <thead>
            <tr>
                <th scope="col" width="20px">Id</th>
                <!-- other column headers -->
            </tr>
        </thead>
        <tbody>
            ~TABLE_EVENTS~
        </tbody>
    </table>
</div>

and in my code the preprocessor call this function for `~TABLE_EVENTS~:

String WebApp::tableEvents()
{
  String html = "";

  for (int i = 0; i < _events.Count(); i++)
  {
    Event *e = _events.List()->get(i);
    if (e == nullptr)  continue;

    html += "<tr>";
    html += "<td>" + String(e->id) + "</td>";
    // other fields
    html += "</tr>\n";
  }

  return html;
}

Then I have another html page with a form to submit a new event (that will be added to the table). In the request handler I have the following code:

  _server.on("/event.html", HTTP_POST, [this](AsyncWebServerRequest *request)
  {
    Event *event = new Event();

    int id = -1;
    if (request->hasParam("id")) id = request->getParam("id")->value().toInt();
    // handling of other fields

    addEvent(event, id); // add the event to the list above
    request->redirect(F("/")); // redirect to index.html
  });

My default server configuration is:

  _server.serveStatic("/", LittleFS, "/www")
      .setDefaultFile("index.html")
      .setTemplateProcessor(std::bind(&WebApp::processor, this, std::placeholders::_1));

When I submit the form from event.html it adds a new item to the list and it redirects to index.html. The problem is I don't see the new content. It still shows the old page (without the new item). Even if I refresh the browser (hitting F5) it does change nothing.

The only way I found is to force the whole reload of the page (CTRL+F5), but of course I cannot ask the users to do that each time!

I tried it with different browsers: Chrome, Firefox and Brave and all have the same behavior, so I'm pretty sure I need to change something in my code.

What I need to set in my code to tell the browser to reload the page and not rely on its cache?

I tried to add:

 <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
 <meta http-equiv="Pragma" content="no-cache">
 <meta http-equiv="Expires" content="0">

In index.html with no luck.

1 Like

where are you using this

In the preprocessor:

String WebApp::processor(const String &var)
{
    if (var == F("TABLE_EVENTS")) return tableEvents();
}

Found the solution, I had to add:

DefaultHeaders::Instance().addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
DefaultHeaders::Instance().addHeader("Pragma", "no-cache");
DefaultHeaders::Instance().addHeader("Expires", "0");

before the _server.begin(); line.

1 Like

Thats the issue with this .it previous webpages using cached memory .Instead of loading it from device

You can also use ajax to update the content over static

I already use Ajax for some features, but it's not always the best solution.

Are there any other possibilities besides my solution using serveStatic()?

have you tried websockets..

using serverstatic() is good rather than implementing each webpage a server.on() if you have few pages you can use server.on it will load directly from server rather than cached version of webpage

1 Like

Sorry, I didn't explained it well. I'm not looking for alternative methods for communicate between client and server. I just need to avoid the browser to use its cache when I use serveStatic().

My solution works and it uses header server-side. But since you said:

Thats the issue with this .it previous webpages using cached memory .Instead of loading it from device

I understood there was a settings in the code (like settings the headers) to overcome this.
No problem otherwise, it works now :slight_smile:


Thanks, now it's clear from your last message!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.