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

Fix runtime-host meta tag when using relative URLs #854

Merged
merged 1 commit into from
Jul 8, 2020
Merged
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
16 changes: 15 additions & 1 deletion packages/optimizer/lib/transformers/RewriteAmpUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ class RewriteAmpUrls {

// runtime-host and amp-geo-api meta tags should appear before the first script
if (!this._usesAmpCacheUrl(host) && !params.lts) {
const versionlessHost = calculateHost({ampUrlPrefix: params.ampUrlPrefix});
let versionlessHost;
if (this.isAbsoluteUrl_(params.ampUrlPrefix)) {
versionlessHost = calculateHost({ampUrlPrefix: params.ampUrlPrefix});
} else {
versionlessHost = '.';
}
this._addMeta(head, 'runtime-host', versionlessHost);
}
if (params.geoApiUrl && !params.lts) {
Expand Down Expand Up @@ -174,6 +179,15 @@ class RewriteAmpUrls {
const meta = createElement('meta', {name, content});
insertBefore(head, meta, firstChildByTag(head, 'script'));
}

isAbsoluteUrl_(url) {
try {
new URL(url);
return true;
} catch (ex) {}

return false;
}
}

module.exports = RewriteAmpUrls;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!doctype html>
<html ⚡>
<head>
<meta name="runtime-host" content="/amp">
<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,7 @@
<!doctype html>
<html ⚡>
<head>
<meta name="runtime-host" content="/amp">
<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