Skip to content

ReferenceError: window is not defined in @tanstack/router-devtools-core during SSR — delegateEvents() called at module scope #7205

@arelia

Description

@arelia

Which project does this relate to?

Router

Describe the bug

When using @tanstack/react-router-devtools in a TanStack Start SSR application, the server crashes with:

ReferenceError: window is not defined
    at delegateEvents (server-_libs/@tanstack/react-router-devtools-C28X7FL_.js:1255:48)
    at loadEntries (server-ssr-ParBoa2f.js:658:52)
    at startRequestResolver (server-ssr-ParBoa2f.js:848:20)
    at Object.fetch (server-ssr-ParBoa2f.js:1028:10)

The root cause is in @tanstack/router-devtools-core. The BaseTanStackRouterDevtoolsPanel module contains top-level (module scope) calls to delegateEvents():

// BaseTanStackRouterDevtoolsPanel-BbL4h7yi.js, line 1410
delegateEvents(["click"]);

// line 1475
delegateEvents(["click"]);

// line 2169
delegateEvents(["click", "mousedown"]);

The delegateEvents function (from the bundled SolidJS runtime in context-D56_tqst.js) unconditionally accesses window.document:

function delegateEvents(eventNames, document = window.document) {
    // ...
}

When the bundler (rolldown/nitro) includes @tanstack/react-router-devtools in the SSR bundle, these calls execute during module initialization on the server, where window is not defined.

Your Example Website or App

https://github.com/arelia/repro-trdevtools

Steps to Reproduce the Bug or Issue

  1. Initialize a new directory
npm init -y
npm install @tanstack/router-devtools-core
  1. Create repro.mjs with this single line
import "./node_modules/@tanstack/router-devtools-core/dist/BaseTanStackRouterDevtoolsPanel-BbL4h7yi.js";
  1. Run with
node repro.mjs
  1. Observe crash
file:///…/node_modules/@tanstack/router-devtools-core/dist/context-D56_tqst.js:1380
function delegateEvents(eventNames, document = window.document) {
                                               ^
ReferenceError: window is not defined

Expected behavior

The devtools module should be safe to import in a server environment. Either:

  1. The delegateEvents() calls should be guarded with typeof window !== 'undefined' / isServer check, so they're no-ops on the server.
  2. The SolidJS delegateEvents function itself should handle server environments by checking for window before accessing window.document in its default parameter.
  3. The devtools package should provide a separate server-safe entry point (e.g., via exports conditions in package.json) that stubs out DOM-dependent code.

Screenshots or Videos

No response

Platform

  • @tanstack/react-router-devtools: 1.166.13
  • @tanstack/router-devtools-core: 1.167.3
  • @tanstack/react-start: 1.167.40
  • @tanstack/react-router: 1.168.22
  • Node.js: v25.8.2
  • Vite: 8.0.0-beta.14
  • Nitro: node-server preset

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions