Repository: next.js
Issue: Cache control header is missing in response
Labels: bug

### Link to the code that reproduces this issue

https://github.com/zeckaissue/nextjs-missing-cache-control

### To Reproduce

I created this repository as a working base to reproduce this bug locally. Unfortunately, I haven't been able to reproduce the bug locally so far.

Because this bug is very random, it’s difficult for me to find the right way to reproduce it. I’m a bit lost on how to debug this issue...

### Current vs. Expected behavior

Sometimes, mainly just after a deployment, the `cache-control` header is not included in the response.


<details>
<summary>Here is two response header of the same page:</summary>

#### With the bug (Missing cache-control value)
![image](https://github.com/user-attachments/assets/efeae13c-e398-4029-9eda-bc9d831e379e)
#### Without the bug
![image](https://github.com/user-attachments/assets/30a97676-b0a7-4319-83a3-c0970814d078)

</details>

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.17.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 9.5.0
Relevant Packages:
  next: 14.2.16 // An outdated version detected (latest is 15.0.3), upgrade is highly recommended!
  eslint-config-next: 14.2.16
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.5.3
Next.js Config:
  output: standalone
```


### Which area(s) are affected? (Select all that apply)

Not sure

### Which stage(s) are affected? (Select all that apply)

Other (Deployed)

### Additional context

- I’m working on an App Router.
- I have a custom Redis cache handler.
- My app is deployed on Azure App Service and is behind Azure Front Door.

### Why it's an issue for me ?
When the response doesn’t include a `Cache-Control` header, Azure Front Door keeps the response cached for an indefinite duration. As a result, even if the content changes, the page is not refreshed.

### Workaround used
Before [v14.2.10](https://github.com/vercel/next.js/releases/tag/v14.2.10) I used a workaround by setting a custom response header through `next.config.js` as a fallback. With this configuration, when the bug occurred, instead of omitting the `Cache-Control `header, Next.js would return the fallback value (in this case, max-age=60).

**Before [v14.2.10](https://github.com/vercel/next.js/releases/tag/v14.2.10) , the behavior was as follows:**
If the page had its own`revalidate` value, the `Cache-Control` header was taken from this revalidate value.
Otherwise, the value was taken from the Next.js configuration (next.config.js).
So the next.js
**So, the header configuration in `next.config.js` was not prioritized over the `revalidate` value defined individually for each route.**

But this behaviour seems to be consider as a bug from nextjs team (https://github.com/vercel/next.js/issues/22319) (#69802)
So after this [pr on v14.2.10](https://github.com/vercel/next.js/pull/69802) my workaround was not working anymore because all page get the same Cache-Control header.

Here the next config workaround
```js
const nextConfig = {
    headers: async () => {
      return [
        {
          source: '/(.*)',
          headers: [
            {
              key: 'Cache-Control',
              value: 'public, s-maxage=60, stale-while-revalidate=60',
            },
          ],
        },
      ];
    },
  }
```
As a potential workaround, I am looking for a way to modify the response just before it is sent to the client.
This would allow me to add a Cache-Control header if one is not already present.
I have opened a discussion about this: https://github.com/vercel/next.js/discussions/72240


Has anyone else encountered the same issue?

Do you have any suggestions to help me debug it better and find potential solutions?

Maybe related to:
- https://github.com/vercel/next.js/issues/70213
- https://github.com/vercel/next.js/issues/22319