Skip to content

ui.navigate.to not triggering sub_pages_navigate event #5487

@GinjiJizai

Description

@GinjiJizai

First Check

  • I added a very descriptive title here.
  • This is not a Q&A. I am sure something is wrong with NiceGUI or its documentation.
  • I used the GitHub search to find a similar issue and came up empty.

Example Code

from nicegui import ui


def root():
    ui.on('sub_pages_navigate', lambda event: ui.notify(f'Navigating to 2 {event.args}'))

    ui.label('main page')
    ui.separator()
    ui.sub_pages({'/': main, '/other': other})

def main():
    ui.label('Main page content')
    ui.link('Go to other page', '/other')
    ui.button('Click me', on_click=lambda: ui.navigate.to('/other'))  

def other():
    ui.label('Another page content')
    ui.link('Go to main page', '/')

ui.run(root, dark=True)

Description

I'm using sub-pages to create a single-page application and I want to execute some code when the URL changes.

After researching the source code of the sub-pages module, I found that the sub_pages_navigate event is fired on sub-page navigation.

When I click a ui.link, the event is fired correctly and my notification is shown.
However, when I use ui.navigate.to, the event is not fired.
The event is also not fired on going back and forward in the browser.

Is there a reason why ui.navigate.to behaves differently from the ui.link implementation?

NiceGUI Version

3.2.0

Python Version

3.13.5

Browser

Firefox

Operating System

Windows

Additional Context

My final goal is to check if my user is still authenticated if a new sub page is accessed. Instead of checking it on every sub page i try to find a central solution like the starlette.middleware.base.BaseHTTPMiddleware, which does not work in that case because sub pages use url based navigation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugType/scope: A problem that needs fixingreviewStatus: PR is open and needs review

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions