Skip to content

Commit 23f1e66

Browse files
authored
fix(runtime): initialize custom elements even when there is no styles (#4296)
* remove top-level check gating init logic based on build flags This commit removes a check that was gating some component initialization logic. This check would force that non-lazy builds would need to have styles (or at least an empty stylesheet) for the init logic to execute that would mark the component as initialized. This flag is necessary to have set `true` for watcher callbacks to execute * add small test to check initialized flag * prettier because I don't have it auto-format spec files 🤦‍♂️
1 parent acd5adf commit 23f1e66

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/runtime/initialize-component.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ export const initializeComponent = async (
1818
Cstr?: any
1919
) => {
2020
// initializeComponent
21-
if (
22-
(BUILD.lazyLoad || BUILD.hydrateServerSide || BUILD.style) &&
23-
(hostRef.$flags$ & HOST_FLAGS.hasInitializedComponent) === 0
24-
) {
25-
if (BUILD.lazyLoad || BUILD.hydrateClientSide) {
26-
// we haven't initialized this element yet
27-
hostRef.$flags$ |= HOST_FLAGS.hasInitializedComponent;
21+
if ((hostRef.$flags$ & HOST_FLAGS.hasInitializedComponent) === 0) {
22+
// Let the runtime know that the component has been initialized
23+
hostRef.$flags$ |= HOST_FLAGS.hasInitializedComponent;
2824

25+
if (BUILD.lazyLoad || BUILD.hydrateClientSide) {
2926
// lazy loaded components
3027
// request the component's implementation to be
3128
// wired up with the host element
@@ -81,7 +78,7 @@ export const initializeComponent = async (
8178
} else {
8279
// sync constructor component
8380
Cstr = elm.constructor as any;
84-
hostRef.$flags$ |= HOST_FLAGS.hasInitializedComponent;
81+
8582
// wait for the CustomElementRegistry to mark the component as ready before setting `isWatchReady`. Otherwise,
8683
// watchers may fire prematurely if `customElements.get()`/`customElements.whenDefined()` resolves _before_
8784
// Stencil has completed instantiating the component.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { getHostRef } from '@platform';
2+
import { Component } from '@stencil/core';
3+
import { newSpecPage } from '@stencil/core/testing';
4+
5+
import { HOST_FLAGS } from '../../utils';
6+
7+
describe('initialize component', () => {
8+
@Component({
9+
tag: 'cmp-a',
10+
})
11+
class CmpA {}
12+
13+
it('should mark the component as initialized', async () => {
14+
const page = await newSpecPage({
15+
components: [CmpA],
16+
html: `<cmp-a><cmp-a>`,
17+
});
18+
19+
const hostFlags = getHostRef(page.root).$flags$;
20+
expect(hostFlags & HOST_FLAGS.hasInitializedComponent).toBe(HOST_FLAGS.hasInitializedComponent);
21+
});
22+
});

src/runtime/test/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"baseUrl": ".",
2323
"paths": {
2424
"@stencil/core": ["../../index.ts"],
25-
"@stencil/core/testing": ["../../testing/index.ts"]
25+
"@stencil/core/testing": ["../../testing/index.ts"],
26+
"@platform": ["../../client/index.ts"]
2627
}
2728
}
2829
}

0 commit comments

Comments
 (0)