Troubleshooting
Common Issues
Cache Not Updating (Zombies)
If you find that your cache isn't updating after a mutation, check the following:
- Epoch Mismatch: Ensure you are invalidating the correct route pattern.
- Success Status: Invalidation only triggers on 2xx responses. If your POST/PUT fails, the cache won't be invalidated.
- Race Conditions: SWR can sometimes serve stale data if a refresh is still pending.
High Memory Usage
If your Node.js process is consuming too much memory:
- Reduce
maxBodySize: Prevent large responses from being buffered. - Shorten
gcTime: Evict stale data more aggressively. - Use Redis: Offload the cache storage from your Node.js heap to an external Redis instance.
Unexpected MISS on GET requests
- Vary Headers: If you use
vary, any change in those headers will cause a MISS. - Query Params: If
sortQueryis false,?a=1&b=2and?b=2&a=1are treated as different routes.
Debugging
You can inspect the X-Cache header in your browser's network tab or via curl:
bash
curl -I http://localhost:3000/api/dataLook for:
X-Cache: HIT(Serving from cache)X-Cache: MISS(Fetched from source)X-Cache: STALE(Serving stale, refreshing in background)