Susanta Kumar Sahoo

Personal Blog

User-authored JavaScript found on Page: Fix Google AMP Error

Recently, I got the following warning in Google Search Console:

User-authored JavaScript found on page (Critical)

Remove any custom JavaScript code from your page. AMP pages with custom JavaScript may appear in Google search results without AMP-specific display features.

When you’re on the Google Console Dashboard, the screenshot might look some like this:

Fix errors on your AMP pages

AND, Here’s a screenshot of the warning.

AMP JavaScript Warning in Google Search Console

When I clicked on one of the affected AMP pages, I could see the following window with details of the error message.

How to Remove any custom JavaScript code from your page

Although I could have easily ignored this warning but I hate seeing errors. So, I decided to dig in to figure out what might have triggered the errors.

If you are seeing similar issues, read on to know how to fix it.

Run AMP Validator

Although the warning already says your AMP page has some custom JavaScript, you need to run the AMP validator to establish the details of the issue and where exactly it appears in your page source.

Therefore, you need to go to the AMP Validator tool and enter the URL of the AMP page and hit on the “Validate” button.

Note: The AMP Validator is different from AMP Test tool which also tests if your AMP page is valid. It’s important to keep this in mind because the AMP test tool will still say your page is valid (because you’ve used AMP markup) while the AMP validator tool won’t (due to the user authored javascript).

The validator tool showed me the following error on the 27th line.

<script>if (document.location.protocol != “https:”) {document.location = document.URL.replace(/^http:/i, “https:”);}</script><script type=’text/javascript’ src=’https://cdn.ampproject.org/v0.js’ async></script>

https://gist.github.com/topleague/fa0e726f52a3655cb52681972a134bcc

Please, note that the user authored javascript issue could be different for different websites. Therefore, you may not see this exact code in your validation test. However, if you did, you’re in luck because I’m going to tell you how to fix it.

In my case, I wasn’t sure where to find such javascript snippet to fix the error.

A couple of random searches led me to get suspicious about plugins. But how would I know which plugin was responsible? Another thread blamed it on the custom javascript in the footer. But, I found no javascript in my footer.

Therefore, I turned to Google Search to find the solution.

I used this search phrase to see if anybody has faced a similar issue: “document.location.protocol + AMP”

Luckily, I found this thread in WordPress Support.

Uncheck Javascript Redirection in SSL Plugin

I’ve been using Really Simple SSL plugin ever since I configured my site with SSL certificate. The plugin never really created any issues early on or maybe, I never noticed it.

The document.location.protocol you see in the AMP Validator tool is triggered by this SSL plugin. Therefore, you need to go to the settings and uncheck javascript redirection as shown in the screenshot below.

User-authored JavaScript found on page

Click on the save button and you’re done!

Go back to the AMP Validator tool and revalidate the affected page(s) and you should no longer any error.

That’s it!

Just wait for a couple of days until Google bots recrawl and reindex your AMP pages.

Update:

Here’s an updated report appearing on the 19th June, 2018, just 9 days after I fixed the errors. As you can see, the number of affected pages has automatically gone down from 41 to 23. At this rate, I’m assuming it might take one more week for Google Search to index to rest of the pages.

AMP Issues in Google Search Console (Resolved)

Hope you find this post helpful!