-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@W-15205324 New rule - disallow import of scoped modules (#164)
* New rule - disallow import of scoped modules * Addressin the comments * Small correction in the title * Remove the link for formFactor * Remove the rule regarding the dynamic imports * Remove the link to the internal doc * Update docs/rules/no-form-factor-in-ssrable-components.md Co-authored-by: Laura <[email protected]> * Update lib/rules/no-static-imports-of-user-specific-scoped-modules-in-ssrable-components.js Co-authored-by: Laura <[email protected]> * Fixing the failing test * Update lib/rules/no-static-imports-of-user-specific-scoped-modules-in-ssrable-components.js Co-authored-by: nrobertdehault <[email protected]> * Addressing feedabck * Adding the link to the public doc * rebasing * Fix the link. * Renaming * Removing the config --------- Co-authored-by: lturanscaia <[email protected]> Co-authored-by: Laura <[email protected]> Co-authored-by: nrobertdehault <[email protected]>
- Loading branch information
1 parent
d05b230
commit a334492
Showing
8 changed files
with
415 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Using @salesforce/client/formFactor in SSR-able components is not the best practice(`lwc/ssr-no-form-factor`) | ||
|
||
## Rule details | ||
|
||
The [`@salesforce/client/formFactor`](https://developer.salesforce.com/docs/platform/lwc/guide/create-client-form-factor.html) module defaults to a value of `"Large"` during SSR, regardless of the device that made the request. This can cause issues where the UI shifts once client-side rendering is complete, particularly when rendering on smaller devices. To avoid this, use **[CSS media queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries)** to handle form factors and **[responsive images](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images)** for different screen sizes. | ||
|
||
Example of **incorrect** code: | ||
|
||
```js | ||
import FORM_FACTOR from '@salesforce/client/formFactor'; | ||
export default class Sample extends LightningElement { | ||
classes = ''; | ||
connectedCallback() { | ||
this.classes = FORM_FACTOR === 'small' ? 'mobile' : ''; | ||
} | ||
} | ||
``` | ||
|
||
```html | ||
<template> | ||
<div class="{classes}"> | ||
<div class="col-3"><!-- ... --></div> | ||
<div class="col-6"><!-- ... --></div> | ||
<div class="col-3"><!-- ... --></div> | ||
</div> | ||
</template> | ||
``` | ||
|
||
```css | ||
.col-3 { | ||
width: 25%; | ||
} | ||
.col-6 { | ||
width: 50%; | ||
} | ||
.mobile .col-3 { | ||
width: 100%; | ||
} | ||
.mobile .col-6 { | ||
width: 100%; | ||
} | ||
``` | ||
|
||
Example of **correct** code: | ||
|
||
```js | ||
export default class Sample extends LightningElement {} | ||
``` | ||
|
||
```html | ||
<template> | ||
<div> | ||
<div class="col-3"><!-- ... --></div> | ||
<div class="col-6"><!-- ... --></div> | ||
<div class="col-3"><!-- ... --></div> | ||
</div> | ||
</template> | ||
``` | ||
|
||
```css | ||
.col-3 { | ||
width: 25%; | ||
} | ||
.col-6 { | ||
width: 50%; | ||
} | ||
|
||
/* mobile */ | ||
@media (max-width: 768px) { | ||
[class*='col-'] { | ||
width: 100%; | ||
} | ||
} | ||
``` |
61 changes: 61 additions & 0 deletions
61
docs/rules/ssr-no-static-imports-of-user-specific-scoped-modules.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Deprecation Notice for Salesforce Scoped Modules during Server-Side Rendering (SSR) | ||
|
||
Static imports of user-specific scoped modules, such as `@salesforce/user/*`, are not supported in LWC components marked with `lightning__ServerRenderable` or `lightning__ServerRenderableWithHydration`. | ||
|
||
## Rule details | ||
|
||
The following Salesforce scoped modules are deprecated when using Server-Side Rendering (SSR): | ||
|
||
- `@salesforce/user/*` | ||
- `@salesforce/userPermission/*` | ||
- `@salesforce/customPermission/*` | ||
|
||
To replace these deprecated modules, use dynamic imports to fetch the necessary data on-demand. This ensures that SSR can run without issues and user-specific data is loaded appropriately after hydration on the client-side. For more details, refer to the guide on [Adding Dynamic Data to LWR Sites](https://developer.salesforce.com/docs/atlas.en-us.exp_cloud_lwr.meta/exp_cloud_lwr/advanced_expressions.htm). | ||
|
||
### Handling Deprecation in components | ||
|
||
If your component relies on one of these scoped modules, follow these best practices: | ||
|
||
1. **On the Server (SSR)**: | ||
|
||
- Render a placeholder during SSR to avoid negatively impacting **[web vitals](https://web.dev/articles/vitals)**. | ||
|
||
2. **On the Client (after Hydration)**: | ||
- **[Dynamically import](https://developer.salesforce.com/docs/platform/lwr/guide/lwr-portable-best-practices.html#dynamically-import-non-portable-modules)** the module after hydration to avoid SSR issues. | ||
|
||
```html | ||
<template> | ||
<template lwc:if="{userId}"><c-user-profile user-id="{userId}"></c-user-profile></template> | ||
<!-- Rendering a placeholder avoids layout shifts on the client when the user ID is loaded --> | ||
<template lwc:else><c-user-placeholder></c-user-placeholder></template> | ||
</template> | ||
``` | ||
|
||
Example of **incorrect** code: | ||
|
||
```js | ||
import { LightningElement } from 'lwc'; | ||
import Id from '@salesforce/user/Id'; | ||
|
||
export default class UserProfile extends LightningElement { | ||
userId = Id; | ||
} | ||
``` | ||
|
||
Example of **correct** code: | ||
|
||
```js | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class UserProfile extends LightningElement { | ||
userId; | ||
|
||
async connectedCallback() { | ||
if (!import.meta.env.SSR) { | ||
// Only load user-specific scoped modules on the client | ||
// This logic requires hydration of the component | ||
this.userId = await import('@salesforce/user/Id'); | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.