Ensure beforeLeavePrevious only runs on client