AMP Is Ready to Own Your Entire Front End
Companion demo: Repository | Live app
When I first started dabbling with AMP, I thought it was all about speed and little gray lightning bolts. Those struck me as completely worthwhile goals, but the rest of my team was skeptical: Isn’t AMP limiting? Doesn’t it block JavaScript? Can you even do CSS? What about our ads? Our branding? How do you wrangle a CMS into producing AMP-friendly output?
I didn’t know the answer to all of those questions. But the AMP demo looked better than most of our sites did, so I was optimistic that we could cross those bridges in due time. Given the payoff of insanely fast page loads for mobile users accessing our articles via SERPs, I felt it was worth at least a prototype.
It turns out I was right. The AMP version of our blogs looks similar to the non-AMP version; probably a bit better. JavaScript is allowed. So is CSS. So are ads, forms, analytics, performance monitoring—our AMP version wants for nothing.
On my next project, a thought occurred to me. If nobody minded, I’d just as soon build the whole site as one singular AMP version, rather than have the “normal” front end, paired with an AMP version. Recently the AMP team released a rather hilarious video that confirmed my suspicion: AMP can, and maybe should, power your entire front end.
I can’t say I’ve had the chance to implement that approach all the way through to a launch yet, but I intend to, and I’ll do so with the same confidence with which I approach any other project. Allow me to rebut some common objections to this.
Design
Most people are aware that AMP can render attractive combinations of HTML and CSS, but there are lingering concerns about certain CSS properties and values being unsupported. I found that this is simply not the case. Responsive web design is supported in full, as are web fonts, icon fonts, SVG, full-page background images—probably anything you’re used to using.
CSS
One of the first roadblocks I hit was when I attempted to load my normal front-end CSS into AMP: There’s an 85 kB limit on CSS and somehow over the years, our stylesheet had grown to about 270 kB. At first, I found this really discouraging, but it caused me to seriously consider the merits of utility-first CSS. Bootstrap and Tailwind are good examples of this, where you give an element a class of m-5
(or similar) instead of writing .thing { margin: 2em; }
in your stylesheet. This approach allowed me to achieve design parity in only 42 kB, and I now believe it’s a better way to work, regardless.
JavaScript
It’s true that AMP does place some restrictions on the type of JavaScript you can run, and the sources from which you can load it. For this, I am thankful, as JavaScript dependencies tend to be the most fragile part of my projects. And actually, you can do a lot more with JS in AMP than I expected. AMP has helpful modules for common UI elements like show/hides, AJAX form submits, validation, instant content updates, and more.
There’s also amp-script, which allows you to execute nearly any arbitrary JavaScript, but scoped within the opening and closing <amp-script>
tags.
State
It’s easy to assume that AMP is just for news sites: Clearly modern tools like data binding and JS state management are out of bounds… right? Wrong! AMP stands at the ready to bind DOM interactions with a global state object, thanks to amp-bind. In fact, the AMP project was the first platinum donor to the Preact library, suggesting that it intends to only increase feature parity with the leading JS frameworks.
Scope
Can AMP do everything that you could possibly do on a non-AMP site? No, of course not, by definition. But speaking more practically, it can deliver the 90% of the features you actually care about, which probably does you a favor by preventing your app from being an edge case of whatever framework it might otherwise use.
AMP as a Front-end Framework: A Surprisingly Effective Approach
It’s more abstraction than you normally prefer. It’s a bit less flexible. Guess what? Abstraction is not inherently bad. Flexibility is not inherently good. There is an appropriate level of each, and I believe AMP delivers it for most projects.
Plus, it delivers something that no other framework can: Google’s CDN, their SERP pre-fetching, and whatever SEO benefits they choose to convey beyond the attractive gray lightning bolt. A valuable ally for any site to gain!
Contributors
Scott Fennell
Scott has written hundreds of WordPress themes and plugins. He specializes in third-party API integrations including Twitter, MailChimp, CloudFlare, Bitbucket, and Shopify. He's an experienced public speaker, having presented at WordCamp Portland and also the Google campus in Seattle. He is active as a technology writer, having been published on A List Apart and CSS-Tricks many times. Scott is also a former infantry officer in the US Army.
Show More