caches.default.match() always returns undefined. even after caches.default.put() is executed.

export async function FITThetaTauEvents(ctx: ExecutionContext) {
    const events_list = await (async () => {
        const events_discord_url = `${RouteBases.api}${Routes.guildScheduledEvents("12345678901234")}?with_user_count=true`;

        //check for cache
        const cache_res = await caches.default.match(events_discord_url);
        if (exists(cache_res)) { //always returns udefined here some how <<<<<<<<<<<<<<<-----
            const now = Date.now();
            const cached_time = (new Date(cache_res.headers.get('X-Cached-At')!)).getTime();

            if ((now - cached_time) <= 300000)
            return await cache_res.clone().json() as RESTGetAPIGuildScheduledEventsResult;
        }

        //else fetch
        const proto_res = await fetch(
            events_discord_url,
            {
                body: null,
                method: 'GET',
                headers: {
                    'Authorization': `Bot ${FITThetaTauRESTBot_credentials.token}`,
                    'Content-Type': `application/json`
                }
            }
        );
    
        if (!proto_res.ok) return null;
    
        if (exists(proto_res.clone().body)) {
            const cache_header = new Headers(proto_res.headers);
            cache_header.append('X-Cached-At', new Date().toUTCString());

            ctx.waitUntil(caches.default.put(
                events_discord_url, 
                new Response(proto_res.clone().body, {
                    headers: cache_header,
                    status: proto_res.status,
                    statusText: proto_res.statusText
                })
            ))

            return await proto_res.json() as RESTGetAPIGuildScheduledEventsResult|undefined;
        } else {
            return null;
        }
    })();
Was this page helpful?