Sub topic 10
Navigating between pages and records
This chapter assumes you can build a basic Lightning web component and that you know how Experience Builder pages are organized. It covers how a component sends a visitor to another page or record inside a site, using NavigationMixin.
Why use NavigationMixin
A plain link with an href can break in a site. Site URLs include a base path, and that path differs between Aura and LWR sites and between environments. NavigationMixin from lightning/navigation takes a description of the destination, called a page reference, and builds the correct URL for the current site.
You apply the mixin by wrapping the base class:
export default class CaseLink extends NavigationMixin(LightningElement) {}The mixin adds two methods, each reached through a symbol key:
this[NavigationMixin.Navigate](pageReference)moves the visitor to the destination.this[NavigationMixin.GenerateUrl](pageReference)returns a promise that resolves to a URL string. Use it when you need a realhref, for example so a visitor can open the link in a new tab.
Navigating to a record
The example below sends the visitor to a Case record page. The record Id is stored in a data-id attribute on the clicked element and read back from the event.
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class CaseLink extends NavigationMixin(LightningElement) {
handleClick(event) {
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: event.currentTarget.dataset.id,
objectApiName: 'Case',
actionName: 'view'
}
});
}
}Use event.currentTarget, not event.target. currentTarget is the element that has the handler, so it still works when the visitor clicks a child element inside it.
The matching template puts the Id on the element:
<template>
<a data-id={recordId} onclick={handleClick}>Open case</a>
</template>Common page reference types
The type value decides which attributes you provide. These types are the ones you will use most in sites.
| Type | Goes to | Key attributes |
|---|---|---|
| `standard__recordPage` | A record page | `recordId`, `objectApiName`, `actionName` |
| `standard__objectPage` | An object list page | `objectApiName`, `actionName` |
| `standard__webPage` | An external URL | `url` |
| `comm__namedPage` | A named site page, such as Home | `name` |
Named pages use the API name of the page, which you can find in Experience Builder under the page settings.
this[NavigationMixin.Navigate]({
type: 'comm__namedPage',
attributes: { name: 'Home' }
});How a click becomes a page change
Common errors
- Navigation does nothing. The class is not wrapped in
NavigationMixin(LightningElement), or thetypestring has a typo. - The record page shows an access error. Navigation only moves the visitor. The visitor still needs access to the record through sharing and the site user's profile or permission sets. Guest users usually cannot open Case records at all.
recordIdis undefined. Thedata-idattribute was not rendered, or the handler usedevent.targetand landed on a child element.- A page works in one runtime but not the other. Some page reference types and attributes differ between Aura and LWR sites. Check the supported types for your runtime before you rely on one.
Exercise
A component uses event.target.dataset.id to read a record Id, and it sometimes returns undefined when the visitor clicks the text inside the link. What change fixes this, and why?
Show answer
Use event.currentTarget.dataset.id. target is the innermost element that was clicked, which may be a child without the data-id attribute. currentTarget is the element the handler is attached to, which has the attribute.
Exercise
Write the page reference that sends a visitor to the site page whose API name is Contact_Support.
Show answer
{ type: 'comm_namedPage', attributes: { name: 'ContactSupport' } }, passed to this[NavigationMixin.Navigate].