Shivasai
Shivasai
CDCloudflare Developers
Created by Shivasai on 10/19/2024 in #workers-help
Need the confimation about my code
import { generateJWT } from '../utils/jwt'; export const getComponentById = async (request: Request, env: Env) => { const queryParams = new URL(request.url).searchParams; const componentId = queryParams.get('id'); if (!componentId) { return new Response(JSON.stringify({ error: 'Component ID is required' }), { status: 400, headers: { 'content-type': 'application/json' }, }); } const cacheUrl = new URL(request.url); const url = env.GET_COMPONENT_BY_ID_GCP_URL; const cache = caches.default; const cacheKey = new Request(cacheUrl.toString(), request); let response = await cache.match(cacheKey); if (!response) { // Fetch from GCP Cloud Function const jwtToken = await generateJWT(env); response = await fetch(${url}?componentId=${componentId}, { headers: { Authorization: Bearer ${jwtToken}, }, }); response = new Response(response.body, response); response.headers.append('Cache-Control', 'max-age=0, s-maxage=31536000'); // s-maxage - In cloudflare edge store it for 1 year response.headers.append('content-type', 'application/javascript'); // TODO: Browser caching and other caching headers and strategies await cache.put(cacheKey, response.clone()); } return response; }; --------- This is my worker core logic This will cache the response in edge for 1 year and It will not cache in browser right? If yes If I hit this url once then untill I purge it I need to get the data from cache right? But this is not happening why? I am in cloudflare free plan is it because of this?
1 replies