Skip to content

amp-script: Use updated method to get meta hashes#26535

Merged
mdmower merged 10 commits intoampproject:masterfrom
mdmower:i26534
Feb 11, 2020
Merged

amp-script: Use updated method to get meta hashes#26535
mdmower merged 10 commits intoampproject:masterfrom
mdmower:i26534

Conversation

@mdmower
Copy link
Copy Markdown
Contributor

@mdmower mdmower commented Jan 29, 2020

New getMetaByName method supports retrieving meta content values in
shadow docs as well.

Fixes #26534

- 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"]');
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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;
    }
    
  2. 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:
    1. querySelectorAll('meta[name="amp-script-src"]')
    2. When <meta name="amp-script-src" ...> is imported into <head>, if there already exists the same type of <meta> tag, concatenate their content values

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinkimball Can you take a look at this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for re-review. From my original review:

  1. Opted to limit AmpScriptService changes to shadow docs
  2. Opted for ii: combine multiple ampdoc meta tags into a single meta tag in owner doc

Copy link
Copy Markdown
Contributor

@kevinkimball kevinkimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@mdmower mdmower requested a review from kevinkimball February 1, 2020 07:28
@mdmower
Copy link
Copy Markdown
Contributor Author

mdmower commented Feb 3, 2020

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 examples/ folder for testing:

Copy link
Copy Markdown

@dreamofabear dreamofabear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for identifying and fixing this bug!

' ' +
(n.getAttribute('content') || '')
).trim();
el.setAttribute('content', content);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

  1. allow <meta> in ShadowRoot despite invalid HTML
  2. import script hashes into a dedicated, invisible element in ShadowRoot

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@mdmower mdmower changed the title amp-script: Import meta hashes in PWA amp-script: Use updated method to get meta hashes Feb 6, 2020
@mdmower mdmower requested a review from dreamofabear February 7, 2020 21:41
@mdmower
Copy link
Copy Markdown
Contributor Author

mdmower commented Feb 7, 2020

Ready for review.

Copy link
Copy Markdown
Member

@samouri samouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would wait for someone else's review as well before submitting, but these changes LGTM!

Copy link
Copy Markdown
Contributor

@kevinkimball kevinkimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}
function createMetaHash(name, content) {
if (variant.ampdoc === 'shadow') {
env.ampdoc.setMetaByName(name, content);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, wouldn't this work for single doc also?

Copy link
Copy Markdown
Contributor Author

@mdmower mdmower Feb 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks. I'll defer to the original discussion but it might be worth considering in the future to unify the API

@mdmower mdmower merged commit c98e5f3 into ampproject:master Feb 11, 2020
@mdmower mdmower deleted the i26534 branch February 11, 2020 06:02
@dreamofabear
Copy link
Copy Markdown

Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

amp-script meta hash tags not recognized in PWA

7 participants