Sub topic 4
Choosing targets: app, home and record pages
This chapter assumes you can already read a Lightning web component bundle (HTML template, JavaScript class and .js-meta.xml file) and that you have seen the isExposed flag from the previous chapter. You should also know the three kinds of Lightning pages in Setup: app pages, the home page and record pages. Basic XML syntax is enough for the metadata.
What a target is
A target is a place where Salesforce lets a component be used. The <targets> element in the .js-meta.xml file lists the targets your component supports. Each target is a separate <target> child, and the value is a fixed string.
isExposed answers the question "may admins see this component at all?". Targets answer "on which kinds of pages?". Lightning App Builder reads the list when an admin edits a page. It shows only the components whose targets match the page type being edited.
This chapter covers the three targets used most often in App Builder:
| Target value | Page type in App Builder | Typical use |
|---|---|---|
| `lightning__AppPage` | App page | A custom landing page for a Lightning app |
| `lightning__HomePage` | Home page | The page users see when they open the Home tab |
| `lightning__RecordPage` | Record page | The detail page for one record, such as an Account |
Values are case-sensitive and use two underscores after lightning. lightning_RecordPage and lightning__recordpage are both wrong.
A minimal targets block
This metadata file exposes a component to 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>After you deploy it, an admin can drag the component onto an app page, the home page or a record page. If you delete one <target> line and deploy again, the component disappears from that page type's component list. Pages that already contain the component are a separate case, covered under "Removing a target" below.
The next diagram shows how the targets list controls where the component appears.
How the three page types differ
The target you choose decides what the component can assume about its surroundings.
App pages
An app page is a custom page that you add to a Lightning app, usually through a tab. It is not tied to any record. A component on an app page gets no recordId and no objectApiName. Both properties are undefined.
Use lightning__AppPage for dashboards, search tools, launch pads and other components that work across records.
Home pages
The home page is the page users see when they open the Home tab. Like an app page, it has no record context. The two targets are separate because App Builder keeps them as different page types. A component that supports only lightning__AppPage does not appear when an admin edits a home page.
If the component does the same job on both, list both targets. You can share one targetConfig between them, as the next chapters show.
Record pages
A record page shows one record, such as an Account, Contact or custom object record. This is the only one of the three that provides record context. Lightning sets two public properties automatically:
recordIdis the 15- or 18-character ID of the record being viewed.objectApiNameis the API name of that record's object, for exampleAccount.
For Salesforce to fill them in, your class must declare them with @api, and the names must match exactly.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
get hasRecordContext() {
return Boolean(this.recordId);
}
get contextLabel() {
return this.hasRecordContext
? `${this.objectApiName} ${this.recordId}`
: 'No record context (app or home page)';
}
}The getter hasRecordContext lets one component run safely on all three page types. On an app or home page it returns false, so the template can show a different message instead of failing.
<template>
<lightning-card title={headerText} icon-name="standard:account">
<div class="slds-var-p-around_medium">
<p>{contextLabel}</p>
<p>Max items: {maxItems}</p>
</div>
</lightning-card>
</template>Salesforce sets recordId after the component is created. Do not read it in the constructor. Use a getter, the connectedCallback lifecycle hook, or a wire adapter that reacts to changes.
Choosing targets: decision criteria
Pick targets from what the component needs, not from what is convenient to test.
- Does the component read or edit a specific record? Use
lightning__RecordPageonly. Without a record,recordIdis empty and the component has nothing to show. - Does the component work without any record? Use
lightning__AppPage,lightning__HomePageor both. - Does the component work with or without a record? List all the targets and handle the empty
recordIdcase in code, as in the getter above. - Does it only make sense for one object? Keep
lightning__RecordPageand add anobjectsrestriction. That is covered in a later chapter.
Fewer targets mean fewer places for the component to break. A component that crashes when recordId is missing should not be offered on the home page.
Trade-off: one component or several
A single component with three targets is simple to deploy and maintain. It becomes awkward when the page types need different behavior, with record-only logic mixed into dashboard logic. In that case, build a small component per page type and share logic through a plain JavaScript module that both import. Each component then declares only the targets it really supports.
Targets and target configuration
Targets say where a component can go. The optional <targetConfigs> element says how it behaves for each target. In a targetConfig, the targets attribute is a comma-separated list of the same target values:
<targetConfigs>
<targetConfig targets="lightning__AppPage,lightning__HomePage">
<property name="headerText" type="String" label="Header text" default="Summary" />
</targetConfig>
<targetConfig targets="lightning__RecordPage">
<property name="headerText" type="String" label="Header text" default="Summary" />
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />
</targetConfig>
</targetConfigs>Here admins can set only the header on app and home pages. On record pages they can set the header and the maximum item count. A target that has no matching targetConfig still works. It simply exposes no configurable properties.
A targetConfig can only refer to targets that appear in the <targets> list. A mismatch is one of the common errors below.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Component is missing on home pages | `lightning__HomePage` is not in `<targets>` | Add the target and redeploy |
| Component is missing everywhere | `isExposed` is `false`, or the deploy failed | Set `isExposed` to `true`, then check the deploy output |
| Deploy fails with a metadata error | Misspelled target, such as `lightning_RecordPage` | Correct the value, which is case-sensitive |
| `recordId` is `undefined` on a record page | Property is not declared with `@api`, or is named differently | Declare `@api recordId;` exactly |
| `recordId` is `undefined` on a home page | Home pages have no record | Handle the empty case or remove the target |
| Property does not show in the panel for one page type | `targetConfig` does not list that target | Add the target to the `targets` attribute |
Removing a target
Removing a target from the metadata affects only the component list. If an admin already placed the component on a page of that type, the page keeps working until someone edits it. Removing a target is therefore not a safe way to retire a component. Check your pages first.
Deploying and checking the result
Deploy the bundle to your default org, then open App Builder and look in the component list under Custom.
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardTo verify each target, edit one page of each type. Open an app page, the home page and an Account record page. On each, search the component list for the masterLabel, "Account Summary Card". It should be present on all three. If it is missing from one, compare that page type with your <targets> list.
Exercise
Your component reads data for the current record using recordId. A teammate adds lightning__HomePage to its targets so it can be placed on the home page. What happens to recordId there, and what should the component do?
Show answer
On the home page there is no record, so recordId is undefined. The component should check for an empty recordId, for example with a getter such as hasRecordContext, and show a fallback message or skip the record-based logic. If the component has no useful behavior without a record, the better fix is to remove the home page target.
Exercise
You deployed a component with only lightning__RecordPage as a target, and an admin cannot find it while editing an app page. Is this a bug? What do you change to make it appear there?
Show answer
It is not a bug. App Builder offers a component only on page types listed in its targets. Add <target>lightning__AppPage</target> to the <targets> list, make sure the component handles a missing recordId, and redeploy.