@@ -765,9 +765,9 @@ def add_page(
765765 msg = "Route must be set if component is not a callable."
766766 raise exceptions .RouteValueError (msg )
767767 # Format the route.
768- route = format .format_route (component .__name__ )
768+ route = format .format_route (format . to_kebab_case ( component .__name__ ) )
769769 else :
770- route = format .format_route (route , format_case = False )
770+ route = format .format_route (route )
771771
772772 if route == constants .Page404 .SLUG :
773773 if component is None :
@@ -822,10 +822,11 @@ def add_page(
822822 state = self ._state if self ._state else State
823823 state .setup_dynamic_args (get_route_args (route ))
824824
825- if on_load :
826- self ._load_events [route ] = (
827- on_load if isinstance (on_load , list ) else [on_load ]
828- )
825+ self ._load_events [route ] = (
826+ (on_load if isinstance (on_load , list ) else [on_load ])
827+ if on_load is not None
828+ else []
829+ )
829830
830831 self ._unevaluated_pages [route ] = unevaluated_page
831832
@@ -854,48 +855,35 @@ def _compile_page(self, route: str, save_page: bool = True):
854855 if save_page :
855856 self ._pages [route ] = component
856857
857- def get_load_events (self , route : str ) -> list [IndividualEventType [()]]:
858+ @functools .cached_property
859+ def router (self ) -> Callable [[str ], str | None ]:
860+ """Get the route computer function.
861+
862+ Returns:
863+ The route computer function.
864+ """
865+ from reflex .route import get_router
866+
867+ return get_router (list (self ._pages ))
868+
869+ def get_load_events (self , path : str ) -> list [IndividualEventType [()]]:
858870 """Get the load events for a route.
859871
860872 Args:
861- route : The route to get the load events for.
873+ path : The route to get the load events for.
862874
863875 Returns:
864876 The load events for the route.
865877 """
866- route = route .lstrip ("/" ).rstrip ("/" )
867- if route == "" :
868- return self ._load_events .get (constants .PageNames .INDEX_ROUTE , [])
869-
870- # Separate the pages by route type.
871- static_page_paths_to_page_route = {}
872- dynamic_page_paths_to_page_route = {}
873- for page_route in list (self ._pages ) + list (self ._unevaluated_pages ):
874- page_path = page_route .lstrip ("/" ).rstrip ("/" )
875- if "[" in page_path and "]" in page_path :
876- dynamic_page_paths_to_page_route [page_path ] = page_route
877- else :
878- static_page_paths_to_page_route [page_path ] = page_route
879-
880- # Check for static routes.
881- if (page_route := static_page_paths_to_page_route .get (route )) is not None :
882- return self ._load_events .get (page_route , [])
883-
884- # Check for dynamic routes.
885- parts = route .split ("/" )
886- for page_path , page_route in dynamic_page_paths_to_page_route .items ():
887- page_parts = page_path .split ("/" )
888- if len (page_parts ) != len (parts ):
889- continue
890- if all (
891- part == page_part
892- or (page_part .startswith ("[" ) and page_part .endswith ("]" ))
893- for part , page_part in zip (parts , page_parts , strict = False )
894- ):
895- return self ._load_events .get (page_route , [])
896-
897- # Default to 404 page load events if no match found.
898- return self ._load_events .get ("404" , [])
878+ four_oh_four_load_events = self ._load_events .get ("404" , [])
879+ route = self .router (path )
880+ if not route :
881+ # If the path is not a valid route, return the 404 page load events.
882+ return four_oh_four_load_events
883+ return self ._load_events .get (
884+ route ,
885+ four_oh_four_load_events ,
886+ )
899887
900888 def _check_routes_conflict (self , new_route : str ):
901889 """Verify if there is any conflict between the new route and any existing route.
@@ -916,7 +904,6 @@ def _check_routes_conflict(self, new_route: str):
916904 segments = (
917905 constants .RouteRegex .SINGLE_SEGMENT ,
918906 constants .RouteRegex .DOUBLE_SEGMENT ,
919- constants .RouteRegex .SINGLE_CATCHALL_SEGMENT ,
920907 constants .RouteRegex .DOUBLE_CATCHALL_SEGMENT ,
921908 )
922909 for route in self ._pages :
@@ -1742,6 +1729,11 @@ async def process(
17421729 # assignment will recurse into substates and force recalculation of
17431730 # dependent ComputedVar (dynamic route variables)
17441731 state .router_data = router_data
1732+ router_data [constants .RouteVar .PATH ] = "/" + (
1733+ app .router (path ) or "404"
1734+ if (path := router_data .get (constants .RouteVar .PATH ))
1735+ else "404"
1736+ ).removeprefix ("/" )
17451737 state .router = RouterData (router_data )
17461738
17471739 # Preprocess the event.
0 commit comments