To ensure that your event listeners work properly when navigating back to a page using Astro's View Transitions, you should use the astro:page-load event. This event is fired after a page completes loading, whether from a navigation using view transitions or native to the browser.
Here's how you can modify your code to make it work with View Transitions:
document.addEventListener('astro:page-load', () => {
const inputs = document.querySelectorAll('input')
const handleChange = debounce(post(inputs), 1000)
inputs.forEach((input) => {
input.addEventListener('input', handleChange)
})
})
By wrapping your code in an astro:page-load event listener, you ensure that the event listeners are set up every time the page loads, including after View Transitions.
It's important to note that when using View Transitions, some scripts may no longer re-run after page navigation like they do with full-page browser refreshes. The astro:page-load event is specifically designed to address this issue.
View Transitions guide provides more information on this event:
"When view transitions is enabled on the page, code that would normally execute on DOMContentLoaded should be changed to execute on this event."
This approach ensures that your event listeners are properly added and removed during View Transitions, maintaining the functionality of your interactive elements across different views.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: