Discussion: Using AMP library in non-AMP pages
This discussion has come up more casually before, but I wanted to raise the idea here to think through it a bit (may warrant a discussion in the weekly review).
In short: is there a way to support usage of the AMP library in the context of non-AMP pages?
The use-case that came up recently is that some developers create their AMP page first, and then build canonical equivalents around those pages to fill in features that they want but aren't supported in AMP. It would significantly lower the cost of implementation to have a path where they just add this functionality around the AMP implementation, rather than re-writing as non-AMP.
My assumption is that this usage of AMP is not supported, in part because the resource management and presentation logic built into AMP will affect all elements on the page, and it would be hard to predict how additional non-AMP functionality would work in conjunction.
However — is there some possible workaround? A "sandbox" for AMP content inside non-AMP contexts that doesn't just rely on an iframe? Or rules of thumb for using AMP in non-AMP contexts that will avoid some of these issues?
/cc @cramforce @dvoytenko
This is supported in the PWAMP combo using well defined primitives.
I'm experimenting with this at the moment. Would be good to discuss at weekly review
Thanks @cramforce — that makes total sense (I should have mentioned PWA route in the framing).
@bjalford — would be curious to hear more: are you experimenting with AMP + PWA?
@ericlindley-g PWAMP is the main approach for now. We are actively exploring use of AMP library on a non-AMP page, but there's no telling at this time how well that would work. I'd strongly recommend PWAMP at this time.
Just came across this discussion and had one other use case, I have been implementing a paywall/advert free version of our AMP site and wondered if there is any reason why I couldn't load the AMP JS elements that are used to implement the amp-access elements into my non AMP site.
When I suggested it internally we thought we would try it and see and it doesn't seem to break, why is this a bad idea?
If utilising some specific AMP elements to perform specific tasks on non AMP pages makes development and maintenance easy and will work why should we not do it.
The discussion above has been slightly moved from its original question into the world of PWA, what I think was being asked is, is it OK to load the AMP elements on the non-AMP site and then add additional JS to do the extra things you are after.
Should it just work?
Is it not trying to be an AMP page but is taking advantage of tools already used and developed. In my example above it means that I can use the same JS loads and HTML code on both my AMP and HTML sites so it makes future maintenance easier.
@QES You can certainly use JS. But please keep in mind, AMP page has some assumptions on when and how content can change to prioritize resource scheduling and component phases. This system is getting stronger all the time, but there's no guarantee that we can make that these libraries will be reactive to a complete set of changes that could be executed by a 3p script. We may export some APIs, especially for PWAMP case, but we can't cover the whole set of DOM API variations at this time. For instance, if you use <amp-carousel> and you predefined an initial set of slides, a DOM API invocation such as ampCarousel.appendChild(newSlide) won't really add a new slide. Etc.
@dvoytenko so provided the JS elsewhere on the page doesn't break it, it will be fine.
That seems to be the problem with any added element of JS and something I can live with for consistent implementation (one set of tools - in this particular example I'm playing with AMP-ACCESS) across HTML and AMP.
I can certainly see how certain AMP JS elements might not be sensible or suitable to use but it strikes me that there are a subset of elements that perform tasks / functions that are not replacement of HTML elements but additional functionality like:
AMP-ACESSS AMP-LIVE-LIST etc
They are tools and elements that generally get additional elements from JSON feeds or or poll for content and there are other ways to do the same thing with JS on HTML sites but if one is building the solution for an AMP site that one could use the same JS and same HTML constructs on non AMP sites so there is less to maintain.
@QES I'd certainly also recommend looking into PWAMP (https://www.ampproject.org/docs/guides/pwa-amp). It's a nice way to keep using both tech stacks where they do not step on each other toes.
Hi @dvoytenko thanks, I have looked and implemented things with PWAMP, I suppose my point is that AMP has a set of building blocks that can be used and combined and all just work together.
PWA has various different ideas and projects which don't easily integrate and which need maintenance to keep them working.
From my perspective, PWA offers a number of potentially interesting tools for an AMP site but a level of complexity which is overbearing.
What I love about AMP is that it is a set of building blocks and I only need to load the ones I use and a simple set of rules. I'd like a similar set of blocks and rules for AMP so that anyone with an AMP web site could with just a few lines of code add PWA functionality for say Caching pages and off line Analytics, or Caching and Push Notifications etc
Conceptually PWAMP is great but the AMP side is much more complete and usable whereas the PWA side is a selection of different tools which don't necessarily go together and so are not a mix and match solution.
However, this original conversation was more about the ability to use AMP JS modules on non AMP pages which is a different issue and one where there are a very small number of use cases where there is an advantage to having one set of code to work with rather than two.
Yeah, I see your point. And we are actively looking at this, but the best response I have for now is still this one - i.e. certainly possible, but certain care should be taken.
/cc @pbakaus
I've been successful in gradually adding AMP components to a non-AMP page in order to convert it to AMP (in the end, the AMP page will be the canonical page).
I've started, back in March, by cleaning some of the CSS and using the amp-imgcomponent and since then I've gradually added, amp-analytics, amp-ad, amp-selector and other components. And eventually we got rid of all custom JavaScript.
The transition to HTTPS is the only part that caused some headaches, I've done some workarounds for amp-form to work (had to move "POST" enabled forms out of the way and onto their own page that did not have the AMP library).
Now that HTTPS is in place, the external style-sheets need to be moved into the header and then the amp attribute can be added to the HTML component on any page we like and we have a fully working AMP website.
After that, I'll have to make Drupal play nice with action-xhr for amp-form in order to move the "POST" forms back into their proper place and then the website is a fully working AMP website.
Great to hear the discussion so far on this—though now closing this particular issue given there's no distinct followup in the AMP library itself.