From 20dd4e6800a51878e103a12d837c7d1afaffea1e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 11 Jan 2021 12:18:12 -0800 Subject: [PATCH 1/2] Prevent infinite mobile redirect when non-AMP page served as AMP --- assets/src/mobile-redirection.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/assets/src/mobile-redirection.js b/assets/src/mobile-redirection.js index 63ed99f3860..1d3a762af02 100644 --- a/assets/src/mobile-redirection.js +++ b/assets/src/mobile-redirection.js @@ -58,14 +58,17 @@ if ( url.searchParams.has( noampQueryVarName ) && noampQueryVarValue === url.searchParams.get( noampQueryVarName ) ) { // If the noamp query param is present, remember that redirection should be disabled. sessionStorage.setItem( disabledStorageKey, '1' ); - } else { + } else if ( ampUrl !== location.href ) { // Otherwise, since JS is running then we know it's not an AMP page and we need to redirect to the AMP version. + // Nevertheless, the `url.href !== location.href` condition was added for the edge case where a caching plugin + // is erroneously serving a cached non-AMP page at the AMP URL, so the condition prevents an infinite redirect + // from ensuing. See . window.stop(); // Stop loading the page! This should cancel all loading resources. // Replace the current page with the AMP version. location.replace( ampUrl ); } }( - // Note: The argument here is replaced with JSON in PHP by \AmpProject\AmpWP\MobileRedirection::add_mobile_redirect_script(). + // Note: The argument here is replaced with a JSON object literal in PHP by \AmpProject\AmpWP\MobileRedirection::add_mobile_redirect_script(). AMP_MOBILE_REDIRECTION, ) ); From 2861dff940bdc6024d9c9478bcdd071941eff91d Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 11 Jan 2021 12:32:22 -0800 Subject: [PATCH 2/2] Persist URL fragment through client-side mobile redirection --- assets/src/mobile-redirection.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/assets/src/mobile-redirection.js b/assets/src/mobile-redirection.js index 1d3a762af02..ba833e45e10 100644 --- a/assets/src/mobile-redirection.js +++ b/assets/src/mobile-redirection.js @@ -53,12 +53,17 @@ return; } - const url = new URL( location.href ); + const locationUrlObject = new URL( location.href ); + const amphtmlUrlObject = new URL( ampUrl ); - if ( url.searchParams.has( noampQueryVarName ) && noampQueryVarValue === url.searchParams.get( noampQueryVarName ) ) { + // Persist the URL fragment when redirecting to the AMP version. This is needed because the server-generated amphtml + // link has no awareness of the client-side URL target. + amphtmlUrlObject.hash = locationUrlObject.hash; + + if ( locationUrlObject.searchParams.has( noampQueryVarName ) && noampQueryVarValue === locationUrlObject.searchParams.get( noampQueryVarName ) ) { // If the noamp query param is present, remember that redirection should be disabled. sessionStorage.setItem( disabledStorageKey, '1' ); - } else if ( ampUrl !== location.href ) { + } else if ( amphtmlUrlObject.href !== locationUrlObject.href ) { // Otherwise, since JS is running then we know it's not an AMP page and we need to redirect to the AMP version. // Nevertheless, the `url.href !== location.href` condition was added for the edge case where a caching plugin // is erroneously serving a cached non-AMP page at the AMP URL, so the condition prevents an infinite redirect @@ -66,7 +71,7 @@ window.stop(); // Stop loading the page! This should cancel all loading resources. // Replace the current page with the AMP version. - location.replace( ampUrl ); + location.replace( amphtmlUrlObject.href ); } }( // Note: The argument here is replaced with a JSON object literal in PHP by \AmpProject\AmpWP\MobileRedirection::add_mobile_redirect_script().