#4342·swr

Focus events emit TimeoutNaNWarning with Node.js timers in jsdom

Author: taroj1205Created Sep 28, 2026Updated Sep 28, 2026

Bug report

Description / Observed Behavior

With Node.js 24.18.0 and jsdom, SWR 2.5.1 emits TimeoutNaNWarning when a focus or visibilitychange event reaches its default cache listeners:

TimeoutNaNWarning: NaN is not a number.
Timeout duration was set to 1.

Revalidation still completes asynchronously. I have not established a production-browser malfunction. The default online listener does not produce this warning in the same reproduction.

Expected Behavior

Focus and visibility events should schedule asynchronous revalidation without passing the DOM event as the timer delay or emitting a Node timer warning.

Repro Steps / Code Example

In an empty directory:

bash
npm init -y
npm install [email protected] [email protected] [email protected]

Save this as repro.cjs:

javascript
const assert = require('node:assert/strict');
const { setTimeout: wait } = require('node:timers/promises');
const { JSDOM } = require('jsdom');

const main = async () => {
  const dom = new JSDOM('<!doctype html><html><body></body></html>');
  globalThis.window = dom.window;
  globalThis.document = dom.window.document;

  const { initCache, SWRGlobalState, revalidateEvents } = require('swr/_internal');
  const cache = new Map();
  const [, , , dispose] = initCache(cache);
  const events = [];
  SWRGlobalState.get(cache)[0].key = [(event) => events.push(event)];
  const type = process.argv[2] || 'focus';

  try {
    const target = type === 'visibilitychange' ? document : window;
    target.dispatchEvent(new dom.window.Event(type));
    assert.deepEqual(events, []);
    await wait(20);
    assert.deepEqual(events, [type === 'online'
      ? revalidateEvents.RECONNECT_EVENT
      : revalidateEvents.FOCUS_EVENT]);
    console.log(`${type}: one asynchronous revalidation completed`);
  } finally {
    dispose();
    dom.window.close();
  }
};

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Run each event in a fresh process:

bash
node repro.cjs focus
node repro.cjs visibilitychange
node repro.cjs online

All three exit with status 0 and log one asynchronous revalidation. Only focus and visibilitychange emit TimeoutNaNWarning. This uses the internal cache API to observe the scheduled callbacks without a React renderer.

Additional Context

Verified on macOS arm64, Node.js 24.18.0, SWR 2.5.1, React 19.3.0, and jsdom 30.0.1. The reproduction dependencies were installed with pnpm 11.26.0. The same warning was also observed with SWR 2.5.0.

The default initFocus registers its callback directly with addEventListener, so the callback receives the DOM event. Cache initialization passes setTimeout.bind(undefined, revalidateCallback) without binding a delay. The event therefore becomes the delay, converts to NaN, and Node clamps it to 1 ms with a warning. In contrast, the default initReconnect calls callback() without forwarding the online event.

Binding an explicit delay in the cache's focus scheduling callback appears to avoid this conversion while retaining asynchronous revalidation. I am reporting the upstream behavior rather than suppressing warnings or globally replacing timers in the test environment.