Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimizer: Make dynamic component URL rewrites optional #518

Merged
merged 1 commit into from
Oct 9, 2019
Merged

optimizer: Make dynamic component URL rewrites optional #518

merged 1 commit into from
Oct 9, 2019

Conversation

mattwomple
Copy link
Member

@mattwomple mattwomple commented Oct 4, 2019

DEPENDS ON #517

Not everyone who self-hosts the AMP runtime has the infrastructure to
support dynamic components like amp-geo, ref: AMP cache guidelines.

Introduce option rewriteDynamicComponents that prevents modification of
dynamic component URLs that would otherwise have ampUrlPrefix
substituted.

Two relevant tests added:

  • rewrites_host_but_not_dynamic_components_with_rtv
  • rewrites_host_but_not_dynamic_components_without_rtv

Fixes #515

@mattwomple mattwomple changed the title [wip] optimizer: Make dynamic component URL rewrites optional optimizer: Make dynamic component URL rewrites optional Oct 9, 2019
@mattwomple
Copy link
Member Author

This is ready for review, with the caveat that it has been written with #517 as its base.

Copy link
Collaborator

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

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

Looks good except: I think this behavior should be supported by versioned and unversioned runtime artifacts (or am I missing anything?).

Otherwise few nits.


**Notice:** The behavior of `ampUrlPrefix` when used in conjunction with `ampRuntimeVersion` changed beginning with version 2.0. Prior to 2.0, `rtv/{rtv}/` was automatically appended to `ampUrlPrefix` when `ampRuntimeVersion` was specified. Since version 2.0, `ampUrlPrefix` is not modified when `ampRuntimeVersion` is also specified.

- `rewriteDynamicComponents`: When used in conjunction with `ampUrlPrefix`, this option can be set to `false` to prevent [dynamic AMP components](https://github.com/ampproject/amphtml/blob/master/spec/amp-cache-guidelines.md#guidelines-adding-a-new-cache-to-the-amp-ecosystem) from having their URLs rewritten. This option is only effective if `ampRuntimeVersion` is specified. This ensures the self-hosted runtime version matches version of the dynamic component delivered by `cdn.ampproject.org`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This option is only effective if ampRuntimeVersion is specified. This ensures the self-hosted runtime version matches version of the dynamic component delivered by cdn.ampproject.org.

I don't understand why it only makes sense in conjunction with ampRuntime Version.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is resolved in commit 3354b6a.

* re-written. You can combine `ampRuntimeVersion` and `ampUrlPrefix` to rewrite
* AMP runtime URLs to versioned URLs on a different origin.
*
* rewriteDynamicComponents is only effective if `ampRuntimeVersion` and
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as above

Copy link
Member Author

Choose a reason for hiding this comment

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

This is resolved in commit 3354b6a.

}

// Should be kept up to date with dynamic components listed here:
// https://github.com/ampproject/amphtml/blob/master/spec/amp-cache-guidelines.md#guidelines-adding-a-new-cache-to-the-amp-ecosystem
const dynamicElements = ['amp-geo'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

please move this into a constant at the top of the file

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it to AMP_CONSTANTS. Good? Bad?

// Should be kept up to date with dynamic components listed here:
// https://github.com/ampproject/amphtml/blob/master/spec/amp-cache-guidelines.md#guidelines-adding-a-new-cache-to-the-amp-ecosystem
const dynamicElements = ['amp-geo'];
const dynamicTemplates = [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as above

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it to AMP_CONSTANTS. Good? Bad?

Copy link
Collaborator

Choose a reason for hiding this comment

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

even better

let node = head.firstChild;
let referenceNode = findMetaViewport(head);

while (node) {
if (node.tagName === 'script' && this._usesAmpCacheUrl(node.attribs.src)) {
node.attribs.src = this._replaceUrl(node.attribs.src, ampUrlPrefix);
const isDynamicComponent =
Copy link
Collaborator

Choose a reason for hiding this comment

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

please extract this into a separate method for readability

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me know if commit 3354b6a does this adequately.

@mattwomple
Copy link
Member Author

Looks good except: I think this behavior should be supported by versioned and unversioned runtime artifacts (or am I missing anything?).

See here and my following comment: #515 (comment)

@sebastianbenz
Copy link
Collaborator

Hah! Completely forgot about this 🤦‍♂️. It starts getting tricky. With the revised self-hosting behavior (ampRuntimeVersion no longer automatically being appended) the version will no longer be automatically enforced. However, the good news is: the AMP runtime will automatically download the matching component version if there's a mismatch. So worst case is: the extension needs to be downloaded twice, but will continue to work. That's acceptable for me.

Hence, I'd favor making this independent of the runtime version.

Not everyone who self-hosts the AMP runtime has the infrastructure to
support dynamic components like amp-geo, ref:
https://github.com/ampproject/amphtml/blob/master/spec/amp-cache-guidelines.md#guidelines-adding-a-new-cache-to-the-amp-ecosystem

Introduce option rewriteDynamicComponents that prevents modification of
dynamic component URLs that would otherwise have ampUrlPrefix
substituted.

Two relevant tests added:
 rewrites_host_but_not_dynamic_components_with_rtv
 rewrites_host_but_not_dynamic_components_without_rtv
Copy link
Collaborator

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@sebastianbenz sebastianbenz merged commit 81e525d into ampproject:master Oct 9, 2019
@mattwomple mattwomple deleted the patch-2 branch October 9, 2019 19:55
mdmower added a commit to mdmower/amp-toolbox that referenced this pull request Jan 20, 2020
…roject#518)"

This reverts commit 81e525d.

Do not allow "partial self-hosting". All AMP components should come from
the same origin to ensure version locking support is maintained. An
alternative solution for amp-geo is to fall back to an API request
instead of requiring cache modification when amp-geo-0.1.js is served.
mdmower added a commit to mdmower/amp-toolbox that referenced this pull request Jan 29, 2020
mdmower added a commit to mdmower/amp-toolbox that referenced this pull request Feb 2, 2020
sebastianbenz pushed a commit that referenced this pull request Feb 6, 2020
* Revert "optimizer: Make dynamic component URL rewrites optional (#518)"

This reverts commit 81e525d.

* Revert "optimizer: Do not append rtv/{rtv}/ to ampUrlPrefix (#517)"

This reverts commit 9e7533c.
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.

FR: Make URL rewriting of SSR AMP component optional
3 participants