Author: aegisSoft

Recovering PlayerAPI from React Fiber After Spotify 1.3.8

How ReverseSpotify recovered the live PlayerAPI instance via __reactFiber$ after Spotify 1.3.8 removed global bridges, normalized {current, queued, nextUp} payloads, and resolved image hashes.

#reverse-engineering#react-fiber#spotify#javascript#debugging

Recovering PlayerAPI from React Fiber After Spotify 1.3.8

When Spotify's desktop client moved to rspack in version 1.3.8, the window.Platform and Spicetify globals that mods used to rely on disappeared. Inside ReverseSpotify, the queue tab and cover art lookups started returning empty results overnight.

Scraping the DOM was the obvious fallback, and it works for reading the currently playing track title. It falls apart as soon as you need the actual queued and nextUp lists, paused session metadata, or reliable queue skipping and removal.

Where PlayerAPI still lives

Spotify still renders its desktop UI with React. Even when the global object is no longer exposed on window, the live PlayerAPI instance still gets passed down the component tree inside memoizedProps.

In loader/reverse-loader.js, I added a scanner that walks up from any mounted DOM node's __reactFiber$ pointer:

  1. Query div, button, li, [data-testid] and grab the first element that owns a __reactFiber$ property.
  2. Walk fiber.return upward, capped at 400 hops.
  3. Inspect fiber.memoizedProps on each node for an object where typeof v.getQueue === "function" && v._queue.
  4. Cache the result per window in a WeakMap (PLAYER_API_CACHE) so repeated checks do not re-walk the tree.
const PLAYER_API_CACHE = new WeakMap();

function scanPlayerApi(win) {
  const doc = win && win.document;
  if (!doc || typeof doc.querySelectorAll !== "function") return null;
  const els = doc.querySelectorAll("div,button,li,[data-testid]");
  let fiber = null;
  for (const el of els || []) {
    const key = Object.keys(el).find((k) => k.indexOf("__reactFiber$") === 0);
    if (key) {
      fiber = el[key];
      break;
    }
  }
  for (let depth = 0; fiber && depth < 400; depth++) {
    const props = fiber.memoizedProps;
    if (props && typeof props === "object") {
      for (const k of Object.keys(props)) {
        const v = props[k];
        if (v && typeof v === "object" && typeof v.getQueue === "function" && v._queue) {
          return v;
        }
      }
    }
    fiber = fiber.return;
  }
  return null;
}

The 400-hop limit prevents runaway traversal on detached subtrees, and the WeakMap keeps the lookup at one scan per window lifetime.

Shape and signature mismatches

Once the real PlayerAPI came back, three smaller mismatches showed up right behind it:

  • Queue payload shape: Older bridges expected nextTracks, whereas PlayerAPI.getQueue() returns { current, queued, nextUp }. Merging queued followed by nextUp keeps user-queued tracks ahead of context tracks.
  • removeFromQueue signature: The older API took (uri, index), while the 1.3.8 instance expects an array of items ([{ uri }]). Checking qApi._queue lets the bridge call the right signature without breaking older builds.
  • Image URIs: Track metadata frequently returns spotify:image:<40-hex-chars> or a bare 40-char hash instead of an HTTPS URL. normalizeImageUrl() maps both to https://i.scdn.co/image/<hash>.

Fixing the test blind spot

Right before this change, I also caught a testing bug: parts of the suite were importing duplicated core helper files instead of testing loader/reverse-loader.js directly. Commit d866097 removed those duplicate modules so the test runner executes against the real loader, and the fiber fix landed with 159 JS tests covering queue normalization, double-click guards, and cover resolution.

Walking React fiber internals is inherently a heuristic, not a stable API contract, and a future Spotify client update could rename _queue or restructure props. That is why createPlayerBridge keeps the fallback order Spicetify.Player -> Platform.PlayerAPI -> fiber PlayerAPI -> DOM button clicks, degrading gracefully if one layer breaks.

Command Palette

Search for a command to run...