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

Improve support for self-hosted runtime #25026

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions examples/self-hosted-amprt-amp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!doctype html>
<html ⚡>
<head>
<title>Self-hosted AMP test</title>
<meta charset="utf-8">
<link rel="canonical" href="https://www/">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-geo" src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
body {
margin: 0;
padding: 8px;
font-family: sans-serif;
}
.geo-text,
body[class*="amp-geo"] .nogeo-text {
display: none;
}
.nogeo-text,
body[class*="amp-geo"] .geo-text {
display: block;
}
h3 {
margin-bottom: 0.5em;
}
p, ul {
margin: 0;
}
ul {
list-style-type: none;
padding-left: 0.5em;
margin-bottom: 0.5em;
}
li {
margin-top: 0.5em;
}
</style>
</head>
<body>
<h2>Self-hosted AMP runtime test</h2>

<h3>amp-geo</h3>
<p class="geo-text"><code>amp-geo</code> class successfully added to body.</p>
<p class="nogeo-text"><code>amp-geo</code> <strong>did not</strong> add a class to the body.</p>

<h3>amp-img</h3>
<p><amp-img layout="fixed" width="320" height="214" src="https://android.cmphys.com/temp/img/goldfish.jpg"></amp-img></p>

<h3>amp-form</h3>
<form method="GET" target="_top" action="self-hosted-amprt-pwa.html">
<p>View in PWA:</p>
<ul>
<li><label><input name="page" type="radio" value="amp"> Standard AMP</label></li>
<li><label><input name="page" type="radio" value="opt"> Optimized AMP</label></li>
</ul>
<button type="submit">Submit</button>
</form>

<h3>View as AMP (non-PWA):</h3>
<ul>
<li><a href="self-hosted-amprt-amp.html">Standard AMP page</a></li>
<li><a href="self-hosted-amprt-opt.html">Optimized AMP page</a></li>
</ul>

<amp-geo layout=nodisplay>
<script type="application/json">
{ "ISOCountryGroups": { "isus": [ "us" ] } }
</script>
</amp-geo>
</body>
</html>
37 changes: 37 additions & 0 deletions examples/self-hosted-amprt-opt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html><html ⚡="" i-amphtml-layout="" i-amphtml-no-boilerplate="" transformed="self;v=1"><head><meta charset="utf-8"><style amp-runtime="" i-amphtml-version="011910071803120">html{overflow-x:hidden!important}html.i-amphtml-fie{height:100%!important;width:100%!important}html:not([amp4ads]),html:not([amp4ads]) body{height:auto!important}html:not([amp4ads]) body{margin:0!important}body{-webkit-text-size-adjust:100%;-moz-text-size-adjust:100%;-ms-text-size-adjust:100%;text-size-adjust:100%}html.i-amphtml-singledoc.i-amphtml-embedded{-ms-touch-action:pan-y;touch-action:pan-y}html.i-amphtml-fie>body,html.i-amphtml-singledoc>body{overflow:visible!important}html.i-amphtml-fie:not(.i-amphtml-inabox)>body,html.i-amphtml-singledoc:not(.i-amphtml-inabox)>body{position:relative!important}html.i-amphtml-webview>body{overflow-x:hidden!important;overflow-y:visible!important;min-height:100vh!important}html.i-amphtml-ios-embed-legacy>body{overflow-x:hidden!important;overflow-y:auto!important;position:absolute!important}html.i-amphtml-ios-embed{overflow-y:auto!important;position:static}#i-amphtml-wrapper{overflow-x:hidden!important;overflow-y:auto!important;position:absolute!important;top:0!important;left:0!important;right:0!important;bottom:0!important;margin:0!important;display:block!important}html.i-amphtml-ios-embed.i-amphtml-ios-overscroll,html.i-amphtml-ios-embed.i-amphtml-ios-overscroll>#i-amphtml-wrapper{-webkit-overflow-scrolling:touch!important}#i-amphtml-wrapper>body{position:relative!important;border-top:1px solid transparent!important}#i-amphtml-wrapper+body{visibility:visible}#i-amphtml-wrapper+body .i-amphtml-lightbox-element,#i-amphtml-wrapper+body[i-amphtml-lightbox]{visibility:hidden}#i-amphtml-wrapper+body[i-amphtml-lightbox] .i-amphtml-lightbox-element{visibility:visible}#i-amphtml-wrapper.i-amphtml-scroll-disabled,.i-amphtml-scroll-disabled{overflow-x:hidden!important;overflow-y:hidden!important}amp-instagram{padding:54px 0px 0px!important;background-color:#fff}amp-iframe iframe{box-sizing:border-box!important}[amp-access][amp-access-hide]{display:none}[subscriptions-dialog],body:not(.i-amphtml-subs-ready) [subscriptions-action],body:not(.i-amphtml-subs-ready) [subscriptions-section]{display:none!important}amp-experiment,amp-live-list>[update],amp-share-tracking{display:none}.i-amphtml-jank-meter{position:fixed;background-color:rgba(232,72,95,0.5);bottom:0;right:0;color:#fff;font-size:16px;z-index:1000;padding:5px}amp-list[resizable-children]>.i-amphtml-loading-container.amp-hidden{display:none!important}amp-list[load-more] [load-more-button],amp-list[load-more] [load-more-end],amp-list[load-more] [load-more-failed],amp-list[load-more] [load-more-loading]{display:none}amp-story-page,amp-story[standalone]{min-height:1px!important;display:block!important;height:100%!important;margin:0!important;padding:0!important;overflow:hidden!important;width:100%!important}amp-story[standalone]{background-color:#202125!important;position:relative!important}amp-story-page{background-color:#757575}amp-story .i-amphtml-loader{display:none!important}amp-story-page:not(:first-of-type):not([distance]):not([active]){transform:translateY(1000vh)!important}amp-autocomplete{position:relative!important;display:inline-block!important}amp-autocomplete>input{padding:.5rem;border:1px solid rgba(0,0,0,0.33)}.i-amphtml-autocomplete-results,amp-autocomplete>input{font-size:1rem;line-height:1.5rem}[amp-fx^=fly-in]{visibility:hidden}
/*# sourceURL=/css/ampdoc.css*/[hidden]{display:none!important}.i-amphtml-element{display:inline-block}.i-amphtml-blurry-placeholder{transition:opacity 0.3s cubic-bezier(0.0,0.0,0.2,1)!important}[layout=nodisplay]:not(.i-amphtml-element){display:none!important}.i-amphtml-layout-fixed,[layout=fixed][width][height]:not(.i-amphtml-layout-fixed){display:inline-block;position:relative}.i-amphtml-layout-responsive,[layout=responsive][width][height]:not(.i-amphtml-layout-responsive),[width][height][sizes]:not(.i-amphtml-layout-responsive){display:block;position:relative}.i-amphtml-layout-intrinsic{display:inline-block;position:relative;max-width:100%}.i-amphtml-intrinsic-sizer{max-width:100%;display:block!important}.i-amphtml-layout-container,.i-amphtml-layout-fixed-height,[layout=container],[layout=fixed-height][height]{display:block;position:relative}.i-amphtml-layout-fill,[layout=fill]:not(.i-amphtml-layout-fill){display:block;overflow:hidden!important;position:absolute;top:0;left:0;bottom:0;right:0}.i-amphtml-layout-flex-item,[layout=flex-item]:not(.i-amphtml-layout-flex-item){display:block;position:relative;-ms-flex:1 1 auto;flex:1 1 auto}.i-amphtml-layout-fluid{position:relative}.i-amphtml-layout-size-defined{overflow:hidden!important}.i-amphtml-layout-awaiting-size{position:absolute!important;top:auto!important;bottom:auto!important}i-amphtml-sizer{display:block!important}.i-amphtml-blurry-placeholder,.i-amphtml-fill-content{display:block;height:0;max-height:100%;max-width:100%;min-height:100%;min-width:100%;width:0;margin:auto}.i-amphtml-layout-size-defined .i-amphtml-fill-content{position:absolute;top:0;left:0;bottom:0;right:0}.i-amphtml-layout-intrinsic .i-amphtml-sizer{max-width:100%}.i-amphtml-replaced-content,.i-amphtml-screen-reader{padding:0!important;border:none!important}.i-amphtml-screen-reader{position:fixed!important;top:0px!important;left:0px!important;width:4px!important;height:4px!important;opacity:0!important;overflow:hidden!important;margin:0!important;display:block!important;visibility:visible!important}.i-amphtml-screen-reader~.i-amphtml-screen-reader{left:8px!important}.i-amphtml-screen-reader~.i-amphtml-screen-reader~.i-amphtml-screen-reader{left:12px!important}.i-amphtml-screen-reader~.i-amphtml-screen-reader~.i-amphtml-screen-reader~.i-amphtml-screen-reader{left:16px!important}.i-amphtml-unresolved{position:relative;overflow:hidden!important}.i-amphtml-select-disabled{-webkit-user-select:none!important;-moz-user-select:none!important;-ms-user-select:none!important;user-select:none!important}.i-amphtml-notbuilt,[layout]:not(.i-amphtml-element){position:relative;overflow:hidden!important;color:transparent!important}.i-amphtml-notbuilt:not(.i-amphtml-layout-container)>*,[layout]:not([layout=container]):not(.i-amphtml-element)>*{display:none}.i-amphtml-ghost{visibility:hidden!important}.i-amphtml-element>[placeholder],[layout]:not(.i-amphtml-element)>[placeholder]{display:block}.i-amphtml-element>[placeholder].amp-hidden,.i-amphtml-element>[placeholder].hidden{visibility:hidden}.i-amphtml-element:not(.amp-notsupported)>[fallback],.i-amphtml-layout-container>[placeholder].amp-hidden,.i-amphtml-layout-container>[placeholder].hidden{display:none}.i-amphtml-layout-size-defined>[fallback],.i-amphtml-layout-size-defined>[placeholder]{position:absolute!important;top:0!important;left:0!important;right:0!important;bottom:0!important;z-index:1}.i-amphtml-notbuilt>[placeholder]{display:block!important}.i-amphtml-hidden-by-media-query{display:none!important}.i-amphtml-element-error{background:red!important;color:#fff!important;position:relative!important}.i-amphtml-element-error:before{content:attr(error-message)}i-amp-scroll-container,i-amphtml-scroll-container{position:absolute;top:0;left:0;right:0;bottom:0;display:block}i-amp-scroll-container.amp-active,i-amphtml-scroll-container.amp-active{overflow:auto;-webkit-overflow-scrolling:touch}.i-amphtml-loading-container{display:block!important;pointer-events:none;z-index:1}.i-amphtml-notbuilt>.i-amphtml-loading-container{display:block!important}.i-amphtml-loading-container.amp-hidden{visibility:hidden}.i-amphtml-loader-line{position:absolute;top:0;left:0;right:0;height:1px;overflow:hidden!important;background-color:hsla(0,0%,59.2%,0.2);display:block}.i-amphtml-loader-moving-line{display:block;position:absolute;width:100%;height:100%!important;background-color:hsla(0,0%,59.2%,0.65);z-index:2}@keyframes i-amphtml-loader-line-moving{0%{transform:translateX(-100%)}to{transform:translateX(100%)}}.i-amphtml-loader-line.amp-active .i-amphtml-loader-moving-line{animation:i-amphtml-loader-line-moving 4s ease infinite}.i-amphtml-loader{position:absolute;display:block;height:10px;top:50%;left:50%;transform:translateX(-50%) translateY(-50%);transform-origin:50% 50%;white-space:nowrap}.i-amphtml-loader.amp-active .i-amphtml-loader-dot{animation:i-amphtml-loader-dots 2s infinite}.i-amphtml-loader-dot{position:relative;display:inline-block;height:10px;width:10px;margin:2px;border-radius:100%;background-color:rgba(0,0,0,0.3);box-shadow:2px 2px 2px 1px rgba(0,0,0,0.2);will-change:transform}.i-amphtml-loader .i-amphtml-loader-dot:first-child{animation-delay:0s}.i-amphtml-loader .i-amphtml-loader-dot:nth-child(2){animation-delay:.1s}.i-amphtml-loader .i-amphtml-loader-dot:nth-child(3){animation-delay:.2s}@keyframes i-amphtml-loader-dots{0%,to{transform:scale(.7);background-color:rgba(0,0,0,0.3)}50%{transform:scale(.8);background-color:rgba(0,0,0,0.5)}}.i-amphtml-element>[overflow]{cursor:pointer;position:relative;z-index:2;visibility:hidden}.i-amphtml-element>[overflow].amp-visible{visibility:visible}template{display:none!important}.amp-border-box,.amp-border-box *,.amp-border-box :after,.amp-border-box :before{box-sizing:border-box}amp-pixel{display:none!important}amp-analytics,amp-story-auto-ads{position:fixed!important;top:0!important;width:1px!important;height:1px!important;overflow:hidden!important;visibility:hidden}html.i-amphtml-fie>amp-analytics{position:initial!important}[visible-when-invalid]:not(.visible),amp-list [fetch-error],form [submit-error],form [submit-success],form [submitting]{display:none}amp-accordion{display:block!important}amp-accordion>section{float:none!important}amp-accordion>section>*{float:none!important;display:block!important;overflow:hidden!important;position:relative!important}amp-accordion,amp-accordion>section{margin:0}amp-accordion>section>:last-child{display:none!important}amp-accordion>section[expanded]>:last-child{display:block!important}
/*# sourceURL=/css/ampshared.css*/</style><meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"><link rel="preload" href="https://android.cmphys.com/temp/amprt/v0.js" as="script"><script async="" src="https://android.cmphys.com/temp/amprt/v0.js"></script><script async="" custom-element="amp-geo" src="https://cdn.ampproject.org/rtv/011910071803120/v0/amp-geo-0.1.js"></script><script async="" custom-element="amp-form" src="https://android.cmphys.com/temp/amprt/v0/amp-form-0.1.js"></script><style amp-custom="">body{margin:0;padding:8px;font-family:sans-serif;}.geo-text,body[class*="amp-geo"] .nogeo-text{display:none;}.nogeo-text,body[class*="amp-geo"] .geo-text{display:block;}h3{margin-bottom:0.5em;}p,ul{margin:0;}ul{list-style-type:none;padding-left:0.5em;margin-bottom:0.5em;}li{margin-top:0.5em;}</style><title>Self-hosted AMP test</title><link rel="canonical" href="https://www/"></head>
<body>
<h2>Self-hosted AMP runtime test</h2>

<h3>amp-geo</h3>
<p class="geo-text"><code>amp-geo</code> class successfully added to body.</p>
<p class="nogeo-text"><code>amp-geo</code> <strong>did not</strong> add a class to the body.</p>

<h3>amp-img</h3>
<p><amp-img layout="fixed" width="320" height="214" src="https://android.cmphys.com/temp/img/goldfish.jpg" class="i-amphtml-layout-fixed i-amphtml-layout-size-defined" style="width:320px;height:214px;" i-amphtml-layout="fixed"></amp-img></p>

<h3>amp-form</h3>
<form method="GET" target="_top" action="self-hosted-amprt-pwa.html">
<p>View in PWA:</p>
<ul>
<li><label><input name="page" type="radio" value="amp"> Standard AMP</label></li>
<li><label><input name="page" type="radio" value="opt"> Optimized AMP</label></li>
</ul>
<button type="submit">Submit</button>
</form>

<h3>View as AMP (non-PWA):</h3>
<ul>
<li><a href="self-hosted-amprt-amp.html">Standard AMP page</a></li>
<li><a href="self-hosted-amprt-opt.html">Optimized AMP page</a></li>
</ul>

<amp-geo layout="nodisplay" class="i-amphtml-layout-nodisplay" hidden="hidden" i-amphtml-layout="nodisplay">
<script type="application/json">
{ "ISOCountryGroups": { "isus": [ "us" ] } }
</script>
</amp-geo>


</body></html>
42 changes: 42 additions & 0 deletions examples/self-hosted-amprt-pwa.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!doctype html>
<html>
<head>
<title>Self-hosted PWA test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script>
window.AMP_CONFIG = {
cdnUrl: 'https://android.cmphys.com/temp/amprt',
cdnUrlSupportsCacheModifiedExtensions: false
};
</script>
<script async src="https://android.cmphys.com/temp/amprt/shadow-v0.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="shadowHost"></div>
<script>
(window.AMP = window.AMP || []).push(function(AMP) {
const page = /[?&]page=opt/.test(location.search) ? '-opt' : '-amp';
const url = location.href.replace('-pwa', page);
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'document';
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
const hostElement = document.querySelector('#shadowHost');
window.shadowDoc = AMP.attachShadowDoc(hostElement, xhr.response, url);
}
}
};
xhr.send();
});
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion spec/amp-cache-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ For resources (i.e., HTML, images, fonts) that are larger than 12 MB, you may no

