How to do redirect response caching in edge function

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;
        }
    }
}

@hrishikesh Tagging you since I have not received any response from support for the past 1 day.

@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.

Thanks for your response.

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.

Added the same as you specified but still facing the issue.

 headers: {
                    'Cache-Control': 'public, max-age=86400', // Set the desired cache duration,
                    'cache': 'manual',
                    'Location': currentUrl.currentSlug,
                },

Hi, @Ramji_R.

The documentation shows this being added to the constant named config:

export const config: Config = {
  cache: "manual",
  path: "/hello"
}

Above, it is being added to headers. Would you please try adding to the config constant instead?