stenciljs/core

bug: Styles not rendered, gone from the DOM due to 3rd party framework interference (svelte/sveltekit)

Open

#6,637 opened on Mar 12, 2026

View on GitHub
 (1 comment) (0 reactions) (0 assignees)TypeScript (844 forks)auto 404
Help Wanted

Repository metrics

Stars
 (13,111 stars)
PR merge metrics
 (PR metrics pending)

Description

Prerequisites

Stencil Version

4.22.2

Current Behavior

When having a component, that's having styles that are not scoped and component is not-shadow-dom:

@Component({
  tag: 'foobar',
  scoped: false,
  styleUrl: 'foobar.scss',
})

Then Stencil injects styles into <head> element after <link rel="preload"> elements which is implemented here: https://github.com/stenciljs/core/blob/main/src/runtime/styles.ts#L117

So in my (large and complex sveltekit) application i witnessed svelte doing heavy changes within <head>, in particular removing (previously) injected nodes

The problem is that - when svelte removes nodes that it added, it doesn't verify if the nodes actually belong to svelte components, their imported modules etc, it just removes everything between 2 nodes. Including <style> nodes of our component. So stencil owned styles - are removed. They are not added back after the fact.

I believe the responsible line is here: https://github.com/stenciljs/core/blob/main/src/runtime/styles.ts#L89 it checks if style has already been added. It's good to have this kind of check, however the check should also verify if the element exists in the DOM tree, not only if it was added to an internal list.

Expected Behavior

On component insertion, style presence should be checked in real DOM (not only in an internal list), to avoid anything external meddling with the DOM nodes. It would be handy to configure where styles should be injected, but i don't imagine any reasonable way of doing so.

System Info

irrelevant

Steps to Reproduce

I'll prepare a demo in some time and link it here.

On my end i gonna verify if setting scoped styles fixes my case (shadow dom should help, but i cant use it), and if not - will propose a fix.

Code Reproduction URL

TBD

Additional Information

https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/internal/client/reactivity/effects.js#L570 is my culprit

Contributor guide