ETag - conditional GET for static files
Layer: L7 Application ยท Build flags: PROTOCORE_ENABLE_ETAG (with file serving), PROTOCORE_ENABLE_FILE_SERVING, PROTOCORE_ENABLE_MNT
What this example teaches
With ETag enabled, serve_file() / serve_static() emit a strong ETag (derived from the file size and mtime) and answer a matching If-None-Match with 304 Not Modified - so a browser re-fetching an unchanged asset gets an empty 304 instead of the whole file, saving bandwidth and time.
It is automatic. You serve files exactly as in FileServing; enabling the flag adds the ETag and the conditional-request handling:
server.serve_static("/", LittleFS, "/www");
The first request returns 200 with ETag: "..."; a follow-up that echoes that value in If-None-Match returns 304 with no body. This pairs naturally with set_cache_control() (see FileServing): the cache header lets the browser skip the request entirely until expiry, and the ETag makes the eventual revalidation cheap.
Put a file at data/www/index.html and upload the LittleFS image before running.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPROTOCORE_ENABLE_ETAG=1 -DPROTOCORE_ENABLE_FILE_SERVING=1 -DPROTOCORE_ENABLE_MNT=1" \
--lib="." examples/L7-Application/ETag/ETag.ino
curl -i http://<ip>/ # 200 + ETag: "..."
curl -i -H 'If-None-Match: "<etag>"' http://<ip>/ # 304 Not Modified
Annotated source
The complete sketch (ETag.ino), reproduced verbatim with added explanatory comments:
#define PROTOCORE_ENABLE_ETAG 1
#define PROTOCORE_ENABLE_FILE_SERVING 1
#define PROTOCORE_ENABLE_MNT 1
#include "protocore.h"
#include <LittleFS.h>
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
void setup()
{
Serial.begin(115200);
if (!LittleFS.begin(true))
Serial.println("LittleFS mount failed (upload a filesystem image)");
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
server.serve_static("/", LittleFS, "/www");
server.begin(80);
}
void loop()
{
server.handle();
}
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.
EMBED_INLINE uint32_t protocore_physical_egress_ipv4(void)
Physical.egress_ip as an expression: the address a sketch prints once its link is up.
EMBED_INLINE embed_bool protocore_physical_wifi_up(void)
Physical.wifi_ready as an expression, for a bring-up loop to wait on.
PhysicalVars PhysicalV
The operands and the outcome.
uint8_t * protocore_physical_span(void)
The PROTOCORE_PHYSICAL_BORROW bytes this module's state lives in.