peterz
peterz
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
oh nevermind, I just found the config place in vitest 🙂
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
now I'm a bit confused about secrets. What's the proper way of testing a SELF.fetch(request) and having some secrets as environment variables?
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
just to add another detail, as I said previously, the original worker behaves as expected. The difference is that the origin responses had the cache-control header, and my mock responses didn't.
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
OMG 🤦 I just tested, of course, it makes sense... Thank you very much @James!!! BTW, have a great new year!
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
I wasn't using it to check, it was just to make the test fail. I was checking in the logs. I created this minimal example (from here) where we can see both in the logs and the fail test. Worker:
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = request.url;
const cacheKey = url;
const cache = caches.default;

let response = await cache.match(cacheKey);
if (response) {
console.log('Cache HIT:', url);
return new Response('Cached response');
}
console.log('Cache MISS:', url);

response = new Response('Fresh response');
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
},
} satisfies ExportedHandler<Env>;
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = request.url;
const cacheKey = url;
const cache = caches.default;

let response = await cache.match(cacheKey);
if (response) {
console.log('Cache HIT:', url);
return new Response('Cached response');
}
console.log('Cache MISS:', url);

response = new Response('Fresh response');
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
},
} satisfies ExportedHandler<Env>;
Tests:
describe('Hello World worker', () => {
it.only('responds with Hello World! (twice)', async () => {
const response = await SELF.fetch('https://example.com');
expect(await response.text()).toEqual('Fresh response');

const response2 = await SELF.fetch('https://example.com');
expect(await response2.text()).toEqual('Cached response');
});
});
describe('Hello World worker', () => {
it.only('responds with Hello World! (twice)', async () => {
const response = await SELF.fetch('https://example.com');
expect(await response.text()).toEqual('Fresh response');

const response2 = await SELF.fetch('https://example.com');
expect(await response2.text()).toEqual('Cached response');
});
});
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
I changed the implementation and it still doesn't work. Now I'm testing with two sequential requests to same url, and both are hitting the origin... tried both with SELF.fetch and worker.fetch. No luck.
it('responds with cached response in second request', async () => {
const ctx = createExecutionContext();
const response = await worker.fetch(new IncomingRequest(DOCS_ENDPOINT), env, ctx);
// const response = await SELF.fetch(new IncomingRequest(DOCS_ENDPOINT));
await waitOnExecutionContext(ctx);
expect(await response.text()).toBe(`fresh`);
expect(response.headers.get("CF-Cache-Status")).toBe(null);
console.log('cache status header:', response.headers.get("CF-Cache-Status"));

const ctx2 = createExecutionContext();
const response2 = await worker.fetch(new IncomingRequest(DOCS_ENDPOINT), env, ctx);
// const response2 = await SELF.fetch(new IncomingRequest(DOCS_ENDPOINT));
await waitOnExecutionContext(ctx2);
expect(await response2.text()).toBe(`fresh`);
expect(response2.headers.get("CF-Cache-Status")).toBe('HIT');
console.log('cache status header2:', response2.headers.get("CF-Cache-Status"));
});
it('responds with cached response in second request', async () => {
const ctx = createExecutionContext();
const response = await worker.fetch(new IncomingRequest(DOCS_ENDPOINT), env, ctx);
// const response = await SELF.fetch(new IncomingRequest(DOCS_ENDPOINT));
await waitOnExecutionContext(ctx);
expect(await response.text()).toBe(`fresh`);
expect(response.headers.get("CF-Cache-Status")).toBe(null);
console.log('cache status header:', response.headers.get("CF-Cache-Status"));

const ctx2 = createExecutionContext();
const response2 = await worker.fetch(new IncomingRequest(DOCS_ENDPOINT), env, ctx);
// const response2 = await SELF.fetch(new IncomingRequest(DOCS_ENDPOINT));
await waitOnExecutionContext(ctx2);
expect(await response2.text()).toBe(`fresh`);
expect(response2.headers.get("CF-Cache-Status")).toBe('HIT');
console.log('cache status header2:', response2.headers.get("CF-Cache-Status"));
});
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
ok, thank you very much @James!
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
Another thing then, where can I find the "best practices" currently for testing workers?
21 replies
CDCloudflare Developers
Created by peterz on 12/29/2024 in #workers-help
Vitest and cache
Thank you @James, there are indeed some differences in your example, I'll take a closer look. However, putting aside if I may the request part, only the cache is failing. This fails on the expect(cachedResponse2).toBeDefined(); line.
test.only('test cache only', async () => {
const cache = await caches.default;
const cachedResponse1 = new Response('cached', { status: 200 });
const cacheKey = 'https://example.com/foo';
await cache.put(cacheKey, cachedResponse1);

const cachedResponse2 = await cache.match(cacheKey);
expect(cachedResponse2).toBeDefined();
expect(cachedResponse2.status).toBe(200);
expect(await cachedResponse2.text()).toBe('cached');
});
test.only('test cache only', async () => {
const cache = await caches.default;
const cachedResponse1 = new Response('cached', { status: 200 });
const cacheKey = 'https://example.com/foo';
await cache.put(cacheKey, cachedResponse1);

const cachedResponse2 = await cache.match(cacheKey);
expect(cachedResponse2).toBeDefined();
expect(cachedResponse2.status).toBe(200);
expect(await cachedResponse2.text()).toBe('cached');
});
21 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
Ok, got it, thanks a lot for clarifying 👌
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
Ok. So, for this scenario, I want to cache my responses, but I also want to be able to purge the cache at any time. I've seen that re-deploying doesn't purge the cache. How can I do this?
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
But I don't have any of that configured. I just created a Pages app. If my app responds with cache headers, CF caches it and responds with cache version (I can see both the cache status HIT and if I change content on the CMS the response doesn't change). If my app doesn't respond with cache headers, CF doesn't cache at all (I can confirm it by making changes on the CMS).
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
I guess yes... if it's caching there when I send the cache headers
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
If I don't send the cache headers, CF doesn't cache at all. How can I purge cache?
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
ok, thanks. I'm gonna make a few more tests 👍
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
hmm... does it mean CF doesn't return the cf-cache-status: HIT and Cache-Control: ... headers in that case, but still responds with a cached version? Then the browser doesn't cache anything too...
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
But when I wasn't sending those headers, it wasn't caching at all 😕
19 replies
CDCloudflare Developers
Created by peterz on 4/10/2023 in #pages-help
Cache Purge
I'm not, I don't have a custom domain (yet). I was expecting that too, but it isn't... May it be related to the fact that I'm sending cache headers on the responses?
19 replies