fix(runtime): re-attach removed style elements#6646
fix(runtime): re-attach removed style elements#6646saudademjj wants to merge 1 commit intostenciljs:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses runtime style loss when a third-party framework removes Stencil-managed <style> tags from the DOM, by tracking the concrete style element per scope and re-attaching styles on subsequent renders. It also adds a regression test to ensure removed styles are re-inserted.
Changes:
- Track the actual
<style>element used perscopeIdto detect stale cache entries when style nodes are manually removed. - Clear the new tracked style-element cache on disconnect alongside the existing applied-style cache.
- Add a runtime regression test that removes a component’s style tag and verifies it is re-attached on the next render.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/runtime/styles.ts |
Adds tracking of per-scope style elements and re-attachment logic when cached styles are removed from the DOM. |
src/runtime/disconnected-callback.ts |
Clears the new per-scope style-element tracking cache on disconnect. |
src/runtime/test/style.spec.tsx |
Adds a regression test ensuring removed styles are re-attached on subsequent renders. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
d06da48 to
8b89e1f
Compare
There was a problem hiding this comment.
Pull request overview
This PR addresses a runtime issue where component styles can disappear if a third-party framework removes Stencil-injected <style> elements from the DOM, by tracking the actual style elements per scope and allowing styles to be re-attached on subsequent renders.
Changes:
- Track the concrete
<style>element associated with each applied scope to detect stale cache entries after DOM removal. - Clear the new style-element tracking cache alongside the existing applied-style cache during disconnect.
- Add runtime regression tests covering re-attaching removed styles and preserving slot-fallback CSS in shared shadow-root style containers.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/runtime/styles.ts |
Adds style-element tracking per scopeId, staleness detection, and consolidates slot-fallback CSS concatenation logic. |
src/runtime/disconnected-callback.ts |
Clears the new style-element WeakMap caches on disconnect (mirroring existing applied-style cleanup). |
src/runtime/test/style.spec.tsx |
Adds regression tests for re-attaching removed head styles and for shared shadow-root style container behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
This PR addresses a runtime edge case where Stencil-managed <style> elements can be manually removed from the DOM (e.g. by third-party frameworks) and then fail to reappear, by tracking the concrete style element used per scope so stale cache entries can be detected and corrected.
Changes:
- Track the concrete
<style>element per applied scope to detect when it has been removed from its container and re-attach/recreate it as needed. - Clear the new style-element tracking cache alongside the existing applied-style cache during
disconnectedCallback. - Add regression tests covering re-attachment in
document.headand shared shadow-root style containers (including slot fallback CSS).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/runtime/styles.ts | Adds per-scope style element tracking and re-attachment logic in addStyle. |
| src/runtime/disconnected-callback.ts | Clears the new rootAppliedStyleElms cache on disconnect (element + shadowRoot). |
| src/runtime/test/style.spec.tsx | Adds regression tests for re-attaching removed style elements in head and shadow roots. |
| src/declarations/stencil-private.ts | Updates runtime typings to allow ShadowRoot as a style container key. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
This PR addresses a runtime styling regression where component-generated <style> tags can be removed from the DOM by external frameworks (e.g., SvelteKit head management) and were not re-inserted on subsequent renders. It does so by tracking the actual style element instance used per scope and validating that the tracked element is still attached to the expected container.
Changes:
- Track concrete
<style>elements per applied scope (including shared shadow-root style containers) and re-attach or invalidate stale entries if the DOM node is removed. - Clear the new tracked-style-element cache alongside existing applied-style caches on disconnect.
- Add regression tests covering removed head
<style>tags and removed shared shadow-root style containers, plus tests for constructable stylesheet window selection.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/runtime/styles.ts |
Adds per-container tracking of applied style elements, stale-entry detection, re-attachment behavior, and a helper for shadow-root constructable stylesheets. |
src/runtime/disconnected-callback.ts |
Clears the new rootAppliedStyleElms cache on host/shadow-root disconnect to match existing cache cleanup patterns. |
src/runtime/test/style.spec.tsx |
Adds runtime regression tests ensuring removed style tags/containers are re-attached on subsequent renders. |
src/runtime/test/styles.spec.ts |
Adds unit tests for createShadowRootConstructableStylesheet to ensure the correct window is used (or safely falls back). |
src/declarations/stencil-private.ts |
Expands the applied-style container typing to include ShadowRoot to reflect runtime usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
This PR addresses a runtime robustness issue where component <style> tags can be removed from the DOM by external frameworks (e.g. SvelteKit head management) and not get re-inserted, causing styles to disappear until a full reload.
Changes:
- Track the concrete
HTMLStyleElementused perscopeIdso stale cache entries can be detected and styles can be re-attached when removed from the DOM. - Clear the new tracked style-element cache on disconnect alongside the existing applied-style cache.
- Add regression tests covering removed style re-attachment and shared shadow-root style-container behavior (including slot fallback styles).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/runtime/styles.ts | Tracks applied style elements per root container; reattaches removed styles; refactors constructable stylesheet creation. |
| src/runtime/disconnected-callback.ts | Clears the new rootAppliedStyleElms cache on host/shadowRoot disconnect. |
| src/runtime/test/style.spec.tsx | Adds runtime regression tests for reattaching removed styles and shared shadow-root style containers. |
| src/runtime/test/styles.spec.ts | Adds unit tests for createShadowRootConstructableStylesheet window selection/fallback behavior. |
| src/declarations/stencil-private.ts | Updates runtime typing for the applied-style root/container map. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
0ba7eab to
6bf65f4
Compare
6bf65f4 to
6931d06
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6931d06 to
f4ff1de
Compare
f4ff1de to
570c646
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
570c646 to
898fbfc
Compare
stenciljs#6637) Track style elements in rootAppliedStyles Map and verify DOM presence before skipping re-injection. Frameworks like SvelteKit may remove Stencil-injected <style> elements from <head> during hydration. - Change RootAppliedStyleMap from Set<string> to Map<string, HTMLStyleElement | null> - Verify tracked element parentNode before treating style as applied - null value indicates constructable stylesheet (cannot be externally removed) - Add regression test for style re-attachment after removal
898fbfc to
f4af543
Compare
|
Force-pushed a rewrite that strips this down to the minimum needed for #6637 — 3 files, +63/−15 (previously +528/−53). What changed
This keeps the hot path fast (one Map lookup + one property check) while catching the exact scenario described in #6637. Other details
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
<style>tagsslot-fbCSS and constructable stylesheet window selectionTesting
Closes #6637