Cache POST requests on worker not working

Hi,
I’ve created a worker following this example:


but it seems that cache isn’t working.

Debugging I’ve understood that cache write command
event.waitUntil(cache.put(cacheKey, response.clone()))
is not working, because if I try to get content from cache right above I can’t get it.

Can you please help me? What I have to do in order to cache my POST requests through workers?

Thanks
GB

You are using the identical code I presume, right?

Why do you think it does not work? What does match return? Did you make sure the key is the same, but if you are using the same code that shouldn’t be the issue.

Hi Sandro,
thank you for your reply.
About the code, I’ve just deleted the part about someOtherHostname obviusly, I don’t need it, I just need to cache my POST request.
match returns empty string, also calling it just above cache.put , and I’ve checked cacheKey is always correct.
It seems like cache.put is not working

Did you check what the promise returned by put says?

You might also want to check what you pass to put and if this is the correct data.

I’ve checked what I passed to put, using JSON.stringify , because this is response object, and I don’t know how to print otherwise.

This is the result:
{“webSocket”:null,“url”:“xxxxxxxxx.xxxxxxx.xxxxx/xxxxx”,“redirected”:false,“ok”:true,“headers”:{},“statusText”:“OK”,“status”:200,“bodyUsed”:false,“body”:{“locked”:false}}

But body content should be:
{“banners”: }

I think that can be the root of the problem, but I don’t know if I’m performing a bad debug.

GB

Yes, if you don’t store the right data in the cache that would be the problem.

You really need to double check if the request key and response value have all the right values. Unfortunately software development and debugging is beyond the scope of the forum, StackOverflow might help you here.

Hi sandro,
I’ve finally understood the root of the problem.
Cache on worker is not working on dev enviroment (I don’t know why my chrome doesn’t show this warning like firefox).
Anyway now I’m using wrangler, I’ve published worker but it seems it doesn’t work.

How can I check that my worker is correctly in production? There’s no indicator on cloudflare panel about that.

Thank you, regards
GB

Is your Worker doing what it is supposed to do? You can check the code in the dashboard and verify that it is the current one.

Yes, it is the current one. But I still can’t get cache working.
The only way to use cache is in production but I can’t debug it because I’m not in dev…this is an ‘enpass’

GB

Yes, there are certain features which you can only test in production. I would suggest you run the code in the production environment and add necessary debug statements (e.g. you could return a response with the information in question).

Caching itself works, if it does not in your case that will be because of something in your code I am afraid.

According to https://blog.cloudflare.com/announcing-wrangler-dev-the-edge-on-localhost/ the caching should however work with Wrangler. You might have a general code issue in that case maybe

Cache API, KV, and more!

Since wrangler dev runs on the edge, you can now easily test the state of a cache.put(), without having to deploy your Worker to production.

wrangler dev will spin up a new KV namespace for development, so you don’t have to worry about affecting your production data.

And if you’re looking to test out some of the features provided on request.cf that provide rich information about the request such as geo-location — they will all be provided from the Cloudflare data center.

I’ve tried again and again, but cache never work.
I’m following “cache api” example and cloudflare documentation without success.
I’ve tried to use wrangler dev but is very tricky to use in my test environment; maybe I can do it but spending too much time, for something that should already work in production.
Right now I think can’t get this solution working so I’ve to try something else for my purposes.

Thank you
GB

I ran a cache test earlier and it cached just fine, so there shouldn’t be an issue with the API. I am afraid you’ll either be still in the wrong environment or have some issues in your code. Maybe you are storing the wrong request object. Check for any errors. StackOverflow might also be able to help you with code related issues.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.