DioxusLabs/dioxus · error

this code should be running in a web context

Error message

this code should be running in a web context

What it means

The playground devtools code called web_sys::window() in a context where no global window exists — i.e. the code is not running in a browser/web environment (or runs in a non-window context such as a worker without DOM).

Source

Thrown at packages/web/src/devtools.rs:289

            // Remove the old force reload param
            query_params.retain(|param| !param.starts_with("dx_force_reload="));
            // Add the new force reload param
            let force_reload = format!("dx_force_reload={noise}");
            query_params.push(&force_reload);

            // Rejoin the query
            let query = query_params.join("&");

            _ = link.set_attribute("href", &format!("{url}?{query}"));
        }
    }
}

/// Initialize required devtools for dioxus-playground.
///
/// This listens for window message events from other Windows (such as window.top when this is running in an iframe).
fn playground(tx: UnboundedSender<HotReloadMsg>) {
    let window = web_sys::window().expect("this code should be running in a web context");

    let binding = Closure::<dyn FnMut(MessageEvent)>::new(move |e: MessageEvent| {
        let Ok(text) = e.data().dyn_into::<JsString>() else {
            return;
        };
        let string: String = text.into();
        let Ok(hr_msg) = serde_json::from_str::<HotReloadMsg>(&string) else {
            return;
        };
        _ = tx.unbounded_send(hr_msg);
    });

    let callback = binding.as_ref().unchecked_ref();
    window
        .add_event_listener_with_callback("message", callback)
        .expect("event listener should be added successfully");

    binding.forget();

View on GitHub (pinned to 24f6a829df)

Solutions

  1. Only call this devtools code in a web (WASM) context; guard it with cfg(target_arch = "wasm32") or platform checks.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/web/src/devtools.rs:289 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of DioxusLabs/dioxus@24f6a829df (2026-08-23). Data as JSON: /api/errors/1e1f00971ec3b85a. Report an issue: GitHub.