I have a lambda function that creates a PDF from url. (See the source code at the bottom of this post).
It should be smart enough to use netlify’s caching system. If a request comes in with an etag in the “if-none-equals”, that etag is compared with the current etag of the source document. If the etags are the same, a status code of 304 is returned with an empty body. Otherwise, the PDF is generated and a 200 is returned. For example, here are the header responses (locally) on 304 with a curl:
HTTP/1.1 304 Not Modified
X-Powered-By: Express
etag: “69d78a182422efb388bd0d67be027c43-ssl”
cache-control: public, max-age=0, must-revalidate
age: 24484
x-nf-request-id: 01FPXBZZ66SQ7WZ6TV5E8DNTSW
Date: Tue, 14 Dec 2021 21:01:24 GMT
Connection: keep-alive
Keep-Alive: timeout=5
That’s how the code is written and how it runs locally on the netlify dev server. Great.
Once deployed, however, the code always returns 200, regardless of the etags. See for example this request x-nf-request-id: 01FPXBGJB2E78VY1J2T7FBJG4J. So, why won’t 304 work? For reference, here is what the response looks like:
HTTP/2 200
cache-control: public, max-age=0, must-revalidate
content-disposition: filename=“PSprints-ets-keathley-gae.pdf”
content-type: application/pdf
link: https://peacefulscience.org/prints/ets-keathley-gae; rel=“canonical”
server: Netlify
x-nf-request-id: 01FPXCC8B7337M3BV1VGT5GPBD
content-length: 1363195
date: Tue, 14 Dec 2021 21:08:06 GMT
age: 21
etag: “a502404ce6b7e8c9ec44d7a66c330b40-ssl”
The curl request was:
curl https://peacefulscience.org/pdf/prints/ets-keathley-gae/ -H ‘if-none-equals: “c613561eba2f7733b4b1deeba3fcb149-ssl”’ -I
The 200 response includes a body with the pdf, and the age increases with time, so it seems that netlify is caching and serving the result rather than responding with a 304. Is this intended behavior? It seems that this is impacting performance on the client side, by requiring re-downloads of files when it is unneeded.
Is there a way to fix this?