You can use the @lwrjs/router package to create a single page LWR application (SPA) with client-side routing. Important exports from the package include:
The first step in creating a router is to set up the root component of your SPA. You specify a rootComponent value in a routes object in your app’s lwr.config.json file. Check out the following example and “Routing Properties” for more information.
The routes defined in lwr.config.json are server-side. They differ from the RouteDefinition array that you set up in the next step for the client-side router.
In the previous step you created a router in your root component file. That router includes a promise in RouteDefinition.handler to a route handler module that’s called when a location matches a RouteDefinition. Route handler modules determine the associated “view”, which is the component to display when the application navigates to a location.
In LWR, route handler modules are dynamically imported, so they are provided via promises. Promises allow the module code to be lazily loaded, improving application performance.
Route handler modules allow you to define enhanced routing rules such as branching and pivoting logic based on data and metadata values. Here’s another route handler, this time with branching logic:
1// src/modules/example/namedPageHandler/namedPageHandler.js23export default class NamedPageHandler{4 callback;56 constructor(callback){7 this.callback = callback;8}910 dispose(){11 /* noop */12}1314 update({attributes}){15 let viewGetter;1617 // Get the "pageName" from the incoming page reference18 switch(attributes.pageName){19 case "products":20 viewGetter = ()=> import('example/products');21 break;22 case "recipes":23 viewGetter = ()=> import('example/recipes');24 break;25 case "contact":26 viewGetter = ()=> import('example/contact');27 break;28 default:29 return;30}3132 this.callback({33 viewset:{34 default: viewGetter,35},36});37}38}
For example, in this snippet, the NavigationContext wire obtains a ContextId value. It assigns the ID to the navContext property, where it can be used by the navigate() API.
1import{LightningElement, wire}from 'lwc';2import{NavigationContext, navigate}from 'lwr/navigation';34export default class HomeLink extends LightningElement{5 @wire(NavigationContext)6 navContext;78 navigateHome(event){9 // Navigate when the button is clicked10 event.preventDefault();11 if(this.navContext){12 navigate(this.navContext, {13 type: 'home',14 attributes:{format: 'list'},15 state:{'dark-mode': 'true'},16});17}18}19}
Sometimes the router navigation completes, but the component’s new view contains an error. This situation is considered a successful navigation, so an errornavigate event doesn’t fire. To handle this situation:
Feature is available as a developer preview. Feature is not generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. All commands, parameters, and other features are subject to change or deprecation at any time, with or without notice. Do not implement functionality developed with these commands or tools.