amp-script: Use updated method to get meta hashes#26535
amp-script: Use updated method to get meta hashes#26535mdmower merged 10 commits intoampproject:masterfrom
Conversation
- When .attachShadowDoc() is called, import <meta name="amp-script-src"> tags into the host document head - Amend amp-script to always look in win.doc.head (instead of ampdoc.getHeadNode) for amp-script-src meta tags. Note that there is no <head> in an AmpDocShadow instance and the only valid location for <meta> tags is in a <head> element. - When shadowDoc.close() is called, remove any <meta name="amp-script-src"> tags in the host document head
| // the specific type of ampdoc, the top document head must be referenced. | ||
| // For example, there is no <head> element in an AmpDocShadow instance. | ||
| const headNode = dev().assertElement(ampdoc.win.document.head); | ||
| const allowedHashes = headNode.querySelector('meta[name="amp-script-src"]'); |
There was a problem hiding this comment.
- Is this the best approach, or would it be better to specially handle just the AmpDocShadow case?
let headNode = ampdoc.getHeadNode(); if (headNode instanceof ShadowRoot) { headNode = ampdoc.win.document.head; } - There have been murmurings over time that maybe someday multiple AMP documents could be displayed at once in a PWA. Should we prepare for this possibility? I see a couple of options:
querySelectorAll('meta[name="amp-script-src"]')- When
<meta name="amp-script-src" ...>is imported into<head>, if there already exists the same type of<meta>tag, concatenate theircontentvalues
There was a problem hiding this comment.
@mdmower there is support for multiple documents shown on one PWA today - that is the use case for the multidoc-manager.js file you edited, so I think we do need to support multiple meta tags to move forward
There was a problem hiding this comment.
Ready for re-review. From my original review:
- Opted to limit AmpScriptService changes to shadow docs
- Opted for ii: combine multiple ampdoc meta tags into a single meta tag in owner doc
kevinkimball
left a comment
There was a problem hiding this comment.
Hi @mdmower , thanks for your contribution. This fix looks correct to me from an initial review. Can you please add some test coverage for your change? Thanks!
- Handle multiple shadow docs - Only revise handling of AmpDocShadow in amp-script
|
There may still be room for improvement in the future w.r.t. multidoc handling, but this patchset is sufficient to bring amp-script support to PWAs, including after PWA navigation and when multiple docs are loaded. This minimal PWA can be dropped in the |
dreamofabear
left a comment
There was a problem hiding this comment.
Thanks so much for identifying and fixing this bug!
src/multidoc-manager.js
Outdated
| ' ' + | ||
| (n.getAttribute('content') || '') | ||
| ).trim(); | ||
| el.setAttribute('content', content); |
There was a problem hiding this comment.
I'd prefer to keep the script hashes separate between documents (to avoid allowing a document to enable scripts on another). Could we instead just import these nodes and append them to the shadow root?
We do something similar for JSON scripts: https://github.com/ampproject/amphtml/pull/26535/files#diff-0a54674fbde60777f171c0027199f578L452
Then we can revert the amp-script.js changes.
/cc @dvoytenko
There was a problem hiding this comment.
We could. I was avoiding insertion of meta tags into ShadowRoot since they should only appear under the <head> element. Do you have a preference for one of the following?
- allow
<meta>inShadowRootdespite invalid HTML - import script hashes into a dedicated, invisible element in
ShadowRoot
There was a problem hiding this comment.
Fair point. We don't really need this to exist in the shadow DOM, so maybe we can add a metadata abstraction to the AmpDoc class instead.
I.e. similar to the existing AmpDoc.getParam(), add AmpDoc.getMeta(name) which returns the "content" attribute value for the meta tag with the corresponding name. AmpDocShadow would populate this in mergeShadowHead_, and AmpDocSingle would query its head node.
Revert "amp-script: Add shadow meta import/cleanup test" This reverts commit af2eefc. Revert "amp-script: Ensure head meta on import" This reverts commit d9ffc56. Revert "amp-script: More whitespace simplification" This reverts commit 6b48726. Revert "amp-script: Simplify meta import whitespace cleanup" This reverts commit 15be318. Revert "amp-script: Perform checkSha384 test in shadow" This reverts commit 44f68cd. Revert "amp-script: Revisions based on review" This reverts commit b12b167. Revert "amp-script: Import meta hashes in PWA" This reverts commit 7eb105c.
- New getMetaByName method supports retrieving meta content values in shadow docs as well.
|
Ready for review. |
| } | ||
| function createMetaHash(name, content) { | ||
| if (variant.ampdoc === 'shadow') { | ||
| env.ampdoc.setMetaByName(name, content); |
There was a problem hiding this comment.
nit, wouldn't this work for single doc also?
There was a problem hiding this comment.
Thanks for the review. AmpDocSingle does not implement this method since the meta tags are expected to exist in <head>. .setMetaByName() is shadow-only for now.
There was a problem hiding this comment.
Got it, thanks. I'll defer to the original discussion but it might be worth considering in the future to unify the API
|
Looks great! |
New getMetaByName method supports retrieving meta content values in
shadow docs as well.
Fixes #26534