Sub topic 4
Choosing targets: app, home and record pages
This chapter assumes you know the basic structure of a Lightning web component (HTML template, JavaScript class, and .js-meta.xml file). It also assumes you can deploy code to an org with the Salesforce CLI and have worked with Lightning pages in Setup.
What a target is
A target is a page type where Lightning App Builder lets an admin place your component. You list targets in the <targets> element of the .js-meta.xml file. Setting isExposed to true makes the component visible to App Builder. The targets decide which pages offer it.
If a page type is not listed, the component does not appear in the component list for that page type. A component with isExposed set to true and no targets is not offered on any page.
The three common targets
| Target | Page type | Typical use |
|---|---|---|
| `lightning__AppPage` | App page | Dashboards or landing pages for a Lightning app |
| `lightning__HomePage` | Home page | The Home tab, such as announcements or task summaries |
| `lightning__RecordPage` | Record page | A page for one record, such as an Account |
Choose targets based on where the component makes sense. A component that reads the current record belongs on a record page. A component that shows general information, such as a news feed, fits an app or home page.
Declaring the targets
This metadata file exposes the component on all three page types:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Account Summary Card</masterLabel>
<description>Shows a titled card with the current record context.</description>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>Each <target> element holds one value. To add a page type, add another <target> line.
How targets relate to the component class
The metadata file controls where the component can go. The JavaScript class controls what the component can receive. Record pages automatically set two public properties when they are declared with @api:
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
}On a record page, recordId holds the ID of the record being viewed and objectApiName holds its object name, such as Account. On app and home pages these two properties stay undefined, because no single record is in context.
<template>
<lightning-card title={headerText} icon-name="standard:account">
<div class="slds-var-p-around_medium">
<p>Record Id: {recordId}</p>
<p>Object: {objectApiName}</p>
<p>Max items: {maxItems}</p>
</div>
</lightning-card>
</template>If you expose this component on a home page, the first two lines show empty values. Write the template so it handles that case, or choose only the targets where the data exists.
flowchart TDChoosing targets in practice
The diagram below shows how to decide.
Follow these guidelines:
- Start with the narrowest set of targets that fits. You can add more later.
- Use
lightning__RecordPageonly when the component uses record context. - Add
lightning__AppPageandlightning__HomePagewhen the same component is useful without a record. - Change targets deliberately. Removing a target from a component already placed on a page can block saving or deploying changes to that page.
Deploying and checking
Deploy the bundle to your default org:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardThen open App Builder on an app page, the Home page, and an Account record page. The component should appear under Custom in the component list on each. If it is missing from one page type, compare that page type to the <targets> list first.
Exercise
A component is exposed with only lightning__RecordPage. An admin says it is missing when editing the Home page. What is the cause, and what do you change?
Show answer
The Home page uses the lightning__HomePage target, which is not listed. Add <target>lightning__HomePage</target> to the <targets> element and redeploy. Check that the component still works without recordId, because no record is in context on the Home page.