[PR #339] [MERGED] fix: unstable_cache undefined cache #492

Closed
opened 2026-05-06 13:08:22 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/339
Author: @JaredStowell
Created: 3/8/2026
Status: Merged
Merged: 3/8/2026
Merged by: @james-elicx

Base: mainHead: jstowell/fix-undefined-caching


📝 Commits (1)

  • a77cbf4 Fix unstable_cache undefined cache

📊 Changes

2 files changed (+38 additions, -2 deletions)

View changed files

📝 packages/vinext/src/shims/cache.ts (+17 -2)
📝 tests/shims.test.ts (+21 -0)

📄 Description

Summary

Fix unstable_cache() so cached functions that return top-level undefined actually hit the cache on subsequent calls.

Problem

unstable_cache() was serializing results with JSON.stringify(result) and reading them back with JSON.parse(body).

For a top-level undefined return value, JSON.stringify(undefined) returns undefined rather than a JSON string. On the next read, JSON.parse(undefined) throws, so the shim treated the entry as invalid and recomputed the function every time.

Changes

  • Add a dedicated sentinel payload for top-level undefined results in the unstable_cache() serialization path
  • Teach the read path to recognize that sentinel before falling back to JSON.parse
  • Add a regression test covering an unstable_cache()-wrapped function that returns undefined and should only execute once across repeated calls

Why

This is a real correctness/performance bug in the cache shim: callers still receive undefined, but the cache never produces a true hit for that value.

Testing

  • pnpm test tests/shims.test.ts -t "unstable_cache"

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/339 **Author:** [@JaredStowell](https://github.com/JaredStowell) **Created:** 3/8/2026 **Status:** ✅ Merged **Merged:** 3/8/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `jstowell/fix-undefined-caching` --- ### 📝 Commits (1) - [`a77cbf4`](https://github.com/cloudflare/vinext/commit/a77cbf4a3c3ac0905b25cb78f046b32ca7c79f84) Fix unstable_cache undefined cache ### 📊 Changes **2 files changed** (+38 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/shims/cache.ts` (+17 -2) 📝 `tests/shims.test.ts` (+21 -0) </details> ### 📄 Description **Summary** Fix `unstable_cache()` so cached functions that return top-level `undefined` actually hit the cache on subsequent calls. **Problem** `unstable_cache()` was serializing results with `JSON.stringify(result)` and reading them back with `JSON.parse(body)`. For a top-level `undefined` return value, `JSON.stringify(undefined)` returns `undefined` rather than a JSON string. On the next read, `JSON.parse(undefined)` throws, so the shim treated the entry as invalid and recomputed the function every time. **Changes** - Add a dedicated sentinel payload for top-level `undefined` results in the `unstable_cache()` serialization path - Teach the read path to recognize that sentinel before falling back to `JSON.parse` - Add a regression test covering an `unstable_cache()`-wrapped function that returns `undefined` and should only execute once across repeated calls **Why** This is a real correctness/performance bug in the cache shim: callers still receive `undefined`, but the cache never produces a true hit for that value. **Testing** - `pnpm test tests/shims.test.ts -t "unstable_cache"` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:22 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#492
No description provided.