AMP Shadow incompatible with AMP Boilerplate
What's the issue?
When using AMP boilerplate incompatible with AMP shadow.
How do we reproduce the issue?
Can be reproduced here: https://p2er.github.io/amp-articleslider/amp-pwa-with-ampshadow.html Warning: race condition may change outcome as described on demo page
- Open Example URL
- Navigate with link to related article
- Problem varies depending on initially loaded library 3.1 If AMP shadow is loaded first, AMP components do no longer work 3.2 If AMP boilerplate is loaded first, AMP shadow can't initialize
What problem would be solved?
Using AMP components as Markup for the PWA page to utilize preloading and keep the codebase between both versions as close as possible. Even though the page is no longer valid AMP it should be able to load additional AMP article Templates with AMP shadow as the user progresses through the site. If AMP shadow is used to load the article content for the current page as well and therefor the AMP boilerplate is removed from the PWA page, the article content would not be visible to search engines.
What browsers are affected?
All browsers, due to JS bug.
Which AMP version is affected?
Powered by AMP ⚡ HTML shadows – Version 1494957764037 or Powered by AMP ⚡ HTML – Version 1494957764037
/to @dvoytenko
@p2er Hi! There are few issues on this page. In short, shadow docs are not created to work on an AMP page at this time. Shadow API specifically allows opening AMP documents inside the non-AMP shell. This way, any technology can be used in the shell, such as React and Angular.
More specifically:
Shadow vs AMP Runtime
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async src="https://cdn.ampproject.org/shadow-v0.js"></script>
Shadow runtime cannot be used with main AMP Runtime in the same context. A shell page would only use shadow-v0.js.
Boilerplate CSS should not be on the shell page
<style amp-boilerplate>...</style>
Waiting for shadow runtime to load
if (!AMP || ! AMP.attachShadowDoc) {
return false;
}
This could be fine depending on your logic. But if you were to want to wait for shadow runtime to load, you can use this code:
(window.AMP = window.AMP || []).push(function(AMP) {
// AMP can be used now.
});
I usually recommend wrapping this fragment in a promise.
Bottom line
I think your request may be coming from the desire to use AMP components in both shell page and in an AMP document. If that's the case, we've been trying to find a way to open something like this up. Unfortunately, this has been elusive because AMP routing, security and privacy models are all very tightly coupled with the document itself. We are still looking into it though. In the meantime, the assumption is that Shadow API would be used by a non-AMP shell.
This issue hasn't been updated in awhile. @dvoytenko Do you have any updates?
I'm seeing the same problem and yes, I'm using AMP components in the PWA shell page while trying to use AMP shadow to load AMP document fragments in the page. Hope this limitation can be removed.
This issue hasn't been updated in awhile. @dvoytenko Do you have any updates?
This issue hasn't been updated in awhile. @dvoytenko Do you have any updates?
/to @danielrozenberg
Hi, do we have anything new on this?
I'm developing a PWA with shadow-v0 that try to use amp-ads but i cannot use them on non-AMP parts with v0.
I tryed to use amp-ads under a dinamically created shadow root that just have an ad but it still fails because the lazy loading fires when the ad just trespassed the viewport topside. Also, shadow root fails on using amp-sticky-ads because the inner shadowed document isn't really scrollable.
I think loading documents within an existing AMP document would not be too much work, as <amp-next-page> does this. What I'm not sure about is how much effort it would be to unload documents from an existing AMP document, but I think it would definitely be quite a bit of work to make sure things get cleaned up correctly.
@sparhami Generally, calling close on a ampdoc to be unloaded should be enough and is already in use.
@sparhami Generally, calling close on a ampdoc to be unloaded should be enough and is already in use.
A couple of problems I see:
- Services are currently shared between the inner and outer doc. This can lead to memory leaks if things are not cleaned up correctly. One example I found that will be an issue is in VideoManager:
https://github.com/ampproject/amphtml/blob/fa12ba77ec6a2f7c7309f0778a0454ed5ed72b41/src/service/video-manager-impl.js#L187
Videos are registered and added to the this.entries_ array. There is no mechanism for unregistering a video. As a result, if you unload an AmpDoc, all the video elements contained within will be retained by the outer doc.
I'm not sure how many places this is a problem, but I think there would be some effort needed to find and fix the issues. It also isn't clear how exactly to do the clean up. For example, does an element that registers with the video service know what the doc is being unloaded and that it needs to unregister?
- Some services listen to
windowbut never unlisten. For example:
https://github.com/ampproject/amphtml/blob/fa12ba77ec6a2f7c7309f0778a0454ed5ed72b41/src/service/video-manager-impl.js#L1033
I've noticed that some services are instantiated when loading an AmpDoc, even when using single doc mode. For example, an InstrumentationService instance is created when you load an AmpDoc with <amp-analytics> (though it is currently not actually used).
My concern here is that some services will listen to window for events and not clean up correctly. Since the window belongs to the outer doc, this will leak memory.
I think the second problem is just services not cleaning up properly, which just need to be fixed on a case by case basis.
I'm going to close this issue. As @dvoytenko explained this is the expected behavior. While there are bugs related to components/services behaving badly in a multidoc environment, that's different from the initial intent of this issue, and separate issues need to be created with examples so we can attempt to track down and fix them.