In the edge function, we are validating whether the provided request exists in the CMS. If it is present, we perform a redirect. Now, we want to implement caching for this redirect. We are looking for guidance on how to specifically cache redirects within the edge function. It’s important to note that once a redirect is established, it will remain unchanged. How can we achieve caching for these redirects in the edge function?
edge-function.ts
const url = new URL(req.url);
const data = await (post(redirectItems(url.pathname)))
if (data && data.data && data.data.redirectManagerCollection && data.data.redirectManagerCollection.items.length > 0) {
const redirectItem = data?.data?.redirectManagerCollection.items;
var currentUrl = redirectItem.find(x => x.redirectSlug == url.pathname);
if (currentUrl) {
return Response.redirect(currentUrl.currentSlug,301);
}
}
I also tried this but still no luck. I’m testing like deploy the code, check whether the redirect is working, Now go to cms and revoke access token. So now again check the same url which i tested above to see whether i get the redirected version in new incognito browser but it doesn’t perform the redirect
export default async (req: Request, context: Context) => {
// APPROACH - 2
const url = new URL(req.url);
const data = await (post(redirectItems(url.pathname)))
if (data && data.data && data.data.redirectManagerCollection && data.data.redirectManagerCollection.items.length > 0) {
const redirectItem = data?.data?.redirectManagerCollection.items;
var currentUrl = redirectItem.find(x => x.redirectSlug == url.pathname);
if (currentUrl) {
// return Response.redirect(currentUrl.currentSlug,301);
const response = new Response('', {
status: 301,
headers: {
'Cache-Control': 'public, max-age=86400', // Set the desired cache duration
'Location': currentUrl.currentSlug,
},
});
return response;
}
}
}
@Ramji_R, for context, forums are not bound by the Enterprise Support SLA. If you need a guaranteed response time as in your contract, you should reach out to Netlify Support helpdesk.
As to answer your question, the safest way to ensure the data is persistent, is by using Netlify Blobs: Netlify Blobs | Netlify Docs. You can store the redirects as JSON and match them for any new requests.
The only worry my team has is reading the whole json file. We have 18K redirects and it increases our load time by 1second which we can’t afford. I believe it will parse all the 18K and will do the redirect even though we cached the json file using blobs.
So we planned to cache the redirect response in netlify cdn via edge function. So in the edge the redirect will be cached so the other users can use it.
Is there any way to avoid using the json file and cache the redirect in cdn. @hrishikesh
Interesting. Yeah, I see how this could be an issue. You can try using caching, but it is specific to a CDN node + cleared after each deploy, so I could imagine you running to some levels of performance issues with that too. However, I noticed that you haven’t added the cache: 'manual' config to the Edge Function: Optional configuration for edge functions | Netlify Docs, so it’s probably not caching as you’d expect it to. Let us know if that improves the situation, but if not, we can ask the devs for any potential solutions here.