Next.js Discord

Discord Forum

What is the preferred way for setting the `no-store` header on `404` responses with page router?

Unanswered
Philippine Crocodile posted this in #help-forum
Open in Discord
Philippine CrocodileOP
We have a complex architecture where we run a Next.js client in a Kubernetes orchestration within a cloud provider environment. We have a persistent bug where 404 responses are intermittently being served up by the Next.js server when they shouldn't be. The data for our client comes from a CMS where multiple content creators have access and pages are created, enabled, disabled, and deleted not infrequently.

A load balancer cache sits in front of our client server and is caching 404 responses despite our negative cache policy being configured to not cache 404 responses. Reading through the cloud provider's documentation I found a suggestion to explicitly send responses with the no-store cache header.

What is the preferred approach for adding HTTP status-specific response headers such that Next.js doesn't overwrite them as it is wont to do?

We're running Next v14.2.3.

11 Replies

Philippine CrocodileOP
Adding that I assume the answer will be to add middleware to intercept the 404 response and append the header, but specifically I am curious as to if there are any details to keep in mind while doing so.
Just a heads up, I wouldn't necessarily point the intermittent 404s at Next. We used to use a similar, fairly complex k8s setup, and it would just... decide to throw random 404s from some of our services. Still have no idea why to this day. I have a hunch it was some failure with how we were handling autoscaling.
Sorry if that's unhelpful, just felt the pain when I read your post and remembered when I was having similar issues on a similar setup
Philippine CrocodileOP
Just a heads up, I wouldn't necessarily point the intermittent 404s at Next.
For sure, I'm just at a point where I'm just making small changes to various points of the stack to see if there are improvements.
We did, it was awesome for what it was, but the juice wasn't worth the squeeze. We're a fairly small team of about 12, and our heaviest traffic is about 500rps on our Go API / Next frontend at peak hours, so nothing too insane
Well, I'm half lying here
We did and we didn't, we use Knative now 😂
It's been a much more straightforward setup though
Philippine CrocodileOP
Ah interesting, I'll check out Knative and see if it will work better for us
Philippine CrocodileOP
For posterity regarding the question in my post:

Adding revalidate: 10 to the return object within getStaticProps() when no page data is found adds the no-store and redundant must-revalidate headers to the response.

return {
  notFound: true,
  revalidate: 10
};