Sub topic 5
Understanding PageReference objects
This chapter explains the object that every navigation call in Lightning Web Components takes as input: the PageReference. You should already know the basic structure of a Lightning web component, how to import from lightning/navigation, and how to apply NavigationMixin to a component class. Later chapters cover each page type in detail. Here you learn the shape of the object and the rules that apply to all page types.
What a PageReference is
A PageReference is a plain JavaScript object that describes a destination. It does not navigate anywhere by itself. You pass it to a navigation function, and the platform works out the page to show and the URL to use.
Because it is only data, you can build it in a getter, store it in a field, compare it, or log it. The same object works with both NavigationMixin.Navigate and NavigationMixin.GenerateUrl.
A PageReference has three properties:
| Property | Required | Purpose |
|---|---|---|
| `type` | Yes | A string that names the kind of page, such as `standard__recordPage`. |
| `attributes` | Yes | Values that identify the specific page, such as a record ID or an object API name. |
| `state` | No | Extra key-value pairs that change how the page behaves, such as a filter or a default field value. |
The type property
The type value selects the kind of destination. It also decides which attributes you must supply. The table lists the types you will meet most often.
| Type | Destination |
|---|---|
| `standard__recordPage` | A record page, for example to view or edit one record |
| `standard__objectPage` | An object page, such as a list view or the New form |
| `standard__recordRelationshipPage` | A related list of a record |
| `standard__namedPage` | A standard page identified by name, such as Home |
| `standard__navItemPage` | A custom tab (nav item) |
| `standard__app` | An app |
| `standard__webPage` | An external URL |
| `comm__namedPage` | A named page in an Experience Cloud site |
Types use two underscores after the prefix. Typing standard_recordPage with one underscore is a common mistake, and the navigation will not work. Experience Cloud sites support only some of these types, so check the reference for the context where your component runs.
The attributes property
The attributes object holds the identifying values. For a record page it needs recordId and actionName. For an object page it needs objectApiName and actionName. For a web page it needs url. Each type has its own required set, and a missing or misspelled attribute leaves the platform unable to resolve the destination.
const accountView = {
type: 'standard__recordPage',
attributes: {
recordId: '001000000000001AAA',
objectApiName: 'Account',
actionName: 'view'
}
};The actionName attribute tells the platform what to do on the page. Typical values are view, edit, new, list, and home, but which ones are valid depends on the type. A later chapter lists the valid combinations for record and object pages.
The state property
The state property carries optional parameters. For many page types it shows up as query parameters in the browser URL. This makes state useful when you want a destination to open in a particular configuration, and it lets a user bookmark or share the resulting URL.
Rules to follow:
- Keep state values as strings. Convert numbers and booleans yourself, and parse them back when you read them.
- Prefix your own keys with
c__. Keys without a namespace prefix are reserved for standard parameters, andc__marks the key as belonging to a custom component. - Keep state small. It becomes part of the URL, so do not put sensitive data in it.
const accountsFiltered = {
type: 'standard__objectPage',
attributes: {
objectApiName: 'Account',
actionName: 'list'
},
state: {
filterName: 'Recent'
}
};The state for standard pages, such as list view filters and default field values, is covered in its own chapter. The c__ convention matters most for custom pages and components, which read the values back with the CurrentPageReference wire adapter, also covered later.
One object, two uses
The key idea is reuse. A PageReference feeds two different functions:
this[NavigationMixin.Navigate](pageRef)moves the user to the page now.this[NavigationMixin.GenerateUrl](pageRef)returns a Promise that resolves to a URL string, which you can use as thehrefof a link.
Define the object once in a getter and call both functions with it. That way the link and the click handler cannot drift apart.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class AccountShortcut extends NavigationMixin(LightningElement) {
@api recordId;
url;
get accountPageRef() {
return {
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
objectApiName: 'Account',
actionName: 'view'
}
};
}
connectedCallback() {
this[NavigationMixin.GenerateUrl](this.accountPageRef).then((url) => {
this.url = url;
});
}
handleClick(event) {
event.preventDefault();
this[NavigationMixin.Navigate](this.accountPageRef);
}
}The template pairs with this class. The href lets users open the link in a new tab, and the click handler keeps normal clicks inside the single-page app.
<template>
<a href={url} onclick={handleClick}>Open account</a>
</template>Calling preventDefault stops the browser from following the href as a full page load, so Navigate handles the click instead.
Replacing history entries
Navigate accepts an optional second argument. When you pass true, the new page replaces the current entry in the browser history instead of adding one.
this[NavigationMixin.Navigate](this.accountPageRef, true);Use this for redirects, such as sending a user away from a page that no longer applies. With the default false, the Back button returns to the page the user left.
Common errors
- Wrong type string. Typos in
typeare the most frequent cause of a navigation that silently does nothing. Copy type names from the documentation. - Missing required attribute. A record page without
recordId, or a web page withouturl, cannot be resolved. Check your attributes against the type. - Unresolved values. If a value such as
this.recordIdisundefinedwhen you build the object, the reference is incomplete. In a getter that runs early, check the value before using it. - Non-string state values. Pass
String(count)rather than the number itself. - Missing mixin.
this[NavigationMixin.Navigate]exists only on a class declared withNavigationMixin(LightningElement).
Summary of the model
Think of a PageReference as an address with three parts. The type says which kind of building. The attributes give the street number. The state adds delivery instructions. Navigation functions read that address and take the user there, or write it out as a URL.
Exercise
A component builds this object and calls Navigate, but nothing happens: { type: 'standard__recordPage', attributes: { objectApiName: 'Account', actionName: 'view' } }. What is the most likely cause, and how do you fix it? Also, how would you pass a custom value named pageSize with the number 25 in state?
Show answer
The attributes lack the required recordId for a record page, so the platform cannot resolve a destination. Add recordId with the record's ID. For the custom value, add a state property with a prefixed key and a string value: state: { c__pageSize: String(25) }.
Exercise
You want one account link that works on a normal click and also opens in a new tab with a middle click. Which two NavigationMixin functions do you use, and what do you give both of them?
Show answer
Use GenerateUrl to produce the string for the anchor's href, and Navigate in the click handler (after preventDefault). Give both the same PageReference object, ideally from one getter.