How to make login / logout links cache independent?

Solution from:

https://stackoverflow.com/questions/48511579/jquery-check-wordpress-logged-in-cookie

was a dead end. It does not work with WooCommerce, because login is not done through standard WordPress login screen, but dedicated login form and there are no cookies accessible through JavaScript created when user logs in.

I created a working solution through jQuery Ajax, though I’m not happy with result, because links appear with some delay. At least they show up correctly for cached pages and user is not confused.

Then I sought for something both reliable and fast, what I perfectly found in this answer by Janos Szabo which shows how to create and delete script-accessible cookies on login and logout:

Check if user is logged in using JQuery

For anyone interested here is the code of my Ajax solution:

Contents of file “taisho.js” in “js” folder of child theme:

jQuery.ajax(
{
    type: "post",
    url: my_ajax_object.ajax_url,
    data: {
        action: 'is_user_logged_in'
    },
    success: function(response){        
        var menu_links = response == 'yes' ? logged : anonymous;
        $('nav.secondary-navigation').html('<ul id="menu-topmenu" class="menu">' + menu_links + '</ul>');       
    }   
});

Contents of my functions.php file:

// Store login and my account + logout links as js variables for conditional display, to be used in js file.
add_action('wp_footer', 'taisho_login_logout');
function taisho_login_logout() {
    ?>
    <script type="text/javascript">

        var anonymous = <?php echo json_encode(
            '<li><a href="' . get_permalink( woocommerce_get_page_id( 'myaccount' ) ) . '">Log in</a></li>'
            )?>;
        var logged = <?php echo json_encode(
            '<li><a href="'. get_permalink( woocommerce_get_page_id( 'myaccount' )) . '">My account</a></li>
            <li><a href="'. wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) . '">Log out</a>               </li>'
            )?>;    

    </script>
    <?php
}

// Enqueue js file residing in current (parent or child) theme and enable Ajax url in this file through wp_localize_script.
add_action( 'wp_enqueue_scripts', 'taisho_login_check' );
function taisho_login_check() {
    wp_enqueue_script( 'taisho-login-check', get_stylesheet_directory_uri() . '/js/taisho.js', array('jquery'));
    wp_localize_script( 'taisho-login-check', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

// Check if user is logged in through Ajax.
add_action('wp_ajax_is_user_logged_in', 'ajax_check_user_logged_in');
add_action('wp_ajax_nopriv_is_user_logged_in', 'ajax_check_user_logged_in');
function ajax_check_user_logged_in() {
    echo is_user_logged_in()?'yes':'no';
    die();
}