Skip to content

fix: sync query_params on browser back/forward navigation#9490

Open
kimjune01 wants to merge 3 commits into
marimo-team:mainfrom
kimjune01:fix/query-params-browser-navigation
Open

fix: sync query_params on browser back/forward navigation#9490
kimjune01 wants to merge 3 commits into
marimo-team:mainfrom
kimjune01:fix/query-params-browser-navigation

Conversation

@kimjune01
Copy link
Copy Markdown

Fixes #4153

When users navigate with browser back/forward buttons, the URL changes but mo.query_params doesn't update, leaving cells stale. The frontend tracks URL changes via pushState when cells update query params, but the reverse path (browser navigation → kernel update) was missing.

Fix

Add a popstate listener in useMarimoKernelConnection that parses the new query params and sends them to the kernel via a new update_query_params endpoint. The kernel clears and updates its query_params state, then re-runs dependent cells.

Backend wiring follows the same pattern as set_ui_element_value@requires("read") permission, command dispatched through the kernel's control request handler.

Fixes marimo-team#4153. When users navigate using browser back/forward buttons,
query_params now updates and dependent cells re-execute.

Changes:
- Add UpdateQueryParamsCommand to sync URL state with kernel
- Add popstate listener to detect browser navigation
- Add /api/kernel/update_query_params endpoint
- Update QueryParams state triggers cell re-execution via State system

The fix leverages marimo's existing State system: when the browser URL
changes via popstate, the frontend sends the new params to the kernel,
which updates the QueryParams state object, triggering re-execution of
cells that depend on query_params.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 10, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment May 12, 2026 3:11am

Request Review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 10, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 11 files

Architecture diagram
sequenceDiagram
    participant Browser as Browser (User)
    participant FE as Frontend Runtime (useMarimoKernelConnection)
    participant Router as URL Router (pushState/popstate)
    participant RequestClient as Request Client (sendUpdateQueryParams)
    participant API as API Server (/api/kernel/update_query_params)
    participant Kernel as Kernel Runtime (control handler)
    participant QueryParams as QueryParams State
    participant Scheduler as Cell Scheduler

    Note over Browser,Scheduler: Browser Back/Forward Navigation → Query Param Sync

    Browser->>Router: browser back/forward click
    Router->>Router: URL changes (popstate event)
    Router->>FE: popstate event fires
    FE->>FE: parseQueryParams() from window.location.href
    FE->>RequestClient: sendUpdateQueryParams({ queryParams: {...} })
    
    alt Static/Wasm (Islands or Pyodide)
        RequestClient->>RequestClient: putControlRequest(type: "update-query-params")
    else Server mode
        RequestClient->>API: POST /api/kernel/update_query_params
        API->>API: @requires("read") permission check
        API->>Kernel: dispatchControlRequest(UpdateQueryParamsRequest)
    end

    Kernel->>Kernel: handle_update_query_params()
    Kernel->>QueryParams: clear() + update(request.query_params)
    QueryParams->>QueryParams: _set_value() triggers state change
    QueryParams->>Scheduler: state update notification
    Scheduler->>Scheduler: _run_cells(set()) re-executes dependent cells
    Scheduler->>Kernel: broadcastNotification(CompletedRunNotification)
    Kernel-->>API: SuccessResponse
    API-->>RequestClient: 200 OK
    RequestClient-->>FE: null (void)
    FE-->>Browser: UI updates with new cell values

    Note over Browser,Kernel: Same pattern as set_ui_element_value
Loading

@kimjune01
Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

… wiring

The new update_query_params endpoint was missing from the OpenAPI schema
generation (MODELS list) and the generated TypeScript types. Also missing
from the frontend mock, lazy-request proxy, and toasting wrapper—causing
TypeScript build failures.
@github-actions github-actions Bot added the bash-focus Area to focus on during release bug bash label May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bash-focus Area to focus on during release bug bash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Notebooks using query_params do not update on browser navigation changes

1 participant