Routing and page registration
Reflex turns Python page declarations into one route map for the backend and frontend. The map gives each page a URL, checks dynamic path rules, supplies route data to state, and connects the route to its generated frontend page. It also lets static serving separate valid application paths from unknown paths.
The routing model sits below Application runtime. It connects to Application lifecycle and configuration, State and events, Frontend compilation, and Runtime foundations.
Page registration
Page registration gives the application a renderable page and the URL that selects it. A page can be added with App.add_page or declared with @rx.page; both paths prepare the same validated page record before it enters the route map. The decorator keeps the render function unchanged and records its page data for the application to collect during compile.
Registration sources
The application accepts explicit page entries and decorated functions. A callable can use a kebab-case form of its name as the route when no route is given. A prebuilt component needs an explicit route. Decorated functions must be imported so their registration code runs.
The application prepares and validates each page before it changes route state. Direct registration commits one prepared page after conflict checks. During compile, decorated pages are collected and plugin pages use staged registrars. Plugin contributions commit only after all hooks, route conflicts, and dynamic-argument checks pass. A failed plugin batch therefore does not leave partial pages or route variables in the application.
| Source | Route rule | Main use |
|---|---|---|
App.add_page | An explicit route or a route derived from a callable name | Register a page during application setup |
@rx.page | Optional route and page settings recorded with the function | Register a page near its render function |
| Plugin page hook | Uses the same page contract during compile | Add framework or plugin-owned pages |
References: reflex/page.py• L17-67 reflex/app.py• L1515-1644 reflex/app.py• L1646-1873 reflex/app.py• L2186-2189
Route ownership
Each normalized route has one page owner. Repeating a route with the same component can merge unset title, description, and load-event values. A different component cannot take the route. Plugin pages cannot replace an app-owned route or another plugin page. A plugin can instead detect and keep an app-owned page through the registration contract.
The application also rejects dynamic routes that would occupy the same path tree while using different names for the same dynamic segment. These ownership rules keep backend matching and the generated React Router tree in agreement. A conflict is reported during registration or page compilation instead of being left to the browser router.
References: reflex/app.py• L1646-1873 reflex/app.py• L1907-1909 reflex/app.py• L1959-2026
Route model
The route model describes a URL as ordered path segments. Static segments name fixed parts of the URL. Bracketed segments name values that the browser supplies at navigation time.
Route forms
Reflex accepts these route forms:
| Form | Meaning | Value exposed to the page |
|---|---|---|
/products | Fixed path | No path argument |
/users/[user_id] | One required dynamic segment | One string |
/docs/[[section]] | One optional dynamic segment | One string or an empty value |
/posts/[[...splat]] | Optional catch-all path | A list of path segments |
Static route parts use letters, numbers, underscores, and hyphens. Dynamic names start with a letter or underscore and then use letters, numbers, or underscores. Dynamic names must be unique within a route. The optional catch-all form must be the final route segment and uses the reserved splat name.
References: reflex/route.py• L12-55 reflex/route.py• L57-96 packages/…/route.py• L36-68
Route validation
Validation runs before a page enters the application route map. It checks each segment's shape, rejects unsupported catch-all placement, and rejects repeated dynamic argument names when the route arguments are extracted. This prevents a route that the backend can parse differently from the route that the frontend can serve.
References: reflex/app.py• L1560-1644 reflex/route.py• L12-96
Route matching
Route matching maps a path relative to the frontend path prefix back to the registered route pattern. The matcher normalizes the leading and trailing slash and treats /index as the root path. It matches ASCII letters without case sensitivity. It does not remove application configuration from the path itself.
When the application looks up page-load events from a browser URL, it first checks the configured frontend path prefix. It removes that prefix only when it is the whole first path segment. The frontend already reports paths relative to that prefix, so those paths go directly to the matcher. A URL outside the prefix uses the registered 404 page and its load events.
When more than one pattern could match, the more specific pattern wins. A fixed path therefore takes priority over a dynamic path with the same shape. If no pattern matches, the application also uses the registered 404 page and its load events.
The route match moves through the runtime and frontend as shown below.
flowchart LR
A[Browser URL] --> B[Check frontend path prefix]
B --> C[Create relative path]
C --> D[Normalize path]
D --> E[Compare registered patterns]
E --> F{Match found}
F -->|Yes| G[Select page and load events]
F -->|No| H[Select 404 page]
G --> I[Render generated frontend page]
H --> I
References: reflex/route.py• L99-185 reflex/route.py• L188-234 reflex/app.py• L1922-1957
Dynamic route data
Dynamic route data connects the selected URL to page state. The route pattern identifies the page. The browser path supplies the values for its dynamic segments.
Page URL data
The page view exposes both matched and browser-facing forms of the URL. The matched path keeps the route pattern, such as /page/[page_id]. raw_path keeps the path shown in the browser, such as /page/11. The page data also exposes the full URL and a parameter map.
The parameter map carries dynamic path values and query values together. A single dynamic value is a string. A catch-all value is a list. Query access is also available through the router URL, which separates the raw query string from its parsed key-value view.
References: reflex/…/data.py• L198-217 reflex/…/data.py• L244-305 reflex/…/data.py• L341-377
State route variables
The application installs state variables for the dynamic names used by registered routes. These variables read from the current page parameter map, so a page can use a route value without parsing the browser URL itself. The framework keeps the value type stable when the same argument name appears on more than one route, including routes contributed by plugins.
References: reflex/state.py• L1344-1399 reflex/app.py• L1641-1644 reflex/app.py• L1830-1864
Page metadata
Page metadata turns page settings into document-level information for the generated frontend page. A page can define its title, description, image, extra meta entries, and script tags. Unset values use application defaults or the route-based default title.
Title and description values can be fixed text or state-derived Var values. Empty plain values remain unset, while a supplied Var counts as page metadata. For the 404 page, unset title and description values use the standard not-found defaults. The generated page receives the title and image entry, an optional description, and the extra meta entries supplied by the page. Application-wide document elements remain separate from these page-level values.
References: reflex/page.py• L17-43 reflex/app.py• L298-309 reflex/app.py• L1515-1644 packages/…/route.py• L71-91
Frontend page mapping
The frontend uses generated page modules that mirror the application route map. The compiler converts route segments into React Router file names, then emits one page module for each registered route. The frontend route list discovers those modules and adds a wildcard entry for the 404 page.
The mapping preserves the difference between static and dynamic segments. A route such as users/[id] becomes a frontend route module whose file name carries the dynamic $id segment. Optional segments and catch-all segments receive their own React Router forms, so the same route meaning is retained on both sides.
When the backend serves the compiled frontend as static files, it uses the same route matcher to identify valid application paths that do not have a pre-rendered file. Those paths receive the frontend shell with a successful status so the client router can render the page. Unknown paths keep the 404 status. Standalone frontend serving uses the route table written during compilation when an application route matcher is not available.
The complete page path is assembled in stages:
- Python registration creates a normalized route and page record.
- Page compilation evaluates the render function and attaches page metadata.
- Route conversion creates the generated frontend module path and import name.
- The frontend route list discovers the modules and sends unmatched paths to the 404 page.
- Static serving uses the compiled route table to separate routable SPA fallbacks from unknown paths.
References: reflex/…/utils.py• L677-724 reflex/…/compiler.py• L739-764 reflex/…/compiler.py• L1216-1234 reflex/…/compiler.py• L1523-1532 reflex/app.py• L1296-1333 packages/…/routes.js• L1-9
Page load behavior
Each page can define load events that run when that page becomes active. The application stores these events by route, matches the current path, and falls back to the 404 events when the path has no registered page.
Client-side navigation updates the route data and starts the page load sequence. A direct window.history.replaceState call changes the browser URL but is not a navigation. The router observes that URL on the next event instead. A framework redirect is a navigation and updates the route data without an extra event from the user.
References: reflex/app.py• L1680-1714 reflex/app.py• L1933-1957 reflex/app.py• L1318-1333
Change checklist
When these files or sections are changed, remember to consider these:
- Route syntax changes must stay aligned between
verify_route_validity,get_route_args, the route matcher, and frontend file-name conversion. - Page preparation changes must apply to direct registration, decorated pages, and staged plugin page hooks.
- Plugin route checks must preserve the rule that a failed hook batch leaves no partial routes or dynamic route variables.
- Changes to dynamic argument names or types must account for state variables installed from every registered route.
- Page metadata changes must update both decorator and direct-registration paths so they produce the same document head.
- Changes to the 404 route must stay aligned with the frontend wildcard route and backend load-event fallback.
- Changes to frontend-path scoping and route normalization must preserve configured frontend path prefixes and root-route aliases.
- Changes to the route list must stay aligned with the static frontend mount and its compiled route manifest so direct loads of dynamic pages keep the right status.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Two different pages register the same route | Registration fails instead of choosing an undefined owner | Keep one page owner per normalized route |
| A plugin registers an app-owned route or duplicates another plugin route | The plugin batch fails before its routes are committed | Use unique plugin routes and check app-owned routes before registration |
| A plugin hook raises an error after earlier hooks add pages | The staged plugin contributions and their dynamic variables are not committed | Treat plugin route registration as one validated batch |
| A dynamic route repeats an argument name | Parameter extraction is ambiguous | Use a unique dynamic name in each route |
| A route uses an invalid segment or catch-all position | The page is rejected before compile | Follow the supported static, dynamic, optional, and [[...splat]] forms |
| One route uses a dynamic name as a string and another uses it as a catch-all list | The shared state variable would have conflicting types | Use one argument type for a name across routes |
| A browser URL is outside the configured frontend path | The application returns the 404 page load events instead of matching a page | Keep frontend URLs under the configured frontend path |
| A path matches no registered route | The 404 page and its load events handle the request | Register the intended route or provide the 404 page behavior |
| A page function is decorated but never imported | Its route is never recorded | Import the module that defines the decorated page |
| Standalone static serving has no valid compiled route manifest | A valid dynamic URL without a pre-rendered file can keep a 404 status | Compile the frontend with the current route table and preserve its manifest |
Static serving treats /404 or the /index alias as a valid application path | A path the frontend renders as 404 can receive a successful SPA-fallback status | Exclude the synthetic 404 route and only treat the bare root as the index page |
Key files
| File | Why to read it |
|---|---|
| reflex/app.py | Defines page preparation, direct and plugin registration, route ownership, route matching, and load-event lookup. |
| reflex/page.py | Shows the public decorator that records page functions and page settings. |
| reflex/route.py | Defines route validation, dynamic argument extraction, specificity, and matching for paths relative to the frontend path. |
| packages/…/route.py | Defines the shared route forms, argument types, URL keys, and page defaults. |
| reflex/state.py | Shows how dynamic route values become state variables and how page load events are queued. |
| reflex/…/data.py | Defines the page and URL data exposed to application state. |
| reflex/…/compiler.py | Connects page evaluation, metadata, route compilation, compiled page registration, and the route manifest. |
| reflex/…/utils.py | Shows the conversion from application routes to generated frontend module paths and page metadata. |
| reflex/…/exec.py | Shows how the compiled frontend uses route information for SPA fallback status and standalone manifest-based serving. |
| packages/…/routes.js | Defines frontend route discovery and the fallback wildcard route. |