neverthrow icon indicating copy to clipboard operation
neverthrow copied to clipboard

Returned Ok in a cached NextJS function is treated as an iterator

Open jsimonrichard opened this issue 11 months ago • 3 comments

I stumbled on a weird bug related to how NextJS treats the return values of cached functions. This not a bug with neverthrow, per se, but I wanted to get it on your radar.

NextJS's cache feature does not support functions that return classes. However, when Ok (or Err) is returned from a cached function, NextJS doesn't throw an error because it's an iterator. Instead, it tries to iterate over the class in an attempt to serialize it. For the Ok class, this means that the cached function then returns an empty array (very confusing for someone not familiar with neverthrow).

If you had a "gotcha's" section (or were considering adding one), I might suggesting adding a note about this because of how popular NextJS is and how likely devs might be to try caching (or serializing, in general) a Result object.

Or maybe this isn't as common as it seems... I don't know. I'm relatively new to NextJS, and I just started trying out neverthrow this afternoon.

jsimonrichard avatar May 18 '25 00:05 jsimonrichard

I also just ran into this attempting to use neverthrow in a server action

toppsdown avatar Sep 25 '25 14:09 toppsdown

Does next not have some sort of lint rule to flag this sort of gotcha?

supermacro avatar Oct 07 '25 03:10 supermacro

Not that I'm aware of.

jsimonrichard avatar Oct 08 '25 18:10 jsimonrichard