Skip to content

Commit

Permalink
Pull request #34: AG-12906 Websites reloading continuously
Browse files Browse the repository at this point in the history
Merge in EXTENSIONS/disable-amp from AG-12906 to master

Squashed commit of the following:

commit bd6abd0
Author: Anton <[email protected]>
Date:   Thu Feb 24 11:04:55 2022 +0300

    AG-12906 fix review

commit 3094fbe
Author: Anton <[email protected]>
Date:   Tue Feb 22 19:38:20 2022 +0300

    AG-12906 session storage item to number

commit 3456f5a
Author: Anton <[email protected]>
Date:   Tue Feb 22 19:00:58 2022 +0300

    AG-12906 fix review

commit f898721
Author: Anton <[email protected]>
Date:   Tue Feb 22 18:17:28 2022 +0300

    session storage check

commit b7dd04e
Author: Anton <[email protected]>
Date:   Mon Feb 21 16:11:56 2022 +0300

    AG-12906 Websites reloading continuously
  • Loading branch information
Anton Kaprielov committed Feb 28, 2022
1 parent 6ee2dc6 commit 0e3c787
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/google-amp.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ const hideAmpIcon = (amp) => {
* Redirects amp version to normal
*/
export const ampRedirect = () => {
// Prevent redirecting to another page if run in an iframe
if (window.self !== window.top) {
const DISABLE_AMP_REDIRECTED = '__disable_amp_redirected';
// timeout to prevent automatic redirects to amp
const REDIRECT_WAIT_TIME_OUT_MS = 30000;
const disableAmpRedirectedDate = Number(sessionStorage.getItem(DISABLE_AMP_REDIRECTED));
if (
// Prevent redirecting to another page if run in an iframe
window.self !== window.top
// Do not redirect if since last redirect past less than REDIRECT_WAIT_TIME_OUT_MS
|| (disableAmpRedirectedDate
&& Date.now() - disableAmpRedirectedDate < REDIRECT_WAIT_TIME_OUT_MS)
) {
return;
}
const canonicalLink = document.querySelector('head > link[rel="canonical"]');
const ampProjectScript = document.querySelector('head > script[src^="https://cdn.ampproject.org"]');
if (ampProjectScript && canonicalLink && URL_PATTERN_REGEX.test(canonicalLink.href)) {
sessionStorage.setItem(DISABLE_AMP_REDIRECTED, Date.now());
window.top.location.href = canonicalLink.href;
}
};
Expand Down

0 comments on commit 0e3c787

Please sign in to comment.