|
1 | 1 | import { globalScripts } from '@app-globals';
|
2 | 2 | import { addHostEventListeners, doc, getHostRef, loadModule, plt, registerHost } from '@platform';
|
3 | 3 | import { connectedCallback, insertVdomAnnotations } from '@runtime';
|
| 4 | +import { CMP_FLAGS } from '@utils'; |
4 | 5 |
|
5 | 6 | import type * as d from '../../declarations';
|
6 | 7 | import { proxyHostElement } from './proxy-host-element';
|
@@ -84,6 +85,24 @@ export function hydrateApp(
|
84 | 85 |
|
85 | 86 | if (Cstr != null && Cstr.cmpMeta != null) {
|
86 | 87 | // we found valid component metadata
|
| 88 | + |
| 89 | + if ( |
| 90 | + opts.serializeShadowRoot !== false && |
| 91 | + !!(Cstr.cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) && |
| 92 | + tagRequiresScoped(elm.tagName, opts.serializeShadowRoot) |
| 93 | + ) { |
| 94 | + // this component requires scoped css encapsulation during SSR |
| 95 | + const cmpMeta = Cstr.cmpMeta; |
| 96 | + cmpMeta.$flags$ |= CMP_FLAGS.shadowNeedsScopedCss; |
| 97 | + |
| 98 | + // 'cmpMeta' is a getter only, so needs redefining |
| 99 | + Object.defineProperty(Cstr as any, 'cmpMeta', { |
| 100 | + get: function (this: any) { |
| 101 | + return cmpMeta; |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
87 | 106 | createdElements.add(elm);
|
88 | 107 | elm.connectedCallback = patchedConnectedCallback;
|
89 | 108 |
|
@@ -333,3 +352,41 @@ function waitingOnElementMsg(waitingElement: HTMLElement) {
|
333 | 352 | function waitingOnElementsMsg(waitingElements: Set<HTMLElement>) {
|
334 | 353 | return Array.from(waitingElements).map(waitingOnElementMsg);
|
335 | 354 | }
|
| 355 | + |
| 356 | +/** |
| 357 | + * Determines if the tag requires a declarative shadow dom |
| 358 | + * or a scoped / light dom during SSR. |
| 359 | + * |
| 360 | + * @param tagName - component tag name |
| 361 | + * @param opts - serializeShadowRoot options |
| 362 | + * @returns `true` when the tag requires a scoped / light dom during SSR |
| 363 | + */ |
| 364 | +export function tagRequiresScoped(tagName: string, opts: d.HydrateFactoryOptions['serializeShadowRoot']) { |
| 365 | + if (typeof opts === 'string') { |
| 366 | + return opts === 'scoped'; |
| 367 | + } |
| 368 | + |
| 369 | + if (typeof opts === 'boolean') { |
| 370 | + return opts === true ? false : true; |
| 371 | + } |
| 372 | + |
| 373 | + if (typeof opts === 'object') { |
| 374 | + tagName = tagName.toLowerCase(); |
| 375 | + |
| 376 | + if (Array.isArray(opts['declarative-shadow-dom']) && opts['declarative-shadow-dom'].includes(tagName)) { |
| 377 | + // if the tag is in the dsd array, return dsd |
| 378 | + return false; |
| 379 | + } else if ( |
| 380 | + (!Array.isArray(opts.scoped) || !opts.scoped.includes(tagName)) && |
| 381 | + opts.default === 'declarative-shadow-dom' |
| 382 | + ) { |
| 383 | + // if the tag is not in the scoped array and the default is dsd, return dsd |
| 384 | + return false; |
| 385 | + } else { |
| 386 | + // otherwise, return scoped |
| 387 | + return true; |
| 388 | + } |
| 389 | + } |
| 390 | + |
| 391 | + return false; |
| 392 | +} |
0 commit comments