Sub topic 6
Reading site and user context
This chapter assumes you can build a basic Lightning web component and use ES module imports. It also assumes you have read the earlier chapters on Experience Builder and on exposing components.
Why context imports exist
A site component often needs to know who is viewing it and which site it is running in. You could query the User object through Apex, but that costs a server call for facts the platform already has. Lightning Web Components provide scoped module imports for these values instead.
The common imports
| Import | Value |
|---|---|
| `@salesforce/user/Id` | The Id of the current user. For a visitor who is not logged in, this is the site's guest user. |
| `@salesforce/user/isGuest` | `true` if the visitor is unauthenticated. |
| `@salesforce/community/Id` | The Id of the current site. |
| `@salesforce/community/basePath` | The URL path prefix of the site, such as `/support`. It is an empty string if the site is at the domain root. |
Each import is a default export. You can name it anything, but names like userId and isGuest make the code easier to read.
A working example
This component displays the context values and builds a link to the site home page.
import { LightningElement } from 'lwc';
import userId from '@salesforce/user/Id';
import isGuest from '@salesforce/user/isGuest';
import communityId from '@salesforce/community/Id';
import basePath from '@salesforce/community/basePath';
export default class SiteContext extends LightningElement {
userId = userId;
isGuest = isGuest;
communityId = communityId;
get homeUrl() {
return `${basePath}/`;
}
}<template>
<template lwc:if={isGuest}>
<p>You are browsing as a guest.</p>
</template>
<template lwc:else>
<p>Signed in as user {userId}.</p>
</template>
<p>Site Id: {communityId}</p>
<a href={homeUrl}>Home</a>
</template>Prefixing links with basePath keeps them correct if an admin later changes the site's URL path. A hard-coded /support/ would break.
How the values behave
The platform resolves these imports when the component loads, so they are constants for that page view. They are not reactive. If a guest logs in, the browser loads a new page and the component is created again with new values.
The imports are also available only inside the platform. In Jest tests you must supply them yourself. Use jest.mock with the { virtual: true } option and return a default value for each module.
Passing context to Apex
Use the user Id to filter data in the browser, such as highlighting a record the user owns. Do not use it to decide what the user may see. Apex already knows the running user, so you rarely need to send the Id at all. The getMyCases method from the data chapter relies on WITH USER_MODE and sends no user Id.
Common errors
- Using the imports in a component that also runs outside a site. In a Lightning Experience page,
community/Idandcommunity/basePathgive no meaningful site value, so check for an empty result before building links. - Assuming
userIdidentifies a person for guests. All guests share one guest user record. - Mutating the imported value. Imports are read-only bindings, so copy them into a class field first, as shown above.
- Hard-coding paths. Build every internal URL from
basePath, or useNavigationMixin, which handles the prefix for you.
Exercise
A component builds the link /support/contact by string concatenation. The admin changes the site path to /help. What breaks, and which import fixes it?
Show answer
The link still points to /support/contact, so it no longer matches the site. Import basePath from @salesforce/community/basePath and build the link as ${basePath}/contact.
Exercise
Why is hiding a "My cases" list with isGuest not enough to protect case data?
Show answer
isGuest only affects what the template renders. A guest can still call the Apex method directly, so the method must enforce sharing and field access itself, for example with with sharing and WITH USER_MODE, and the guest profile must not grant access to the data.