Skip to content

Commit

Permalink
Only inject runtime-host meta tag if host is given (#855)
Browse files Browse the repository at this point in the history
* Only include host in runtime-host meta tag
* Print warning if ampUrlPrefix is not absolute
* Don't inject runtime-host meta tag if ampUrlPrefix is not absolute.
This will fallback to cdn.ampproject.org.
  • Loading branch information
sebastianbenz authored Jul 8, 2020
1 parent 73647dd commit 5c15f11
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
12 changes: 6 additions & 6 deletions packages/optimizer/lib/transformers/RewriteAmpUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const {calculateHost} = require('../RuntimeHostHelper');
class RewriteAmpUrls {
constructor(config) {
this.esmModulesEnabled = config.experimentEsm;
this.log = config.log;
}
transform(root, params) {
const html = firstChildByTag(root, 'html');
Expand Down Expand Up @@ -109,13 +110,12 @@ class RewriteAmpUrls {

// runtime-host and amp-geo-api meta tags should appear before the first script
if (!this._usesAmpCacheUrl(host) && !params.lts) {
let versionlessHost;
if (this.isAbsoluteUrl_(params.ampUrlPrefix)) {
versionlessHost = calculateHost({ampUrlPrefix: params.ampUrlPrefix});
} else {
versionlessHost = '.';
try {
const url = new URL(host);
this._addMeta(head, 'runtime-host', url.origin);
} catch (e) {
this.log.warn('ampUrlPrefix must be an absolute URL');
}
this._addMeta(head, 'runtime-host', versionlessHost);
}
if (params.geoApiUrl && !params.lts) {
this._addMeta(head, 'amp-geo-api', params.geoApiUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8"><style amp-runtime i-amphtml-version="123456789000000">/* example.com v0.css */</style>
<meta name="viewport" content="width=device-width,minimum-scale=1">
<link rel="preload" href="https://example.com/amp/rtv/123456789000000/v0.js" as="script">
<meta name="runtime-host" content="https://example.com/amp">
<meta name="runtime-host" content="https://example.com">
<meta name="amp-geo-api" content="/geo">
<script async src="https://example.com/amp/rtv/123456789000000/v0.js"></script>
<script async custom-element="amp-experiment" src="https://example.com/amp/rtv/123456789000000/v0/amp-experiment-0.1.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!doctype html>
<html >
<head>
<meta name="runtime-host" content=".">
<script async nomodule src="/amp/rtv/001515617716922/v0/amp-experiment-0.1.js"></script>
<script async custom-element="amp-experiment" src="/amp/rtv/001515617716922/v0/amp-experiment-0.1.mjs" type="module" crossorigin="anonymous"></script>
<script async nomodule src="/amp/rtv/001515617716922/v0.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!doctype html>
<html >
<head>
<meta name="runtime-host" content=".">
<script async nomodule src="/amp/v0/amp-experiment-0.1.js"></script>
<script async custom-element="amp-experiment" src="/amp/v0/amp-experiment-0.1.mjs" type="module" crossorigin="anonymous"></script>
<script async nomodule src="/amp/v0.js"></script>
Expand Down

0 comments on commit 5c15f11

Please sign in to comment.