cannot access cached pages created by worker
Hi. Beginner programmer here, using lots of help from AI. On free cloudflare plan.
I am trying to fetch some data, do some math on it and create a page with the data. I expect the data to be cached for a full day instead of fetching and creating a new page on each request.
I succesfully created an HTTP endpoint which can read the previously stored data (in KV), create the webpage and cache it. I can then access the page from my browser. This is all good.
My problem is I want this to happen on a schedule. I created the scheduler, which successfully runs. fetches and stores the data. Here is where my problems start. From this scheduler I fetch the http endpoint mentioned earlier, which should trigger the creation of the website and caching. According to the logs I am succesfully doing this but when I try to access the cached page from my local browser i get a cache not found.
I have already attempted doing this by triggering the http endpoint. I have also done it using internal function calls and finally with lots of AI help i changed it to do the same with RPC.
In all three methods I theoretically am creating the page and caching it with no logged errors, but when I try to access it with my browser i cannot find the page. (as i mentioned earlier, if I trigger the http endpoint from my browser, then it does work).
What am I missing? Could it be that when the worker caches the page it is cached in a zone different than the one my browser accesses? If yes, how do i solve this?
What other things should I check?
thank you!
4 Replies
What does the code look like up to and including your call to
caches.default.put
?Hi, Here is the section of the current code. This was all written by AI since nothing I tried worked. This version is using internal function calls as fetching the working http endpoint didnt work either.
When you add something to the cache my understanding is that it needs a cache control header to specify how the cache handles it. What do the headers on the response object that you are caching look like? You need a header like : 'Cache-Control': 's-maxage=10000' so it gets remembered for a period of time, in this case 10000 seconds.
On a side note. I love the AI tools for helping me generate code but I have found they often have an issue with context and understanding your specific environment and / or goal. Any large chunks of AI I have ever used needed reworking, individual lines are often 100% correct. I would build up your code one piece at a time and firmly understand what each line does, ideally by reading the documentation.
I turned to the AI assistant once I ran out of options. Nothing works. I will go back to using simple fetch of the functioning http endpoint. That one kind of worked at one point, though I haven't been able to get back to that yet.
Thanks.