@stencil/core 5.0.0-rc.14 → 5.0.0-rc.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -647,19 +647,28 @@ const getSlottedChildNodes = (childNodes) => {
647
647
  * @returns a reference to the slot node that matches the provided name, `null` otherwise
648
648
  */
649
649
  function getHostSlotNodes(childNodes, hostName, slotName) {
650
- let i = 0;
651
- let slottedNodes = [];
652
- let childNode;
653
- for (; i < childNodes.length; i++) {
654
- childNode = childNodes[i];
650
+ const slotNodes = [];
651
+ collectHostSlotNodes(childNodes, hostName, slotName, slotNodes);
652
+ return slotNodes;
653
+ }
654
+ /**
655
+ * Pre-order walk for {@link getHostSlotNodes}, appending to `out`.
656
+ * @param childNodes the nodes to search
657
+ * @param hostName the host name of the slot to match on
658
+ * @param slotName the name of the slot to match on; stops at the first match when given
659
+ * @param out the matching slot nodes, in document order
660
+ * @returns true once a named slot has been found
661
+ */
662
+ function collectHostSlotNodes(childNodes, hostName, slotName, out) {
663
+ for (let i = 0; i < childNodes.length; i++) {
664
+ const childNode = childNodes[i];
655
665
  if (childNode["s-sr"] && (!hostName || childNode["s-hn"] === hostName) && (slotName === void 0 || getSlotName(childNode) === slotName)) {
656
- slottedNodes.push(childNode);
657
- if (typeof slotName !== "undefined") return slottedNodes;
666
+ out.push(childNode);
667
+ if (slotName !== void 0) return true;
658
668
  }
659
- slottedNodes = [...slottedNodes, ...getHostSlotNodes(internalCall(childNode, "childNodes"), hostName, slotName)];
660
- if (typeof slotName !== "undefined" && slottedNodes.length) return slottedNodes;
669
+ if (collectHostSlotNodes(internalCall(childNode, "childNodes"), hostName, slotName, out) && slotName !== void 0) return true;
661
670
  }
662
- return slottedNodes;
671
+ return false;
663
672
  }
664
673
  /**
665
674
  * Get all 'child' sibling nodes of a slot node
@@ -778,11 +787,40 @@ function findSlotFromSlottedNode(slottedNode, parentHost) {
778
787
  slotName: ""
779
788
  };
780
789
  const slotName = slottedNode["s-sn"] = getSlotName(slottedNode) || "";
790
+ let hostSlots = slotNodeCache.get(parentHost);
791
+ const cached = hostSlots?.get(slotName);
792
+ if (cached && isHostSlot(cached, parentHost, slotName)) return {
793
+ slotNode: cached,
794
+ slotName
795
+ };
796
+ const slotNode = getHostSlotNodes(internalCall(parentHost, "childNodes"), parentHost.tagName, slotName)[0];
797
+ if (slotNode) {
798
+ if (!hostSlots) slotNodeCache.set(parentHost, hostSlots = /* @__PURE__ */ new Map());
799
+ hostSlots.set(slotName, slotNode);
800
+ }
781
801
  return {
782
- slotNode: getHostSlotNodes(internalCall(parentHost, "childNodes"), parentHost.tagName, slotName)[0],
802
+ slotNode,
783
803
  slotName
784
804
  };
785
805
  }
806
+ /**
807
+ * The slot node last found per host and slot name. Each slotted child of a host looks up its
808
+ * slot, and a full search walks the host's whole internal tree, so a host with many slotted
809
+ * children would otherwise cost O(children × tree).
810
+ */
811
+ const slotNodeCache = /* @__PURE__ */ new WeakMap();
812
+ /**
813
+ * Whether a cached slot node is still the host's slot of that name.
814
+ * @param slotNode the cached slot node
815
+ * @param host the host element
816
+ * @param slotName the slot name
817
+ * @returns true if `slotNode` is still a slot of `host` named `slotName`, inside its internal tree
818
+ */
819
+ const isHostSlot = (slotNode, host, slotName) => {
820
+ if (!slotNode["s-sr"] || slotNode["s-hn"] !== host.tagName || getSlotName(slotNode) !== slotName) return false;
821
+ for (let n = internalCall(slotNode, "parentNode"); n; n = internalCall(n, "parentNode")) if (n === host) return true;
822
+ return false;
823
+ };
786
824
  //#endregion
