Repository navigation
Support ClientRouter navigation in the Search component (set up and tear down per element) #4421
Replies: 1 comment
|
The current behaviour is intended as we don’t support the It’s not really a very common need for documentations and introduces complexity to the code and increased client JS size which the vast majority of users won’t benefit from. That’s also why there’s no simple option for adding it. It's also not only the search, but same goes the sidebar persistence, mobile menu, and many others areas. Instead, we rely on a extra package dedicated to client-side routing (and also view transitions) that interested users can choose to opt-in when desired: 👜 Bag of Tricks ✨ ( It comes with a great guide on how to quickly set it up in a Starlight project, but also comes with many extra options and features to tweak the experience to your liking depending on if you're just looking for a SPA-like navigation or extend it further with custom view transitions. |
Uh oh!
There was an error while loading. Please reload this page.
What version of
starlightare you using?0.41.3
What is your idea?
Make the
Searchcomponent'ssite-searchelement set itself up and tear itself down per element instance, so it keeps working when a site uses Astro's<ClientRouter />(view transitions):connectedCallback, not the constructor or a one-timeDOMContentLoadedlistener. Register listeners with anAbortControllersignal.disconnectedCallback: abort those listeners, close the dialog if it's open, and callpagefindUI.destroy().#starlight__searchcontainer (this.querySelector(...)), not a document-wide selector, so the incoming page's instance never binds to the outgoing one's container.data-astro-rerunto the inline script that defines the element.#4337 (0.42.10) already handles the script running after
DOMContentLoaded. This covers the remaining case, where the element is removed and inserted again on a client-side navigation.Why is this feature necessary?
With
<ClientRouter />, the element's one-time listeners onwindowand the Pagefind instance outlive the page that created them. After a navigation the search button can open a dialog from the old page, keyboard shortcuts fire twice, and Pagefind keeps state from the detached DOM. aspire.dev ships this as a localbun patchofcomponents/Search.astro.Do you have examples of this feature in other projects?
Custom elements that hold global listeners commonly pair
connectedCallback/disconnectedCallbackwith anAbortController, and Pagefind UI exposesdestroy()for this.Participation
All reactions