How to send 304 response on repeated visits?

I am trying to replicate polyfill.io functionality with Cloudflare Workers. Here is my code gist https://gist.github.com/asvny/b910527e6b8fe12c1051eb40bf8c138c

Say for example a person X visits our site: ( Currently how it works )

  1. On initial visit = Status: 200
    Since the cache is empty it generates the response and caches it and then finally sends it with appropriate caching headers and ETag.
  2. On repeated visit = Status: 200
    This time, the cache has the content, so it responds immediately but with status 200 code.

What I expect to happen is,

  1. On initial visit = status: 200
    2: On repeated visit = status: 304
    Since I send an ETag header, I expect the worker to check based on request’s If-none-match header and then respond with a 304 status code.

Note: There is no external fetch requests. All the content is generated in the worker script itself.

But am I not sure how to do it in Cloudflare worker? Thank you.

You’ll have to manage that in your worker code. Generate the etag and compare it to the if-non-match header. If they match you return a 304 without any body.