Skip to content

Conversation

@PascalThuet
Copy link

Summary

This PR fixes the ReferenceError: window is not defined error that occurs when importing dash.js in SSR (Server-Side Rendering) environments like Next.js.

Problem

When using dash.js in a Next.js project with a simple import:

import * as DASH from 'dashjs'

The following error is thrown during SSR:

ReferenceError: window is not defined
    at .../node_modules/dashjs/dist/modern/esm/dash.all.min.js

This happens because MediaPlayerFactory.js executes code at module-level that accesses window APIs (addEventListener, setInterval, etc.) without checking if running in a browser environment.

Solution

Wrap all the auto-creation logic in MediaPlayerFactory.js inside a browser environment check:

if (typeof window !== 'undefined' && window) {
    // auto-creation logic that uses window APIs
}

This is a minimal, non-breaking change that:

  • ✅ Allows dash.js to be imported in SSR environments without errors
  • ✅ Preserves the exact same behavior in browser environments
  • ✅ Follows the standard approach used by most browser-only libraries
  • ✅ Requires no changes from library users

Test plan

  • All 3238 existing unit tests pass
  • ESLint passes
  • Manual test: import dash.js in a Next.js project with SSR

Related issue

Fixes #4728

…ed" error

Wrap the auto-creation logic in a browser environment check to allow
dash.js to be imported in SSR environments like Next.js without throwing
a "window is not defined" error.

The module-level code that accesses window APIs (addEventListener,
setInterval, etc.) is now only executed when running in a browser context.

Fixes Dash-Industry-Forum#4728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

SSR error window is not defined

2 participants