Can a worker domain own its cache?
cache.match() always returns null. Am I using cache incorrectly? I'm trying to
- check for cache.match(request.url) (request.url = workerDomain/data?type=1)
- if no match, generate data via expensive function
- waitFor(cache.put(request.url, new Response(data)
It doesn't work. Is that because there's no host other than a worker? Is a 3rd-party host required to use cache? Can a worker use any arbitrary cache key within its domain?
2 Replies
Cache API requires the use of a custom domain (or route) and doesnt work on the default workers.dev domain
I'm using a custom domain. What I meant is that no domain is being proxied, serving data that would be cached eg from a WordPress site. Without a server, can I still use the cache API? Can a worker (worker.domain.com) generate data and then cache it to an arbitrary url/key within the same domain?
For example
first run
- check cache for data @ worker.domain.com/data?type=1
- cache misses, generate data
- cache data @ worker.domain.com/data?type=1
- return data
subsequent runs
- check cache for data @ worker.domain.com/data?type=1
- return data from cache
I'm beating my head against the wall -
I've tried both of these vanilla examples
https://developers.cloudflare.com/workers/examples/cache-api/
https://developers.cloudflare.com/workers/examples/cache-using-fetch/
I can never get a cache match. I've worked with various routes/triggers of customdomain/, customdomain/, customdomain/test/, customdomain/test/ etc. I've set a rule for Cache Level: Cache Everything.
I've used the cache API before, and it's beyond me why I can't get these examples to work. What could I be missing?