Sub topic 8
Generating URLs for links with GenerateUrl
This chapter assumes you can build a basic Lightning Web Component. It also assumes you know @api, Promises, and how to apply NavigationMixin.
Why generate a URL
Navigate moves the user right away. Sometimes you need a real link instead. A link lets users open a record in a new tab, copy the address, or hover to see where it goes. A button with a click handler can't do any of that.
NavigationMixin.GenerateUrl takes the same PageReference object as Navigate. It returns a Promise that resolves to a URL string. Nothing is navigated.
Generating a URL
Apply the mixin to your class, then call this[NavigationMixin.GenerateUrl] with a PageReference.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class RecordLink extends NavigationMixin(LightningElement) {
@api recordId;
url;
connectedCallback() {
this[NavigationMixin.GenerateUrl]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
actionName: 'view'
}
}).then((url) => {
this.url = url;
});
}
}The call is asynchronous. url is undefined until the Promise resolves, so the template must handle that first render.
Using the URL in a template
Bind the result to the href of an anchor.
<template>
<template lwc:if={url}>
<a href={url} target="_blank">Open record</a>
</template>
</template>The lwc:if directive keeps the anchor out of the DOM until the URL exists. Because the anchor has a real href, the browser handles "open in new tab" and "copy link address" for you.
Navigating from the same link
If you want a normal click to use in-app navigation, you can keep the generated href and also call Navigate. Intercept the click, stop the browser's default action, and navigate:
handleClick(event) {
event.preventDefault();
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
actionName: 'view'
}
});
}Add onclick={handleClick} to the anchor. A normal click then navigates inside the app. A middle-click or "open in new tab" still uses the generated href.
Common errors
- Reading
this.urlright after callingGenerateUrl. The value is not set until the Promise resolves. - Forgetting
NavigationMixin(LightningElement)in theextendsclause. Without it,this[NavigationMixin.GenerateUrl]is not a function. - Building the URL by hand, such as
'/lightning/r/Account/' + id + '/view'. A PageReference keeps working if the URL format changes. It also works across Experience Cloud sites and other containers. - Passing a PageReference with a missing required attribute, such as no
recordIdon a record page. The generated URL will be wrong or the call will fail.
Exercise
Why does a template that renders <a href={url}> need to guard against url being undefined, and which directive does that?
Show answer
GenerateUrl returns a Promise, so url is not set during the first render. Wrapping the anchor in <template lwc:if={url}> renders it only after the URL has resolved.
Exercise
You want an anchor that navigates in-app on a normal click but still supports "open in new tab". What do you combine?
Show answer
Set href to the URL from GenerateUrl. Add a click handler that calls event.preventDefault() and then this[NavigationMixin.Navigate] with the same PageReference.