`ampdoc.getUrl()` is empty on page loaded by `<amp-next-page>`
When new page append, Addthis buttons not sharing current url. It is sharing https://s7.addthis.com/dc/amp-addthis.html?_amp_=2104022034000. And share counts not showing true results.
Repro case: https://amp-next-page-addthis.glitch.me/
<amp-addthis
width="320"
height="92"
data-pub-id="ra-5c191331410932ff"
data-widget-id="mv93"
data-widget-type="inline"
>
</amp-addthis>
<div style="height: 150vh"></div>
<amp-next-page>
<script type="application/json">
[
{
"url": "./?1",
"title": "same page",
"image": "https://preview.amp.dev/static/samples/img/amp.jpg"
},
{
"url": "./?2",
"title": "same page",
"image": "https://preview.amp.dev/static/samples/img/amp.jpg"
}
]
</script>
</amp-next-page>
<div style="height: 150vh"></div>
If I click on the first + button, the shared URL is correct.
If I click on the second + button, we see that it uses https://s7.addthis.com/dc/amp-addthis.html..., because the URL passed by AMP is empty. (The iframe falls back to sharing its own URL when it doesn't know the host's.)
I can add a logpoint here and see that this.getAmpDoc().getUrl() is empty on the second page:
https://github.com/ampproject/amphtml/blob/b5c85d7510aadcbc4562228e795c7d94d7ddc562/extensions/amp-addthis/0.1/amp-addthis.js#L370
I'm bumping this to P1 since this might be breaking more components in unknown ways. @dmanek Feel free to lower priority if you have more info.
Thanks for the repro case @alanorozco.
This reason for this bug is that when amp-next-page appends new content, it does so in a shadow dom. Thus, calling getAmpDoc().getUrl() returns an empty string. This issue will affect any component that calls getAmpDoc within amp-next-page. One fix would be to update getAmpDoc in custom-element.js to return the root amp doc when called from within shadow dom, but will need to investigate if that breaks anything else.
Per conversation with @alanorozco, the fix is to include the url for documents in the shadow dom, that way getAmpDoc().getUrl() does not return an empty string. Also lowering to P2 since this is not a regression.