Is the logic of this code correct and consistent with astro?
<script>
import { navigate } from "astro:transitions/client";
document.addEventListener("astro:page-load", () => {
// If the user tries to access from a page other than a form
if (!history.state?.fromFrom) {
navigate("/", { history: "replace" });
}
// Handle pop state (redirect to home)
const onPopState = () => {
navigate("/", { history: "replace" });
}
// Add popstate event listener
window.addEventListener("popstate", onPopState);
// Popstate event listener cleanup
document.addEventListener("astro:before-swap", () => {
window.removeEventListener("popstate", onPopState);
})
});
</script>