• Resolved enricobt

    (@enricobt)


    Hi,

    I would like to refresh the page once after loading even in AMP environment.

    I have seen that it can normally be done via a simple Java script like the following I found in this article:


    <script id = “reload” type = “text / plain” target = “amp-script”>
    window.onload = function () {
    if (! localStorage.justOnce) {
    localStorage.setItem (“justOnce”, “true”);
    window.location.reload ();
    }
    }
    </script>

    Then I read this article on using Java script within AMP, but I honestly don’t know how to exactly combine all the APM / HTML and Java pieces.
    Is there any chance of getting help on this matter? Perhaps a solution to a similar problem has already been solved in other posts?

    Greetings

    Enrico

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Milind More

    (@milindmore22)

    Hello @enricobt

    Thank you for the support topic, Can you please share the reason for reloading the AMP page?

    Also, we not sure how it might behave in the AMP cache or if the google search console might flag them as invalid AMP pages, we will recommend avoiding such practices.

    The amp-script runs your custom JavaScript in a Web Worker. Normally Web Workers don’t have access to the DOM, you can check this list of DOM elements and their support

    here is a code snippet that might help you

    Code will inject a script on the first load but it won’t be present once it’s reloaded.

    /**
     * Reload Once.
     */
    function check_cookie_and_reload() {
    
    	$reload_cookie = filter_input( INPUT_COOKIE, 'reload_cookie', FILTER_SANITIZE_STRING );
    
    	if ( ! $reload_cookie && function_exists( 'amp_is_request' ) && amp_is_request() ) {
    
    		setcookie( 'reload_cookie', '1', time() + ( 86400 * 30 ), '/' ); // 86400 = 1 day.
    		?>
    		<script id="reload" type="text/javascript" data-ampdevmode>
    				window.location.reload ();
    		</script>
    		<?php
    	}
    }
    
    add_action( 'wp_footer', 'check_cookie_and_reload' );
    Plugin Author Weston Ruter

    (@westonruter)

    This script will get removed if the user is not logged-in, however, since AMP dev mode won’t be enabled.

    Thread Starter enricobt

    (@enricobt)

    Hi Milind
    The reason why I need this script is that I’m using another Plugin, AMP Enacher, to show a POPUP (a newsletter registration form) when user open a post from our site.
    I found that POPUP is shown but conflicting with Cookie acceptance POPUP and I opened a ticket to AMP Encher support (the ticket is here)
    With the latest release 1.0.43 they fixed but, after user acceptance of cookies, the popup is not appearing if user doesn’t refresh the page or move to another page.
    You can try by yourself opening a post from magazine.lorenzovinci.it as this one for example (open in a private connection).
    So I didn’t have too many options now besides the code you sent me, but unfortunately, this script, as Weston wrote, is not working.
    Do you have any other suggestions?
    Regards
    Enrico

    • This reply was modified 3 years, 9 months ago by enricobt.
    • This reply was modified 3 years, 9 months ago by enricobt.
    Plugin Support Milind More

    (@milindmore22)

    Hello @enricobt

    I think we have hit the limitation here, I will recommend trying the WP GDPR Cookie Notice plugin instead of your cookie plugin.

    hopefully this will help you!

    Plugin Support Milind More

    (@milindmore22)

    As we didn’t receive a response I’ll mark this as resolved. Feel free to open a new support topic if you require any further assistance.

    Thread Starter enricobt

    (@enricobt)

    Hi Mild
    I apologize for the late reply.
    Unfortunately, I cannot replace the Iubenda plugin as it includes a full set of services, in addition to the plugin itself.
    It would definitely be better for me to replace the POPUP plugin, but there are no AMP-compatible alternatives I’ve found so far.
    Do you have any suggestions on this issue, such as another POPUP module which can also be used in AMP?
    Greetings
    Enrico

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Relaod once AMP pages’ is closed to new replies.