ctx.router
A router instance based on React Router, used for programmatic navigation within RunJS. It is typically used in conjunction with ctx.route and ctx.location.
Use Cases
Note:
ctx.routeris only available in RunJS environments with a routing context (e.g., JSBlock within a page, Flow pages, event flows, etc.); it may be null in pure backend or non-routing contexts (e.g., Workflows).
Type Definition
Router is derived from @remix-run/router. In RunJS, navigation operations such as jumping, going back, and refreshing are implemented via ctx.router.navigate().
Methods
ctx.router.navigate()
Navigates to a target path, or executes a back/refresh action.
Signature:
Parameters:
to: Target path (string), relative history position (number, e.g.,-1to go back), ornull(to refresh the current page).options: Optional configuration.replace?: boolean: Whether to replace the current history entry (default isfalse, which pushes a new entry).state?: any: State to pass to the target route. This data does not appear in the URL and can be accessed viactx.location.stateon the target page. It is suitable for sensitive information, temporary data, or information that should not be placed in the URL.
Examples
Basic Navigation
Replacing History (No new entry)
Passing State
Back and Refresh
Relationship with ctx.route and ctx.location
ctx.router is responsible for "navigation actions," while ctx.route and ctx.location are responsible for the "current route state."
Notes
navigate(path)pushes a new history entry by default, allowing users to return via the browser's back button.replace: truereplaces the current history entry without adding a new one, which is suitable for scenarios like post-login redirection or navigation after successful submission.- Regarding the
stateparameter:- Data passed via
statedoes not appear in the URL, making it suitable for sensitive or temporary data. - It can be accessed via
ctx.location.stateon the target page. stateis saved in the browser history and remains accessible during forward/backward navigation.statewill be lost after a hard page refresh.
- Data passed via
Related
- ctx.route: Current route match information (pathname, params, etc.).
- ctx.location: Current URL location (pathname, search, hash, state);
stateis read here after navigation.

