diff --git a/resources/js/search.js b/resources/js/search.js index 97267e40..7342cb5a 100644 --- a/resources/js/search.js +++ b/resources/js/search.js @@ -43,8 +43,11 @@ document.addEventListener('alpine:init', () => { this.compact = query.matches query.addEventListener('change', (event) => (this.compact = event.matches)) - this.observer = new MutationObserver(() => this.countLater()) - this.observer.observe(this.$refs.view, { childList: true, subtree: true, characterData: true }) + // Alpine registers x-ref as it walks the children, which is after this runs. + this.$nextTick(() => { + this.observer = new MutationObserver(() => this.countLater()) + this.observer.observe(this.$refs.view, { childList: true, subtree: true, characterData: true }) + }) this.$watch('open', () => this.countLater()) }, @@ -60,7 +63,7 @@ document.addEventListener('alpine:init', () => { }, count() { - if (! this.open) { + if (! this.open || ! this.$refs.view) { this.announcement = '' return