- Add the details of your cache to [caches.json](../build-system/global-configs/caches.json).
- Some tools use the deployed version of this file (<https://cdn.ampproject.org/caches.json>) to automatically generate appropriate CORS headers as described at [CORS Security in AMP Guidelines](https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md#cors-security-in-amp).
- Handle dynamically generated components:
- Handle cache-modified extensions:
- [`<amp-geo>`](../extensions/amp-geo/amp-geo.md) — at delivery time, the string `{{AMP_ISO_COUNTRY_HOTPATCH}}` in [`amp-geo.js`](../extensions/amp-geo/0.1/amp-geo.js) is replaced by a string consisting of the user's ISO country code followed by exactly 26 spaces (to avoid breaking map files).

## References
Expand Down
33 changes: 33 additions & 0 deletions src/cache-modified-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Cache modified extensions - documented at spec/amp-cache-guidelines.md under
* headline "Guidelines: Adding a new cache to the AMP ecosystem"
*/
const CACHE_MODIFIED_EXTENSIONS = ['amp-geo'];

/**
* Get status of whether an extension is modified by the cache when served
* @param {string} extensionId
* @return {boolean}
*/
export function isCacheModifiedExtension(extensionId) {
return (
Boolean(extensionId) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Boolean check shouldn't be necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not necessary, but prevents an unnecessary loop if an empty string is passed for extensionId.

CACHE_MODIFIED_EXTENSIONS.some(id => id === extensionId.toLowerCase())
);
}
12 changes: 10 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
* use the src/config.js module for various constants. We can use the
* AMP_CONFIG global to translate user-defined configurations to this
* module.
* @type {!Object<string, string>}
* @type {!Object<string, (string|boolean|RegExp)>}
*/
const env = self.AMP_CONFIG || {};
const DEFAULT_CDN = 'https://cdn.ampproject.org';

const thirdPartyFrameRegex =
typeof env['thirdPartyFrameRegex'] == 'string'
Expand All @@ -33,12 +34,19 @@ const cdnProxyRegex =
? new RegExp(env['cdnProxyRegex'])
: env['cdnProxyRegex'];

const cdnSupportsCacheModifiedExtensions =
typeof env['cdnUrlSupportsCacheModifiedExtensions'] == 'string'
? env['cdnUrlSupportsCacheModifiedExtensions'] !== 'false'
: env['cdnUrlSupportsCacheModifiedExtensions'] !== false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain why this is necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's just a means of supporting cdnUrlSupportsCacheModifiedExtensions defined as either string or boolean.


/** @type {!Object<string, string|boolean|RegExp|Array<RegExp>>} */
export const urls = {
thirdParty: env['thirdPartyUrl'] || 'https://3p.ampproject.net',
thirdPartyFrameHost: env['thirdPartyFrameHost'] || 'ampproject.net',
thirdPartyFrameRegex: thirdPartyFrameRegex || /^d-\d+\.ampproject\.net$/,
cdn: env['cdnUrl'] || 'https://cdn.ampproject.org',
cdn: env['cdnUrl'] || DEFAULT_CDN,
cdnSupportsCacheModifiedExtensions,
defaultCdn: DEFAULT_CDN,
/* Note that cdnProxyRegex is only ever checked against origins
* (proto://host[:port]) so does not need to consider path
*/
Expand Down
Loading