|
| 1 | +# Prevent importing restricted APIs from the "lwc" package in SSR-able components (`@lwc/lwc/ssr-no-disallowed-lwc-imports`) |
| 2 | + |
| 3 | +Restricts importing specific modules from the `lwc` package in components that may be server-side rendered (`lightning__ServerRenderable` or `lightning__ServerRenderableWithHydration`). Certain LWC APIs may not be compatible with server-side rendering and should be avoided in SSR-able components. |
| 4 | + |
| 5 | +This rule is complementary to the general `no-disallowed-lwc-imports` rule and only flags LWC APIs that are otherwise valid but not supported in SSR contexts. |
| 6 | + |
| 7 | +## Rule details |
| 8 | + |
| 9 | +This rule prevents imports of LWC APIs that are not supported or recommended in server-side rendering contexts. By default, it disallows: |
| 10 | + |
| 11 | +- `readonly` - Not supported in SSR environment |
| 12 | + |
| 13 | +Examples of **incorrect** code: |
| 14 | + |
| 15 | +```js |
| 16 | +import { readonly } from 'lwc'; |
| 17 | + |
| 18 | +export default class MyComponent extends LightningElement { |
| 19 | + @api value = readonly({ name: 'test' }); |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +```js |
| 24 | +import { LightningElement, readonly } from 'lwc'; |
| 25 | + |
| 26 | +export default class MyComponent extends LightningElement { |
| 27 | + data = readonly({ items: [] }); |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Examples of **correct** code: |
| 32 | + |
| 33 | +```js |
| 34 | +import { LightningElement } from 'lwc'; |
| 35 | + |
| 36 | +export default class MyComponent extends LightningElement { |
| 37 | + @api value = { name: 'test' }; |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +```js |
| 42 | +import { LightningElement, api, track } from 'lwc'; |
| 43 | + |
| 44 | +export default class MyComponent extends LightningElement { |
| 45 | + @api data; |
| 46 | + @track items = []; |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +```js |
| 51 | +// Re-exporting is allowed since the component doesn't use the API itself |
| 52 | +export { readonly } from 'lwc'; |
| 53 | + |
| 54 | +export default class MyUtilityComponent extends LightningElement { |
| 55 | + // This component is SSR-compatible |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## Configuration |
| 60 | + |
| 61 | +### `disallowlist` |
| 62 | + |
| 63 | +The `disallowlist` property allows you to specify which LWC APIs should be disallowed in SSR context. It accepts an array of strings and overrides the default list. |
| 64 | + |
| 65 | +Examples of **incorrect** code with custom disallowlist: |
| 66 | + |
| 67 | +```js |
| 68 | +/* eslint @lwc/lwc/ssr-no-disallowed-lwc-imports: ["error", { "disallowlist": ["readonly", "track"] }] */ |
| 69 | +import { track } from 'lwc'; |
| 70 | +``` |
| 71 | + |
| 72 | +```js |
| 73 | +/* eslint @lwc/lwc/ssr-no-disallowed-lwc-imports: ["error", { "disallowlist": ["readonly", "track"] }] */ |
| 74 | +import { readonly } from 'lwc'; |
| 75 | +``` |
| 76 | + |
| 77 | +Examples of **correct** code with custom disallowlist: |
| 78 | + |
| 79 | +```js |
| 80 | +/* eslint @lwc/lwc/ssr-no-disallowed-lwc-imports: ["error", { "disallowlist": ["readonly", "track"] }] */ |
| 81 | +import { LightningElement, api } from 'lwc'; |
| 82 | +``` |
| 83 | + |
| 84 | +## When Not To Use It |
| 85 | + |
| 86 | +If your components are never server-side rendered or if you need to use specific LWC APIs that are flagged by this rule, you may want to disable it. However, be aware that using unsupported APIs in SSR contexts may cause runtime errors or unexpected behavior during server-side rendering. |
| 87 | + |
| 88 | +## Relationship to Other Rules |
| 89 | + |
| 90 | +This rule works alongside the general `lwc/no-disallowed-lwc-imports` rule: |
| 91 | + |
| 92 | +- **`no-disallowed-lwc-imports`**: Handles general import/export validation (bare imports, namespace imports, default imports, etc.) |
| 93 | +- **`ssr-no-disallowed-lwc-imports`**: Focuses only on SSR-specific import restrictions for otherwise valid imports |
| 94 | + |
| 95 | +Both rules should typically be used together in SSR-able components. |
0 commit comments