Skip to content

Commit 03ca787

Browse files
wjhsfdivmain
andauthored
chore: simplify style dedupe config (#5271)
* chore: simplify style dedupe config * Apply suggestions from code review Co-authored-by: Dale Bustad <dbustad@salesforce.com> * test(ssr-compiler): fix style dedupe param * test(integration-karma): update hydration tests to use new signature * chore(ssr-runtime): add warning for old renderComponent signature --------- Co-authored-by: Dale Bustad <dbustad@salesforce.com>
1 parent 672be9a commit 03ca787

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"entry": "x/parent",
3-
"styleDedupePrefix": "island1"
3+
"styleDedupe": "island1"
44
}

packages/@lwc/integration-karma/scripts/karma-plugins/hydration-tests.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ async function getSsrCode(moduleCode, testConfig, filename) {
147147
${testConfig};
148148
config = config || {};
149149
${moduleCode};
150-
moduleOutput = LWC.renderComponent('x-${COMPONENT_UNDER_TEST}-${guid++}', Main, config.props || {}, 'sync');`,
150+
moduleOutput = LWC.renderComponent(
151+
'x-${COMPONENT_UNDER_TEST}-${guid++}',
152+
Main,
153+
config.props || {},
154+
false,
155+
'sync'
156+
);`,
151157
{ filename }
152158
);
153159

packages/@lwc/ssr-compiler/src/__tests__/fixtures.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface FixtureConfig {
3232
};
3333

3434
/** The string used to uniquely identify one set of dedupe IDs with multiple SSR islands */
35-
styleDedupePrefix?: string;
35+
styleDedupe?: string;
3636
}
3737

3838
vi.mock('@lwc/ssr-runtime', async () => {
@@ -127,8 +127,7 @@ describe.concurrent('fixtures', () => {
127127
'fixture-test',
128128
module,
129129
config?.props ?? {},
130-
config?.styleDedupePrefix ?? '',
131-
true,
130+
config?.styleDedupe ?? true,
132131
SSR_MODE
133132
)
134133
);

packages/@lwc/ssr-runtime/src/render.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,47 @@ interface ComponentWithGenerateMarkup extends LightningElementConstructor {
193193

194194
export class RenderContext {
195195
styleDedupeIsEnabled: boolean;
196-
stylesheetToId = new WeakMap<Stylesheet, string>();
197196
styleDedupePrefix: string;
197+
stylesheetToId = new WeakMap<Stylesheet, string>();
198198
nextId = 0;
199199

200-
constructor(styleDedupePrefix: string, styleDedupeIsEnabled: boolean) {
201-
this.styleDedupeIsEnabled = styleDedupeIsEnabled;
202-
this.styleDedupePrefix = styleDedupePrefix;
200+
constructor(styleDedupe: string | boolean) {
201+
if (styleDedupe || styleDedupe === '') {
202+
this.styleDedupePrefix = typeof styleDedupe === 'string' ? styleDedupe : '';
203+
this.styleDedupeIsEnabled = true;
204+
} else {
205+
this.styleDedupePrefix = '';
206+
this.styleDedupeIsEnabled = false;
207+
}
203208
}
204209
}
205210

211+
/**
212+
* Create a string representing an LWC component for server-side rendering.
213+
* @param tagName The HTML tag name of the component
214+
* @param Component The `LightningElement` component constructor
215+
* @param props HTML attributes to provide for the root component
216+
* @param styleDedupe Provide a string key or `true` to enable style deduping via the `<lwc-style>`
217+
* helper. The key is used to avoid collisions of global IDs.
218+
* @param mode SSR render mode. Can be 'sync', 'async' or 'asyncYield'. Must match the render mode
219+
* used to compile your component.
220+
* @returns String representation of the component
221+
*/
206222
export async function serverSideRenderComponent(
207223
tagName: string,
208224
Component: ComponentWithGenerateMarkup,
209225
props: Properties = {},
210-
styleDedupePrefix = '',
211-
styleDedupeIsEnabled = false,
226+
styleDedupe: string | boolean = false,
212227
mode: CompilationMode = DEFAULT_SSR_MODE
213228
): Promise<string> {
229+
// TODO [#5309]: Remove this warning after a single release
230+
if (process.env.NODE_ENV !== 'production') {
231+
if (arguments.length === 6 || !['sync', 'async', 'asyncYield'].includes(mode)) {
232+
throw new Error(
233+
"The signature for @lwc/ssr-runtime's `renderComponent` has changed. There is now only one parameter for style dedupe."
234+
);
235+
}
236+
}
214237
if (typeof tagName !== 'string') {
215238
throw new Error(`tagName must be a string, found: ${tagName}`);
216239
}
@@ -222,7 +245,7 @@ export async function serverSideRenderComponent(
222245
markup += segment;
223246
};
224247

225-
emit.cxt = new RenderContext(styleDedupePrefix, styleDedupeIsEnabled);
248+
emit.cxt = new RenderContext(styleDedupe);
226249

227250
if (!generateMarkup) {
228251
// If a non-component is accidentally provided, render an empty template

0 commit comments

Comments
 (0)