insomnia icon indicating copy to clipboard operation
insomnia copied to clipboard

Fix: Simplify the nunjuck rendering cache mechanism

Open cwangsmv opened this issue 4 months ago • 1 comments

Background

Current use-nunjucks hook which manages the Nunjuck rendering cache has the following issue:

  • Components are required to generate and provide their own unique IDs to store cache entries.
  • When new tags are created in the editor, the hook will create duplicate cache records instead of one.
  • Caches are cleared via setTimeout with a hard-coded interval.

Changes:

  • Components only need to pass the enableCache option to the hook in order to enable caching
  • Replace the global cache variable with a useRef-based cache, ensuring cache state is properly scoped to the hook instance and avoiding unique IDs.
  • Clear the render cache automatically whenever any environment (folder, collection, or global level) changes, preventing stale caches.
  • Add more meaningful inline comments to clarify the cache mechanism

Close INS-1248

cwangsmv avatar Aug 25 '25 09:08 cwangsmv

If we think about this as a memoization problem, we should have finite inputs that have a deterministic output, currently this is modeled with useEffects, and useCallbacks. I argue that if we know the inputs we can use a simple memo funciton and not couple this with react at all. So I'm proposing we wipe out the caching mechanism and replace with a simple functional memoize.

jackkav avatar Oct 15 '25 15:10 jackkav