Skip to content

Commit 6502a8c

Browse files
authored
fix(handle-undefined-datastack) (#299)
* fix(avoid-crash-on-undefined-datastack) * add example + log warning in devmode, can't reproduce
1 parent e31f2ac commit 6502a8c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/shell-chrome/src/backend.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,19 @@ function init() {
7070

7171
getAlpineDataInstance(node) {
7272
if (this.isV3) {
73-
return node._x_dataStack[0]
73+
return node._x_dataStack?.[0]
7474
}
7575
return node.__x
7676
}
7777

7878
getReadOnlyAlpineData(node) {
7979
const alpineDataInstance = this.getAlpineDataInstance(node)
80+
if (!alpineDataInstance) {
81+
if (process.env.NODE_ENV !== 'production') {
82+
console.warn('element has no dataStack', node)
83+
}
84+
return
85+
}
8086
if (this.isV3) {
8187
// in v3 magics are registered on the data stack
8288
return Object.fromEntries(Object.entries(alpineDataInstance).filter(([key]) => !key.startsWith('$')))
@@ -276,11 +282,15 @@ function init() {
276282

277283
sendComponentData(componentId, componentRoot) {
278284
const componentData = this.getReadOnlyAlpineData(componentRoot)
285+
286+
if (!componentData) return
287+
279288
const data = Object.entries(componentData).reduce((acc, [key, value]) => {
280289
acc[key] = serializeDataProperty(value)
281290

282291
return acc
283292
}, {})
293+
284294
this._postMessage({
285295
type: BACKEND_TO_PANEL_MESSAGES.SET_DATA,
286296
componentId,

packages/simulator/v3.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,17 @@
121121
<div>
122122
<a data-testid="navigation-target" href="/navigation-target.html">Go to next page</a>
123123
</div>
124+
125+
<h3>Manual test</h3>
126+
<p>
127+
Can't reproduce
128+
<a href="https://github.com/alpinejs/alpine/discussions/1952"
129+
>https://github.com/alpinejs/alpine/discussions/1952</a
130+
>
131+
</p>
132+
<div x-data @focus-input.window="$refs.searchInput.focus()">
133+
<input x-ref="searchInput" />
134+
</div>
135+
<button onclick="document.dispatchEvent(new CustomEvent('focus-input'))">Click</button>
124136
</body>
125137
</html>

0 commit comments

Comments
 (0)