Skip to content
Source
Sign inSign up
TanStack/query

hashKey function throws error with circular references #10814

Closedsulthonzh(GitHub profile, opens in a new tab) opened this issue 1 comment

Bug Report

Description

The hashKey function in @tanstack/query-core throws errors when processing objects or arrays with circular references.

Reproduction Steps

  1. Create an object with a circular reference: const obj = { id: 1 }; obj.self = obj;
  2. Pass it to hashKey(obj)
  3. The function throws a "Maximum call stack size exceeded" error

Expected Behavior

The function should handle circular references gracefully without throwing an error.

Actual Behavior

The function throws:

  • RangeError: Maximum call stack size exceeded for objects with circular references
  • TypeError: Converting circular structure to JSON for arrays with circular references

Code to Reproduce

import { hashKey } from "@tanstack/query-core";

// Object with circular reference
const circularObject = { id: 1, name: "test" };
circularObject.self = circularObject;
hashKey(circularObject); // Throws RangeError

// Array with circular reference
const circularArray = [1, 2, 3];
circularArray.push(circularArray);
hashKey(circularArray); // Throws TypeError

Root Cause

The hashKey function uses JSON.stringify with a replacer function, but doesn't handle circular references, which causes JSON.stringify to fail.

Possible Solution

Implement a circular reference detection mechanism in the replacer function to break circular references.

Environment

  • TanStack Query: 5.100.14
  • Node.js: 20.5.1
  • Browser: N/A
  1. it is documented that Query Keys need to be serializable using JSON.stringify:

    https://tanstack.com/query/v5/docs/framework/react/guides/query-keys

    You can provide your own queryKeyHashFn if you want to opt out of the default behaviour.

  2. TkDodo closed this

  3. TkDodo added labelsworks as designed

Sign in to join this conversation.