# Netlify Functions Response Caching for Redirects

**URL:** <https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131>\
**Category:** Support\
**Tags:** lambda-functions\
**Created:** [December 7, 2023, 4:33pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131 "2023-12-07T16:33:40Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![carbonrobot](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/carbonrobot/32/57079_2.png) [@carbonrobot](https://answers.netlify.com/u/carbonrobot)\
**Post date:** [December 7, 2023, 4:33pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/1 "2023-12-07T16:33:40Z")

</div>

I am unable to cache redirect responses from Lambda functions. I have tried the following response, but the Cache-Status is always “Netlify Edge”; fwd=miss"

```auto
  return new Response(undefined, {
    status: 301,
    headers: {
      Location: "the url to redirect to",
      "Netlify-CDN-Cache-Control": "public, s-max-age=31536000, immutable",
      "Cache-Control": "public, max-age=31536000, immutable",
    },
  });

```

Example of a URL that should be cached in the CDN as long as possible

[https://devshapes.com/zz/z9v7PJdmgJgXOBzTImVlz](https://devshapes.com/zz/z9v7PJdmgJgXOBzTImVlz)

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [December 8, 2023, 12:56pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/2 "2023-12-08T12:56:42Z")

</div>

Have you checked: [Cannot get Netlify api endpoint caching to work as expected - Support - Netlify Support Forums](https://answers.netlify.com/t/105763/7)

---

<div class="post-metadata">

**Author:** ![carbonrobot](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/carbonrobot/32/57079_2.png) [@carbonrobot](https://answers.netlify.com/u/carbonrobot)\
**Post date:** [December 8, 2023, 1:13pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/3 "2023-12-08T13:13:58Z")

</div>

Yes, I have read that. Is there anything specific in that thread I should be aware of?

In my test URL, you can see all other network calls correctly cached, only the 301 is uncached. While it could be possible I am hitting a different CDN node every time, its unlikely to be the case when making the request from the same client repeatedly.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [December 10, 2023, 6:35am UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/4 "2023-12-10T06:35:45Z")

</div>

> [@carbonrobot](#):
>
> its unlikely to be the case when making the request from the same client repeatedly.

That’s not true. But if you can send a HAR file recording or the output of your curls, we can check.

---

<div class="post-metadata">

**Author:** ![carbonrobot](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/carbonrobot/32/57079_2.png) [@carbonrobot](https://answers.netlify.com/u/carbonrobot)\
**Post date:** [December 13, 2023, 5:29pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/5 "2023-12-13T17:29:57Z")

</div>

It’s a public link, so you should be able to generate an HAR file from visiting the site at the posted link above. I have also attached an HAR as well.  
[devshapes.com.har](https://answers.netlify.com/uploads/short-url/kQu6zu9g74qPloY4VsD40pp3aIm.har) (152.2 KB)

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [December 15, 2023, 12:41pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/6 "2023-12-15T12:41:59Z")

</div>

Can you try doing the same with Edge Functions and using their caching: [Optional configuration for edge functions | Netlify Docs](https://docs.netlify.com/edge-functions/optional-configuration/#response-caching)

---

<div class="post-metadata">

**Author:** ![carbonrobot](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/carbonrobot/32/57079_2.png) [@carbonrobot](https://answers.netlify.com/u/carbonrobot)\
**Post date:** [December 18, 2023, 6:03pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/7 "2023-12-18T18:03:58Z")

</div>

I’ve been trying to use edge functions, but I get stuck on the experimental support for npm modules.

“There was an error when loading the “postgres” npm module. Support for npm modules in edge functions is an experimental feature. Refer to [Support for npm modules in Edge Functions - Updates - Netlify Support Forums](https://ntl.fyi/edge-functions-npm) for more information.”

I have tried the workarounds in that thread such as Deno import maps, but I still get the same error during build.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [December 21, 2023, 2:29pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/9 "2023-12-21T14:29:23Z")

</div>

Sorry, I totally missed thie before, but the problem might be happening because you’re using `s-max-age` instead of the `s-maxage` standard. Could you try to remove the extra `-`?

---

<div class="post-metadata">

**Author:** ![carbonrobot](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/carbonrobot/32/57079_2.png) [@carbonrobot](https://answers.netlify.com/u/carbonrobot)\
**Post date:** [January 17, 2024, 5:48pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/10 "2024-01-17T17:48:41Z")

</div>

I’m using s-max-age now but I don’t see any difference in the caching.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [January 17, 2024, 8:43pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/11 "2024-01-17T20:43:34Z")

</div>

I think it should be `s-maxage` and not `s-max-age`.

---

<div class="post-metadata">

**Author:** ![carbonrobot](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/carbonrobot/32/57079_2.png) [@carbonrobot](https://answers.netlify.com/u/carbonrobot)\
**Post date:** [January 17, 2024, 9:14pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/13 "2024-01-17T21:14:01Z")

</div>

Sorry, mistyped that last reply. Here is the actual code.

```auto
  return new Response(undefined, {
    status: 301,
    headers: {
      Location: url || domain,
      "Netlify-CDN-Cache-Control": "public, s-maxage=31536000, immutable",
      "Cache-Control": "public, max-age=31536000, immutable",
    },
  });

```

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [January 17, 2024, 9:19pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/15 "2024-01-17T21:19:38Z")

</div>

Alright, I’ve asked the devs about it. Will let you know what they say.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [January 18, 2024, 12:43pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/16 "2024-01-18T12:43:48Z")

</div>

This is a bug and would be fixed, but right now, you should be able to add another header like `'content-length': '0'` and it would fix it.

---

<div class="post-metadata">

**Author:** ![carbonrobot](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/carbonrobot/32/57079_2.png) [@carbonrobot](https://answers.netlify.com/u/carbonrobot)\
**Post date:** [January 18, 2024, 2:36pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/17 "2024-01-18T14:36:36Z")

</div>

That worked perfectly, thanks for the quick reply.

---

<div class="post-metadata">

**Author:** ![SamO](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/samo/32/41847_2.png) [@SamO](https://answers.netlify.com/u/SamO)\
**Post date:** [January 18, 2024, 2:48pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/18 "2024-01-18T14:48:07Z")

</div>

Glad to hear it! 👍🏿

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [January 24, 2024, 1:28pm UTC](https://answers.netlify.com/t/netlify-functions-response-caching-for-redirects/108131/19 "2024-01-24T13:28:29Z")

</div>

The original bug has been resolved and content-length is no longer needed.
