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.