Google AMP Aftermath: Replacement Image Results

After implementing changes with modifying images according to Google's requirements, I show the results of when I implemented Google AMP pages.

Written by Jonathan "JD" Danylko • Last Updated: • MVC •

Laptop displaying Google Search Engine

About a month ago, I implemented an ActionFilter to modify images in a post to match Google's AMP requirements.

Today, I give you an update of the results, but before I give you the results, I want to explain how much work went into this particular project.

The amount of work put into this was merely a new controller and a new ActionFilter called UseAmpImage.

At first, I tried DisplayModes, but that didn't seem to work because of the caching.

So I decided to create a brand new controller called Amp to output a plain post as per Google's request.

Once implemented, I just had to wait to find out whether everything was functional or not.

The Results

After a month, I decided to check the statistics on Google's Webmaster Tools...err...I mean, Google's Search Console.

With the minimal amount of effort (a controller and an ActionFilter), I was able to update my site to use AMP pages.

Finally, here are the results of my efforts.

Graph from Google's Search Console describing the results of implementing Google AMP pages

Excellent! I was extremely happy with this.

However, what about those 20 pages? Call me a perfectionist, but I had to figure out what was up with those remaining pages.

Fixing an Issue

After looking over the 20 pages that weren't working, I noticed something weird with each post.

Every time I requested one of the 20 pages, I noticed I didn't get ANY HTML back. It was a blank page.

What the ....?????

I went back through the code and everything seemed alright, but then I noticed each post didn't have an image anywhere in the post. It was just text with hyperlinks.

It seems I needed to add a couple lines of code. Here is the updated UpdateAmpImages method.

private string UpdateAmpImages(string response)
{
    // Use HtmlAgilityPack (install-package HtmlAgilityPack)
    var doc = GetHtmlDocument(response);
    var imageList = doc.DocumentNode.Descendants("img");

    const string ampImage = "amp-img";
    if (!imageList.Any()) return response;
    if (!HtmlNode.ElementsFlags.ContainsKey("amp-img"))     {         HtmlNode.ElementsFlags.Add("amp-img", HtmlElementFlag.Closed);     }
    foreach (var imgTag in imageList)     {         var original = imgTag.OuterHtml;         var replacement = imgTag.Clone();         replacement.Name = ampImage;         response = response.Replace(original, replacement.OuterHtml);     }
    return response; }

Notice after the string constant? If we don't have any images in the post, just return the response.

UPDATE (2016-05-01): Thanks to Elmah for letting me know about issues with my AMP pages. I repeatedly kept adding "amp-img" to the ElementFlags Dictionary and it error'd out without giving me an error message. NOW, it's fixed.

Conclusion

I uploaded the change and so far, it seems to be working.

I will probably give it another two weeks before I examine what Google detects in my code, but this is far from over.

I guarantee I will have more errors to fix, but it seems this is a case of fixing one set of errors will result in a ripple across and fix the next set of errors.

Stay tuned.

Have you implemented Google AMP pages yet? Are you having any luck? Post your comments below.

 

The Complete Guide To Google AMP with ASP.NET MVC

Did you like this content? Show your support by buying me a coffee.

Buy me a coffee  Buy me a coffee
Picture of Jonathan "JD" Danylko

Jonathan Danylko is a web architect and entrepreneur who's been programming for over 25 years. He's developed websites for small, medium, and Fortune 500 companies since 1996.

He currently works at Insight Enterprises as an Principal Software Engineer Architect.

When asked what he likes to do in his spare time, he replies, "I like to write and I like to code. I also like to write about code."

comments powered by Disqus