Skip to content

Commit 051522f

Browse files
fix(runtime): double check hostRef value (#6341)
1 parent 6106c70 commit 051522f

13 files changed

+45
-17
lines changed

src/hydrate/platform/hydrate-app.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ async function hydrateComponent(
205205
if (cmpMeta != null) {
206206
waitingElements.add(elm);
207207
const hostRef = getHostRef(this);
208+
if (!hostRef) {
209+
return;
210+
}
208211
addHostEventListeners(this, hostRef, cmpMeta.$listeners$, false);
209212

210213
try {
@@ -214,7 +217,7 @@ async function hydrateComponent(
214217
results.hydratedCount++;
215218

216219
const ref = getHostRef(elm);
217-
const modeName = !ref.$modeName$ ? '$' : ref.$modeName$;
220+
const modeName = !ref?.$modeName$ ? '$' : ref?.$modeName$;
218221
if (!results.components.some((c) => c.tag === tagName && c.mode === modeName)) {
219222
results.components.push({
220223
tag: tagName,

src/hydrate/platform/proxy-host-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function proxyHostElement(elm: d.HostElement, cstr: d.ComponentConstructo
8787
get: function (this: any) {
8888
const ref = getHostRef(this);
8989
// incoming value from a attr / prop?
90-
const attrPropVal = ref.$instanceValues$?.get(memberName);
90+
const attrPropVal = ref?.$instanceValues$?.get(memberName);
9191

9292
if (origGetter && attrPropVal === undefined && !getValue(this, memberName)) {
9393
// if the initial value comes from an instance getter

src/runtime/bootstrap-custom-element.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export const proxyCustomElement = (Cstr: any, compactMeta: d.ComponentRuntimeMet
7777
connectedCallback() {
7878
if (!this.__hasHostListenerAttached) {
7979
const hostRef = getHostRef(this);
80+
if (!hostRef) {
81+
return;
82+
}
8083
addHostEventListeners(this, hostRef, cmpMeta.$listeners$, false);
8184
this.__hasHostListenerAttached = true;
8285
}
@@ -121,7 +124,7 @@ export const forceModeUpdate = (elm: d.RenderNode) => {
121124
const mode = computeMode(elm);
122125
const hostRef = getHostRef(elm);
123126

124-
if (hostRef.$modeName$ !== mode) {
127+
if (hostRef && hostRef.$modeName$ !== mode) {
125128
const cmpMeta = hostRef.$cmpMeta$;
126129
const oldScopeId = elm['s-sc'];
127130
const scopeId = getScopeId(cmpMeta, mode);

src/runtime/bootstrap-lazy.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ export const bootstrapLazy = (lazyBundles: d.LazyBundlesRuntimeData, options: d.
136136

137137
connectedCallback() {
138138
const hostRef = getHostRef(this);
139+
if (!hostRef) {
140+
return;
141+
}
139142

140143
/**
141144
* The `connectedCallback` lifecycle event can potentially be fired multiple times
@@ -174,6 +177,9 @@ export const bootstrapLazy = (lazyBundles: d.LazyBundlesRuntimeData, options: d.
174177
*/
175178
plt.raf(() => {
176179
const hostRef = getHostRef(this);
180+
if (!hostRef) {
181+
return;
182+
}
177183
const i = deferredConnectedCallbacks.findIndex((host) => host === this);
178184
if (i > -1) {
179185
deferredConnectedCallbacks.splice(i, 1);
@@ -185,7 +191,7 @@ export const bootstrapLazy = (lazyBundles: d.LazyBundlesRuntimeData, options: d.
185191
}
186192

187193
componentOnReady() {
188-
return getHostRef(this).$onReadyPromise$;
194+
return getHostRef(this)?.$onReadyPromise$;
189195
}
190196
};
191197

src/runtime/client-hydrate.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,16 @@ export const initializeClientHydrate = (
138138
// if this child is a non-shadow component being added to a shadowDOM,
139139
// let's find and add its styles to the shadowRoot, so we don't get a visual flicker
140140
const cmpMeta = getHostRef(childRenderNode.$elm$);
141-
const scopeId = getScopeId(
142-
cmpMeta.$cmpMeta$,
143-
BUILD.mode ? childRenderNode.$elm$.getAttribute('s-mode') : undefined,
144-
);
145-
const styleSheet = win.document.querySelector(`style[sty-id="${scopeId}"]`);
141+
if (cmpMeta) {
142+
const scopeId = getScopeId(
143+
cmpMeta.$cmpMeta$,
144+
BUILD.mode ? childRenderNode.$elm$.getAttribute('s-mode') : undefined,
145+
);
146+
const styleSheet = win.document.querySelector(`style[sty-id="${scopeId}"]`);
146147

147-
if (styleSheet) {
148-
hostElm.shadowRoot.append(styleSheet.cloneNode(true));
148+
if (styleSheet) {
149+
hostElm.shadowRoot.append(styleSheet.cloneNode(true));
150+
}
149151
}
150152
}
151153

src/runtime/connected-callback.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { insertBefore } from './vdom/vdom-render';
1414
export const connectedCallback = (elm: d.HostElement) => {
1515
if ((plt.$flags$ & PLATFORM_FLAGS.isTmpDisconnected) === 0) {
1616
const hostRef = getHostRef(elm);
17+
if (!hostRef) {
18+
return;
19+
}
20+
1721
const cmpMeta = hostRef.$cmpMeta$;
1822
const endConnected = createTime('connectedCallback', cmpMeta.$tagName$);
1923

src/runtime/disconnected-callback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const disconnectedCallback = async (elm: d.HostElement) => {
1717
const hostRef = getHostRef(elm);
1818

1919
if (BUILD.hostListener) {
20-
if (hostRef.$rmListeners$) {
20+
if (hostRef?.$rmListeners$) {
2121
hostRef.$rmListeners$.map((rmListener) => rmListener());
2222
hostRef.$rmListeners$ = undefined;
2323
}

src/runtime/element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import { getHostRef } from '@platform';
33

44
import type * as d from '../declarations';
55

6-
export const getElement = (ref: any) => (BUILD.lazyLoad ? getHostRef(ref).$hostElement$ : (ref as d.HostElement));
6+
export const getElement = (ref: any) => (BUILD.lazyLoad ? getHostRef(ref)?.$hostElement$ : (ref as d.HostElement));

src/runtime/hmr-component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import { initializeComponent } from './initialize-component';
2121
export const hmrStart = (hostElement: d.HostElement, cmpMeta: d.ComponentRuntimeMeta, hmrVersionId: string) => {
2222
// ¯\_(ツ)_/¯
2323
const hostRef = getHostRef(hostElement);
24+
if (!hostRef) {
25+
return;
26+
}
2427

2528
// reset state flags to only have been connected
2629
hostRef.$flags$ = HOST_FLAGS.hasConnected;

src/runtime/mode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export const computeMode = (elm: d.HostElement) => modeResolutionChain.map((h) =
77

88
// Public
99
export const setMode = (handler: d.ResolutionHandler) => modeResolutionChain.push(handler);
10-
export const getMode = (ref: d.RuntimeRef) => getHostRef(ref).$modeName$;
10+
export const getMode = (ref: d.RuntimeRef) => getHostRef(ref)?.$modeName$;

0 commit comments

Comments
 (0)