787
825
  //#region src/runtime/dom-extras.ts
788
826
  const applyLightDomPatches = (hostElementPrototype) => {
@@ -2564,8 +2602,7 @@ const insertBefore = (parent, newNode, reference, isInitialLoad) => {
2564
2602
  if (parent?.["s-cr"]) checkSlotRelocate = true;
2565
2603
  if (BUILD.scoped && typeof newNode["s-sn"] === "string" && !!newNode["s-sr"] && !!newNode["s-cr"]) addRemoveSlotScopedClass(newNode["s-cr"], newNode, parent, newNode.parentElement);
2566
2604
  else if (typeof newNode["s-sn"] === "string") {
2567
- const hostElm = newNode["s-hn"] && parent.closest?.(newNode["s-hn"]);
2568
- if ((BUILD.lightDomPatches || BUILD.slotChildNodes || BUILD.patchAll && !!(hostElm && getHostRef(hostElm)?.$cmpMeta$.$flags$ & CMP_FLAGS.patchAll)) && parent.getRootNode().nodeType !== NODE_TYPES.DOCUMENT_FRAGMENT_NODE) patchSlottedNode(newNode);
2605
+ if ((BUILD.lightDomPatches || BUILD.slotChildNodes || BUILD.patchAll && hostPatchesAll(newNode, parent)) && parent.getRootNode().nodeType !== NODE_TYPES.DOCUMENT_FRAGMENT_NODE) patchSlottedNode(newNode);
2569
2606
  parent.insertBefore(newNode, reference);
2570
2607
  const { slotNode } = findSlotFromSlottedNode(newNode);
2571
2608
  if (slotNode) {
@@ -2579,6 +2616,16 @@ const insertBefore = (parent, newNode, reference, isInitialLoad) => {
2579
2616
  else return parent?.insertBefore(newNode, reference);
2580
2617
  };
2581
2618
  /**
2619
+ * Whether the host a slotted node belongs to opted into `patchAll`.
2620
+ * @param newNode the slotted node
2621
+ * @param parent the node it is being inserted into
2622
+ * @returns true if the host component has the `patchAll` flag
2623
+ */
2624
+ const hostPatchesAll = (newNode, parent) => {
2625
+ const hostElm = newNode["s-hn"] && parent.closest?.(newNode["s-hn"]);
2626
+ return !!(hostElm && getHostRef(hostElm)?.$cmpMeta$.$flags$ & CMP_FLAGS.patchAll);
2627
+ };
2628
+ /**
2582
2629
  * Adds or removes a scoped class to the parent element of a slotted node.
2583
2630
  * This is used for styling slotted content (e.g. with `::scoped(...) {...}` selectors )
2584
2631
  * in `scoped: true` components.
@@ -4074,7 +4121,7 @@ const insertPolyfillHostInCssText = (cssText) => {
4074
4121
  const _colonHostContextRe = createSupportsRuleRe(":host-context");
4075
4122
  cssText = cssText.replace(_colonHostContextRe, `$1${_polyfillHostContext}`).replace(_colonHostRe, `$1${_polyfillHost}`).replace(_colonSlottedRe, `$1${_polyfillSlotted}`);
4076
4123
  supportsBlocks.forEach((originalSelector, index) => {
4077
- cssText = cssText.replace(`__supports_${index}__`, originalSelector);
4124
+ cssText = cssText.replace(`__supports_${index}__`, () => originalSelector);
4078
4125
  });
4079
4126
  return cssText;
4080
4127
  };
@@ -4307,11 +4354,11 @@ const scopeCss = (cssText, scopeId, commentOriginalSelector) => {
4307
4354
  const scoped = scopeCssText(cssText, scopeId, hostScopeId, slotScopeId, commentOriginalSelector);
4308
4355
  cssText = [scoped.cssText, ...commentsWithHash].join("\n");
4309
4356
  if (commentOriginalSelector) orgSelectors.forEach(({ placeholder, comment }) => {
4310
- cssText = cssText.replace(placeholder, comment);
4357
+ cssText = cssText.replace(placeholder, () => comment);
4311
4358
  });
4312
4359
  scoped.slottedSelectors.forEach((slottedSelector) => {
4313
4360
  const regex = new RegExp(escapeRegExpSpecialCharacters(slottedSelector.orgSelector) + "(?=\\s*[,{]|$)", "g");
4314
- cssText = cssText.replace(regex, slottedSelector.updatedSelector);
4361
+ cssText = cssText.replace(regex, () => slottedSelector.updatedSelector);
4315
4362
  });
4316
4363
  cssText = expandPartSelectors(cssText);
4317
4364
  return cssText;
@@ -4556,6 +4603,28 @@ const proxyComponent = (Cstr, cmpMeta, flags) => {
4556
4603
  };
4557
4604
  //#endregion
4558
4605
  //#region src/runtime/initialize-component.ts
4606
+ /** Distinct style texts kept; an app has one per component and mode. */
4607
+ const SSR_CSS_CACHE_MAX = 1e3;
4608
+ /**
4609
+ * Memoizes a server-side rewrite of a component's style text. The rewrites are pure, but the
4610
+ * platform (and its `styles` registry) is rebuilt per render, so without this they'd rerun on
4611
+ * every render.
4612
+ * @param style the component's style text
4613
+ * @param key what the rewrite depends on besides the text
4614
+ * @param rewrite computes the result
4615
+ * @returns the rewritten style
4616
+ */
4617
+ const ssrCachedCss = (style, key, rewrite) => {
4618
+ if (typeof $stencilSsrCssCache === "undefined" || typeof style !== "string") return rewrite();
4619
+ let byKey = $stencilSsrCssCache.get(style);
4620
+ if (!byKey) {
4621
+ if ($stencilSsrCssCache.size >= SSR_CSS_CACHE_MAX) $stencilSsrCssCache.clear();
4622
+ $stencilSsrCssCache.set(style, byKey = /* @__PURE__ */ new Map());
4623
+ }
4624
+ let result = byKey.get(key);
4625
+ if (result === void 0) byKey.set(key, result = rewrite());
4626
+ return result;
4627
+ };
4559
4628
  /**
4560
4629
  * Initialize a Stencil component given a reference to its host element, its
4561
4630
  * runtime bookkeeping data structure, runtime metadata about the component,
@@ -4656,8 +4725,8 @@ const initializeComponent = async (elm, hostRef, cmpMeta, hmrVersionId) => {
4656
4725
  if (!styles.has(scopeId) || BUILD.hotModuleReplacement && hmrVersionId) {
4657
4726
  const endRegisterStyles = createTime("registerStyles", cmpMeta.$tagName$);
4658
4727
  if (BUILD.hydrateServerSide && BUILD.shadowDom) {
4659
- if (cmpMeta.$flags$ & CMP_FLAGS.shadowNeedsScopedCss) style = scopeCss(style, scopeId, true);
4660
- else if (needsScopedSSR()) style = expandPartSelectors(style);
4728
+ if (cmpMeta.$flags$ & CMP_FLAGS.shadowNeedsScopedCss) style = ssrCachedCss(style, `s:${scopeId}`, () => scopeCss(style, scopeId, true));
4729
+ else if (needsScopedSSR()) style = ssrCachedCss(style, "p", () => expandPartSelectors(style));
4661
4730
  }
4662
4731
  registerStyle(scopeId, style, !!(cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation));
4663
4732
  endRegisterStyles();
@@ -4876,12 +4945,12 @@ const hmrApplyClass = (cmpMeta, NewClass) => {
4876
4945
  /**
4877
4946
  * SSR hosts connected while the HTML parser is still inside them (streamed documents),
4878
4947
  * in connection order - which is document order. Each is held until its subtree is
4879
- * parsed so hydration sees its declarative shadow root and children.
4948
+ * parsed so hydration sees its declarative shadow root and children, and watched for
4949
+ * its end marker arriving (wherever it is - a document observer can't see into shadow roots).
4880
4950
  */
4881
- const pending = /* @__PURE__ */ new Set();
4951
+ const pending = /* @__PURE__ */ new Map();
4882
4952
  /** SSR hosts seen parsed: hydration may since have moved or removed their end marker */
4883
4953
  const parsedHosts = /* @__PURE__ */ new WeakSet();
4884
- let observer;
4885
4954
  const parentOf = (node) => node.parentNode ?? (node.nodeType === NODE_TYPE.DocumentFragment ? node.host : null);
4886
4955
  /**
4887
4956
  * The server writes a `HOST_END_ID` comment as an SSR host's last child, so the parser has
@@ -4901,17 +4970,17 @@ const hasPendingAncestor = (elm) => {
4901
4970
  for (let n = parentOf(elm); n; n = parentOf(n)) if (pending.has(n)) return true;
4902
4971
  return false;
4903
4972
  };
4973
+ const drop = (elm) => {
4974
+ pending.get(elm)?.disconnect();
4975
+ pending.delete(elm);
4976
+ };
4904
4977
  const flush = () => {
4905
- for (const elm of pending) if (!elm.isConnected) pending.delete(elm);
4978
+ for (const elm of pending.keys()) if (!elm.isConnected) drop(elm);
4906
4979
  else if (isParsed(elm)) elm.__attachShadow?.();
4907
- for (const elm of pending) if (isParsed(elm) && !hasPendingAncestor(elm)) {
4908
- pending.delete(elm);
4980
+ for (const elm of pending.keys()) if (isParsed(elm) && !hasPendingAncestor(elm)) {
4981
+ drop(elm);
4909
4982
  elm.connectedCallback?.();
4910
4983
  }
4911
- if (!pending.size) {
4912
- observer?.disconnect();
4913
- observer = void 0;
4914
- }
4915
4984
  };
4916
4985
  /**
4917
4986
  * Whether the parser may still be adding children (including a declarative shadow root)
@@ -4930,18 +4999,13 @@ const mayStillBeParsing = (elm) => win.document?.readyState === "loading" && (!e
4930
4999
  const deferUntilParsed = (elm) => {
4931
5000
  if (pending.has(elm)) return true;
4932
5001
  if (!win.document || !elm.hasAttribute("s-id")) return false;
4933
- const parsed = isParsed(elm);
4934
- if (parsed && !hasPendingAncestor(elm)) return false;
4935
- if (parsed) elm.__attachShadow?.();
4936
- pending.add(elm);
4937
- if (!observer) {
4938
- observer = new MutationObserver(flush);
4939
- observer.observe(win.document, {
4940
- childList: true,
4941
- subtree: true
4942
- });
4943
- win.document.addEventListener("DOMContentLoaded", flush, { once: true });
4944
- }
5002
+ const done = isParsed(elm);
5003
+ if (done && !hasPendingAncestor(elm)) return false;
5004
+ if (done) elm.__attachShadow?.();
5005
+ const observer = new MutationObserver(flush);
5006
+ observer.observe(elm, { childList: true });
5007
+ pending.set(elm, observer);
5008
+ win.document.addEventListener("DOMContentLoaded", flush, { once: true });
4945
5009
  return true;
4946
5010
  };
4947
5011
  //#endregion
@@ -444,19 +444,28 @@ const getSlottedChildNodes = (childNodes) => {
444
444
  * @returns a reference to the slot node that matches the provided name, `null` otherwise
445
445
  */
446
446
  function getHostSlotNodes(childNodes, hostName, slotName) {
447
- let i = 0;
448
- let slottedNodes = [];
449
- let childNode;
450
- for (; i < childNodes.length; i++) {
451
- childNode = childNodes[i];
447
+ const slotNodes = [];
448
+ collectHostSlotNodes(childNodes, hostName, slotName, slotNodes);
449
+ return slotNodes;
450
+ }
451
+ /**
452
+ * Pre-order walk for {@link getHostSlotNodes}, appending to `out`.
453
+ * @param childNodes the nodes to search
454
+ * @param hostName the host name of the slot to match on
455
+ * @param slotName the name of the slot to match on; stops at the first match when given
456
+ * @param out the matching slot nodes, in document order
457
+ * @returns true once a named slot has been found
458
+ */
459
+ function collectHostSlotNodes(childNodes, hostName, slotName, out) {
460
+ for (let i = 0; i < childNodes.length; i++) {
461
+ const childNode = childNodes[i];
452
462
  if (childNode["s-sr"] && (!hostName || childNode["s-hn"] === hostName) && (slotName === void 0 || getSlotName(childNode) === slotName)) {
453
- slottedNodes.push(childNode);
454
- if (typeof slotName !== "undefined") return slottedNodes;
463
+ out.push(childNode);
464
+ if (slotName !== void 0) return true;
455
465
  }
456
- slottedNodes = [...slottedNodes, ...getHostSlotNodes(internalCall(childNode, "childNodes"), hostName, slotName)];
457
- if (typeof slotName !== "undefined" && slottedNodes.length) return slottedNodes;
466
+ if (collectHostSlotNodes(internalCall(childNode, "childNodes"), hostName, slotName, out) && slotName !== void 0) return true;
458
467
  }
459
- return slottedNodes;
468
+ return false;
460
469
  }
461
470
  /**
462
471
  * Get all 'child' sibling nodes of a slot node
@@ -575,11 +584,40 @@ function findSlotFromSlottedNode(slottedNode, parentHost) {
575
584
  slotName: ""
576
585
  };
577
586
  const slotName = slottedNode["s-sn"] = getSlotName(slottedNode) || "";
587
+ let hostSlots = slotNodeCache.get(parentHost);
588
+ const cached = hostSlots?.get(slotName);
589
+ if (cached && isHostSlot(cached, parentHost, slotName)) return {
590
+ slotNode: cached,
591
+ slotName
592
+ };
593
+ const slotNode = getHostSlotNodes(internalCall(parentHost, "childNodes"), parentHost.tagName, slotName)[0];
594
+ if (slotNode) {
595
+ if (!hostSlots) slotNodeCache.set(parentHost, hostSlots = /* @__PURE__ */ new Map());
596
+ hostSlots.set(slotName, slotNode);
597
+ }
578
598
  return {
579
- slotNode: getHostSlotNodes(internalCall(parentHost, "childNodes"), parentHost.tagName, slotName)[0],
599
+ slotNode,
580
600
  slotName
581
601
  };
582
602
  }
603
+ /**
604
+ * The slot node last found per host and slot name. Each slotted child of a host looks up its
605
+ * slot, and a full search walks the host's whole internal tree, so a host with many slotted
606
+ * children would otherwise cost O(children × tree).
607
+ */
608
+ const slotNodeCache = /* @__PURE__ */ new WeakMap();
609
+ /**
610
+ * Whether a cached slot node is still the host's slot of that name.
611
+ * @param slotNode the cached slot node
612
+ * @param host the host element
613
+ * @param slotName the slot name
614
+ * @returns true if `slotNode` is still a slot of `host` named `slotName`, inside its internal tree
615
+ */
616
+ const isHostSlot = (slotNode, host, slotName) => {
617
+ if (!slotNode["s-sr"] || slotNode["s-hn"] !== host.tagName || getSlotName(slotNode) !== slotName) return false;
618
+ for (let n = internalCall(slotNode, "parentNode"); n; n = internalCall(n, "parentNode")) if (n === host) return true;
619
+ return false;
620
+ };
583
621
  //#endregion
584
622
  //#region src/runtime/dom-extras.ts
585
623
  const applyLightDomPatches = (hostElementPrototype) => {
@@ -2336,8 +2374,7 @@ const insertBefore = (parent, newNode, reference, isInitialLoad) => {
2336
2374
  if (parent?.["s-cr"]) checkSlotRelocate = true;
2337
2375
  if (BUILD$1.scoped && typeof newNode["s-sn"] === "string" && !!newNode["s-sr"] && !!newNode["s-cr"]) addRemoveSlotScopedClass(newNode["s-cr"], newNode, parent, newNode.parentElement);
2338
2376
  else if (typeof newNode["s-sn"] === "string") {
2339
- const hostElm = newNode["s-hn"] && parent.closest?.(newNode["s-hn"]);
2340
- if ((BUILD$1.lightDomPatches || BUILD$1.slotChildNodes || BUILD$1.patchAll && !!(hostElm && getHostRef(hostElm)?.$cmpMeta$.$flags$ & CMP_FLAGS.patchAll)) && parent.getRootNode().nodeType !== NODE_TYPES.DOCUMENT_FRAGMENT_NODE) patchSlottedNode(newNode);
2377
+ if ((BUILD$1.lightDomPatches || BUILD$1.slotChildNodes || BUILD$1.patchAll && hostPatchesAll(newNode, parent)) && parent.getRootNode().nodeType !== NODE_TYPES.DOCUMENT_FRAGMENT_NODE) patchSlottedNode(newNode);
2341
2378
  parent.insertBefore(newNode, reference);
2342
2379
  const { slotNode } = findSlotFromSlottedNode(newNode);
2343
2380
  if (slotNode) {
@@ -2351,6 +2388,16 @@ const insertBefore = (parent, newNode, reference, isInitialLoad) => {
2351
2388
  else return parent?.insertBefore(newNode, reference);
2352
2389
  };
2353
2390
  /**
2391
+ * Whether the host a slotted node belongs to opted into `patchAll`.
2392
+ * @param newNode the slotted node
2393
+ * @param parent the node it is being inserted into
2394
+ * @returns true if the host component has the `patchAll` flag
2395
+ */
2396
+ const hostPatchesAll = (newNode, parent) => {
2397
+ const hostElm = newNode["s-hn"] && parent.closest?.(newNode["s-hn"]);
2398
+ return !!(hostElm && getHostRef(hostElm)?.$cmpMeta$.$flags$ & CMP_FLAGS.patchAll);
2399
+ };
2400
+ /**
2354
2401
  * Adds or removes a scoped class to the parent element of a slotted node.
2355
2402
  * This is used for styling slotted content (e.g. with `::scoped(...) {...}` selectors )
2356
2403
  * in `scoped: true` components.
@@ -3844,7 +3891,7 @@ const insertPolyfillHostInCssText = (cssText) => {
3844
3891
  const _colonHostContextRe = createSupportsRuleRe(":host-context");
3845
3892
  cssText = cssText.replace(_colonHostContextRe, `$1${_polyfillHostContext}`).replace(_colonHostRe, `$1${_polyfillHost}`).replace(_colonSlottedRe, `$1${_polyfillSlotted}`);
3846
3893
  supportsBlocks.forEach((originalSelector, index) => {
3847
- cssText = cssText.replace(`__supports_${index}__`, originalSelector);
3894
+ cssText = cssText.replace(`__supports_${index}__`, () => originalSelector);
3848
3895
  });
3849
3896
  return cssText;
3850
3897
  };
@@ -4077,11 +4124,11 @@ const scopeCss = (cssText, scopeId, commentOriginalSelector) => {
4077
4124
  const scoped = scopeCssText(cssText, scopeId, hostScopeId, slotScopeId, commentOriginalSelector);
4078
4125
  cssText = [scoped.cssText, ...commentsWithHash].join("\n");
4079
4126
  if (commentOriginalSelector) orgSelectors.forEach(({ placeholder, comment }) => {
4080
- cssText = cssText.replace(placeholder, comment);
4127
+ cssText = cssText.replace(placeholder, () => comment);
4081
4128
  });
4082
4129
  scoped.slottedSelectors.forEach((slottedSelector) => {
4083
4130
  const regex = new RegExp(escapeRegExpSpecialCharacters(slottedSelector.orgSelector) + "(?=\\s*[,{]|$)", "g");
4084
- cssText = cssText.replace(regex, slottedSelector.updatedSelector);
4131
+ cssText = cssText.replace(regex, () => slottedSelector.updatedSelector);
4085
4132
  });
4086
4133
  cssText = expandPartSelectors(cssText);
4087
4134
  return cssText;
@@ -4326,6 +4373,28 @@ const proxyComponent = (Cstr, cmpMeta, flags) => {
4326
4373
  };
4327
4374
  //#endregion
4328
4375
  //#region src/runtime/initialize-component.ts
4376
+ /** Distinct style texts kept; an app has one per component and mode. */
4377
+ const SSR_CSS_CACHE_MAX = 1e3;
4378
+ /**
4379
+ * Memoizes a server-side rewrite of a component's style text. The rewrites are pure, but the
4380
+ * platform (and its `styles` registry) is rebuilt per render, so without this they'd rerun on
4381
+ * every render.
4382
+ * @param style the component's style text
4383
+ * @param key what the rewrite depends on besides the text
4384
+ * @param rewrite computes the result
4385
+ * @returns the rewritten style
4386
+ */
4387
+ const ssrCachedCss = (style, key, rewrite) => {
4388
+ if (typeof $stencilSsrCssCache === "undefined" || typeof style !== "string") return rewrite();
4389
+ let byKey = $stencilSsrCssCache.get(style);
4390
+ if (!byKey) {
4391
+ if ($stencilSsrCssCache.size >= SSR_CSS_CACHE_MAX) $stencilSsrCssCache.clear();
4392
+ $stencilSsrCssCache.set(style, byKey = /* @__PURE__ */ new Map());
4393
+ }
4394
+ let result = byKey.get(key);
4395
+ if (result === void 0) byKey.set(key, result = rewrite());
4396
+ return result;
4397
+ };
4329
4398
  /**
4330
4399
  * Initialize a Stencil component given a reference to its host element, its
4331
4400
  * runtime bookkeeping data structure, runtime metadata about the component,
@@ -4426,8 +4495,8 @@ const initializeComponent = async (elm, hostRef, cmpMeta, hmrVersionId) => {
4426
4495
  if (!styles.has(scopeId) || BUILD$1.hotModuleReplacement && hmrVersionId) {
4427
4496
  const endRegisterStyles = createTime("registerStyles", cmpMeta.$tagName$);
4428
4497
  if (BUILD$1.hydrateServerSide && BUILD$1.shadowDom) {
4429
- if (cmpMeta.$flags$ & CMP_FLAGS.shadowNeedsScopedCss) style = scopeCss(style, scopeId, true);
4430
- else if (needsScopedSSR()) style = expandPartSelectors(style);
4498
+ if (cmpMeta.$flags$ & CMP_FLAGS.shadowNeedsScopedCss) style = ssrCachedCss(style, `s:${scopeId}`, () => scopeCss(style, scopeId, true));
4499
+ else if (needsScopedSSR()) style = ssrCachedCss(style, "p", () => expandPartSelectors(style));
4431
4500
  }
4432
4501
  registerStyle(scopeId, style, !!(cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation));
4433
4502
  endRegisterStyles();
@@ -4646,12 +4715,12 @@ const hmrApplyClass = (cmpMeta, NewClass) => {
4646
4715
  /**
4647
4716
  * SSR hosts connected while the HTML parser is still inside them (streamed documents),
4648
4717
  * in connection order - which is document order. Each is held until its subtree is
4649
- * parsed so hydration sees its declarative shadow root and children.
4718
+ * parsed so hydration sees its declarative shadow root and children, and watched for
4719
+ * its end marker arriving (wherever it is - a document observer can't see into shadow roots).
4650
4720
  */
4651
- const pending = /* @__PURE__ */ new Set();
4721
+ const pending = /* @__PURE__ */ new Map();
4652
4722
  /** SSR hosts seen parsed: hydration may since have moved or removed their end marker */
4653
4723
  const parsedHosts = /* @__PURE__ */ new WeakSet();
4654
- let observer;
4655
4724
  const parentOf = (node) => node.parentNode ?? (node.nodeType === NODE_TYPE.DocumentFragment ? node.host : null);
4656
4725
  /**
4657
4726
  * The server writes a `HOST_END_ID` comment as an SSR host's last child, so the parser has
@@ -4671,17 +4740,17 @@ const hasPendingAncestor = (elm) => {
4671
4740
  for (let n = parentOf(elm); n; n = parentOf(n)) if (pending.has(n)) return true;
4672
4741
  return false;
4673
4742
  };
4743
+ const drop = (elm) => {
4744
+ pending.get(elm)?.disconnect();
4745
+ pending.delete(elm);
4746
+ };
4674
4747
  const flush = () => {
4675
- for (const elm of pending) if (!elm.isConnected) pending.delete(elm);
4748
+ for (const elm of pending.keys()) if (!elm.isConnected) drop(elm);
4676
4749
  else if (isParsed(elm)) elm.__attachShadow?.();
4677
- for (const elm of pending) if (isParsed(elm) && !hasPendingAncestor(elm)) {
4678
- pending.delete(elm);
4750
+ for (const elm of pending.keys()) if (isParsed(elm) && !hasPendingAncestor(elm)) {
4751
+ drop(elm);
4679
4752
  elm.connectedCallback?.();
4680
4753
  }
4681
- if (!pending.size) {
4682
- observer?.disconnect();
4683
- observer = void 0;
4684
- }
4685
4754
  };
4686
4755
  /**
4687
4756
  * Whether the parser may still be adding children (including a declarative shadow root)
@@ -4700,18 +4769,13 @@ const mayStillBeParsing = (elm) => win.document?.readyState === "loading" && (!e
4700
4769
  const deferUntilParsed = (elm) => {
4701
4770
  if (pending.has(elm)) return true;
4702
4771
  if (!win.document || !elm.hasAttribute("s-id")) return false;
4703
- const parsed = isParsed(elm);
4704
- if (parsed && !hasPendingAncestor(elm)) return false;
4705
- if (parsed) elm.__attachShadow?.();
4706
- pending.add(elm);
4707
- if (!observer) {
4708
- observer = new MutationObserver(flush);
4709
- observer.observe(win.document, {
4710
- childList: true,
4711
- subtree: true
4712
- });
4713
- win.document.addEventListener("DOMContentLoaded", flush, { once: true });
4714
- }
4772
+ const done = isParsed(elm);
4773
+ if (done && !hasPendingAncestor(elm)) return false;
4774
+ if (done) elm.__attachShadow?.();
4775
+ const observer = new MutationObserver(flush);
4776
+ observer.observe(elm, { childList: true });
4777
+ pending.set(elm, observer);
4778
+ win.document.addEventListener("DOMContentLoaded", flush, { once: true });
4715
4779
  return true;
4716
4780
  };
4717
4781
  //#endregion