LWC NavigationMixin
Activate when an LWC needs to navigate — to a record, a list view, a named page, or an external URL. NavigationMixin is the canonical Salesforce API for navigation; raw window.location usage is forbidden on most surfaces and bypasses Salesforce's routing, tab, and mobile handling.
Before Starting
- Import the mixin correctly.
import { NavigationMixin } from 'lightning/navigation';and apply withextends NavigationMixin(LightningElement). - Pick the right PageReference type. Internal app vs Experience Cloud has different names (
standard__*vscomm__*). - Distinguish
navigate()vsgenerateUrl(). Navigate triggers routing; generateUrl returns a URL promise for anchors, copy-to-clipboard, etc.
Core Concepts
PageReference shape
{
type: 'standard__recordPage',
attributes: { recordId: '001...', objectApiName: 'Account', actionName: 'view' },
state: { c__tab: 'details' }
}
attributes are type-specific; state flows through URL params as c__*.
Internal vs Experience Cloud
- Internal:
standard__recordPage,standard__objectPage,standard__namedPage,standard__webPage - Experience Cloud:
comm__namedPage,comm__loginPage(prefer overstandard__)
navigate vs generateUrl
this[NavigationMixin.Navigate](pageRef); // route now
this[NavigationMixin.GenerateUrl](pageRef).then(url => ...); // URL string
GenerateUrl is async (returns a Promise).
New tab
Wrap in an <a target="_blank" href={url}> using generateUrl. The mixin has no direct "open in new tab" option.
Common Patterns
Pattern: Navigate to record view
const ref = { type: 'standard__recordPage',
attributes: { recordId: this.recordId, objectApiName: 'Account', actionName: 'view' } };
this[NavigationMixin.Navigate](ref);
Pattern: Generate URL for copy-to-clipboard
const url = await this[NavigationMixin.GenerateUrl](ref);
navigator.clipboard.writeText(window.location.origin + url);
Pattern: State params for tab selection
{ type: 'standard__recordPage', attributes: { ... },
state: { c__selectedTab: 'history' } }
Receiving component reads @wire(CurrentPageReference) pageRef and pageRef.state.c__selectedTab.
Decision Guidance
| Target | PageReference type |
|---|---|
| Record view / edit | standard__recordPage |
| Object list view | standard__objectPage + list actionName |
| Custom Lightning component | standard__component |
| External URL | standard__webPage |
| Experience Cloud named page | comm__namedPage |
| Relative URL in Experience Cloud | comm__namedPage with pageName |
Recommended Workflow
- Identify target context (internal, Experience, mobile).
- Pick PageReference type matching target and context.
- Populate
attributes(recordId, objectApiName, etc.) per type spec. - Use
statefor transient params (tab, filter). - Choose
Navigate(immediate) orGenerateUrl(async). - For mobile deep-links, test via Mobile Publisher.
- Never fall back to
window.location.href =— breaks routing.
Review Checklist
- NavigationMixin applied via
extends NavigationMixin(LightningElement) - PageReference type matches the surface (standard vs comm)
- Attributes populated with required keys
- State params prefixed
c__where custom - GenerateUrl used for hrefs; Navigate for routing
- No
window.locationfallbacks - Experience Cloud deep-links tested in the Experience context
- Mobile app deep-links tested
Salesforce-Specific Gotchas
stateparam names must start withc__unless using a framework-defined key.standard__namedPagecannot be used in Experience Cloud; usecomm__namedPage.- GenerateUrl is async. Awaiting is required; returning the promise without awaiting leaves consumers with
undefined.
Output Artifacts
| Artifact | Description |
|---|---|
| PageReference catalog | Type × attributes × surface |
| URL helper module | Reusable generateUrl wrappers |
| Deep-link test matrix | Internal / Experience / Mobile coverage |
Related Skills
lwc/lwc-url-params-and-state— state-param handlingadmin/app-and-tab-configuration— tab and app setupmobile/mobile-deep-linking— mobile-specific nav