Skip to content

Commit c7d6eae

Browse files
authored
Merge pull request #1358 from ampproject/dual-domain
amp optimizer to support ampjs.org (not enabled as default)
2 parents 19405f5 + 9cbf6e0 commit c7d6eae

File tree

16 files changed

+215
-130
lines changed

16 files changed

+215
-130
lines changed

packages/optimizer/lib/AmpConstants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const AMP_STORY_DVH_POLYFILL_ATTR = 'amp-story-dvh-polyfill';
2121

2222
module.exports = {
2323
AMP_TAGS: ['amp', '⚡', '⚡4ads', 'amp4ads', '⚡4email', 'amp4email'],
24-
AMP_CACHE_HOST: 'https://cdn.ampproject.org',
24+
AMP_CACHE_HOSTS: ['https://cdn.ampproject.org', 'https://ampjs.org'],
25+
DEFAULT_AMP_CACHE_HOST: 'https://cdn.ampproject.org',
2526
AMP_VALIDATION_RULES_URL: 'https://cdn.ampproject.org/v0/validator.json',
2627
AMP_FORMATS: ['AMP', 'AMP4EMAIL', 'AMP4ADS'],
2728
AMP_RUNTIME_CSS_PATH: '/v0.css',

packages/optimizer/lib/RuntimeHostHelper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
'use strict';
1717

18-
const {AMP_CACHE_HOST, appendRuntimeVersion} = require('./AmpConstants.js');
18+
const {DEFAULT_AMP_CACHE_HOST, appendRuntimeVersion} = require('./AmpConstants.js');
1919

2020
function calculateHost({
21-
ampUrlPrefix = AMP_CACHE_HOST,
21+
ampUrlPrefix = DEFAULT_AMP_CACHE_HOST,
2222
ampRuntimeVersion,
2323
lts = false,
2424
rtv = false,

packages/optimizer/lib/fetchRuntimeParameters.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ try {
2929
}
3030

3131
const {
32-
AMP_CACHE_HOST,
32+
AMP_CACHE_HOSTS,
3333
AMP_RUNTIME_CSS_PATH,
3434
AMP_VALIDATION_RULES_URL,
3535
appendRuntimeVersion,
36+
DEFAULT_AMP_CACHE_HOST,
3637
} = require('./AmpConstants.js');
3738

3839
const KEY_VALIDATOR_RULES = 'validator-rules';
@@ -214,12 +215,12 @@ async function fetchLatestRuntimeData_({config, ampUrlPrefix, lts}, versionKey =
214215
version: await config.runtimeVersion.currentVersion({ampUrlPrefix, lts}),
215216
maxAge: MaxAge.create(AMP_RUNTIME_MAX_AGE).toObject(),
216217
};
217-
if (!ampRuntimeData.version && ampUrlPrefix && ampUrlPrefix !== AMP_CACHE_HOST) {
218+
if (!ampRuntimeData.version && ampUrlPrefix && !AMP_CACHE_HOSTS.includes(ampUrlPrefix)) {
218219
config.log.error(
219-
`Could not download runtime version from ${ampUrlPrefix}. Falling back to ${AMP_CACHE_HOST}`
220+
`Could not download runtime version from ${ampUrlPrefix}. Falling back to ${DEFAULT_AMP_CACHE_HOST}`
220221
);
221222
ampRuntimeData = await fetchLatestRuntimeData_(
222-
{config, ampUrlPrefix: AMP_CACHE_HOST, lts},
223+
{config, ampUrlPrefix: DEFAULT_AMP_CACHE_HOST, lts},
223224
versionKey
224225
);
225226
} else if (!ampRuntimeData.version) {
@@ -240,15 +241,16 @@ async function fetchLatestRuntimeData_({config, ampUrlPrefix, lts}, versionKey =
240241
async function fetchAmpRuntimeStyles_(config, ampUrlPrefix, ampRuntimeVersion) {
241242
if (ampUrlPrefix && !isAbsoluteUrl_(ampUrlPrefix)) {
242243
config.log.warn(
243-
`AMP runtime styles cannot be fetched from relative ampUrlPrefix, please use the 'ampRuntimeStyles' parameter to provide the correct runtime style. Falling back to latest v0.css on ${AMP_CACHE_HOST}`
244+
`AMP runtime styles cannot be fetched from relative ampUrlPrefix, please use the 'ampRuntimeStyles' parameter to provide the correct runtime style. Falling back to latest v0.css on ${DEFAULT_AMP_CACHE_HOST}`
244245
);
245246
// Gracefully fallback to latest runtime version
246-
ampUrlPrefix = AMP_CACHE_HOST;
247+
ampUrlPrefix = DEFAULT_AMP_CACHE_HOST;
247248
ampRuntimeVersion = ampRuntimeVersion || (await config.runtimeVersion.currentVersion());
248249
}
249250
// Construct the AMP runtime CSS download URL, the default is: https://cdn.ampproject.org/rtv/${ampRuntimeVersion}/v0.css
250251
const runtimeCssUrl =
251-
appendRuntimeVersion(ampUrlPrefix || AMP_CACHE_HOST, ampRuntimeVersion) + AMP_RUNTIME_CSS_PATH;
252+
appendRuntimeVersion(ampUrlPrefix || DEFAULT_AMP_CACHE_HOST, ampRuntimeVersion) +
253+
AMP_RUNTIME_CSS_PATH;
252254
// Fetch runtime styles
253255
const styles = await downloadAmpRuntimeStyles_(config, runtimeCssUrl);
254256
if (!styles) {
@@ -257,7 +259,7 @@ async function fetchAmpRuntimeStyles_(config, ampUrlPrefix, ampRuntimeVersion) {
257259
// Try to download latest from cdn.ampproject.org instead
258260
return fetchAmpRuntimeStyles_(
259261
config,
260-
AMP_CACHE_HOST,
262+
DEFAULT_AMP_CACHE_HOST,
261263
await config.runtimeVersion.currentVersion()
262264
);
263265
} else {

packages/optimizer/lib/transformers/AddMandatoryTags.js

Lines changed: 108 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -24,117 +24,11 @@ const {
2424
createElement,
2525
firstChildByTag,
2626
} = require('../NodeUtils');
27-
const {AMP_FORMATS, AMP_TAGS} = require('../AmpConstants');
27+
const {AMP_FORMATS, AMP_TAGS, DEFAULT_AMP_CACHE_HOST} = require('../AmpConstants');
2828

2929
const DEFAULT_FORMAT = 'AMP';
3030
const AUTO_GENERATED_MARKER = 'data-auto';
3131

32-
const BOILERPLATES = {
33-
AMP: [
34-
{
35-
matcher: {
36-
tagName: 'meta',
37-
attribs: {
38-
charset: 'utf-8',
39-
},
40-
},
41-
node: {
42-
tagName: 'meta',
43-
attribs: {
44-
charset: 'utf-8',
45-
},
46-
},
47-
},
48-
{
49-
matcher: {
50-
tagName: 'meta',
51-
attribs: {
52-
name: 'viewport',
53-
},
54-
},
55-
node: {
56-
tagName: 'meta',
57-
attribs: {
58-
name: 'viewport',
59-
content: 'width=device-width,minimum-scale=1,initial-scale=1',
60-
},
61-
},
62-
},
63-
{
64-
matcher: {
65-
tagName: 'noscript',
66-
},
67-
node: {
68-
tagName: 'noscript',
69-
children: [
70-
{
71-
tagName: 'style',
72-
attribs: {
73-
'amp-boilerplate': '',
74-
},
75-
// eslint-disable-next-line max-len
76-
text:
77-
'body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}',
78-
},
79-
],
80-
},
81-
},
82-
{
83-
matcher: {
84-
tagName: 'style',
85-
attribs: {
86-
'amp-boilerplate': '',
87-
},
88-
},
89-
node: {
90-
tagName: 'style',
91-
attribs: {
92-
'amp-boilerplate': '',
93-
},
94-
// eslint-disable-next-line max-len
95-
text:
96-
'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}}',
97-
},
98-
},
99-
{
100-
matcher: {
101-
tagName: 'script',
102-
attribs: {
103-
src: /^https:\/\/.+\/v0\.js$/,
104-
},
105-
},
106-
node: {
107-
tagName: 'script',
108-
attribs: {
109-
async: '',
110-
src: 'https://cdn.ampproject.org/v0.js',
111-
},
112-
},
113-
},
114-
{
115-
matcher: {
116-
tagName: 'link',
117-
attribs: {
118-
rel: 'canonical',
119-
},
120-
},
121-
node: {
122-
tagName: 'link',
123-
attribs: {
124-
rel: 'canonical',
125-
href: (params, log) => {
126-
if (!params.canonical) {
127-
log.warn('No canonical param is given. Setting canonical href to `.`');
128-
params.canonical = '.';
129-
}
130-
return params.canonical;
131-
},
132-
},
133-
},
134-
},
135-
],
136-
};
137-
13832
/**
13933
* AddMandatoryTags - this transformer will automatically add all missing tags required by a valid AMP document.
14034
*
@@ -164,8 +58,114 @@ class AddMandatoryTags {
16458
return;
16559
}
16660

61+
const boilerplates = {
62+
AMP: [
63+
{
64+
matcher: {
65+
tagName: 'meta',
66+
attribs: {
67+
charset: 'utf-8',
68+
},
69+
},
70+
node: {
71+
tagName: 'meta',
72+
attribs: {
73+
charset: 'utf-8',
74+
},
75+
},
76+
},
77+
{
78+
matcher: {
79+
tagName: 'meta',
80+
attribs: {
81+
name: 'viewport',
82+
},
83+
},
84+
node: {
85+
tagName: 'meta',
86+
attribs: {
87+
name: 'viewport',
88+
content: 'width=device-width,minimum-scale=1,initial-scale=1',
89+
},
90+
},
91+
},
92+
{
93+
matcher: {
94+
tagName: 'noscript',
95+
},
96+
node: {
97+
tagName: 'noscript',
98+
children: [
99+
{
100+
tagName: 'style',
101+
attribs: {
102+
'amp-boilerplate': '',
103+
},
104+
// eslint-disable-next-line max-len
105+
text:
106+
'body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}',
107+
},
108+
],
109+
},
110+
},
111+
{
112+
matcher: {
113+
tagName: 'style',
114+
attribs: {
115+
'amp-boilerplate': '',
116+
},
117+
},
118+
node: {
119+
tagName: 'style',
120+
attribs: {
121+
'amp-boilerplate': '',
122+
},
123+
// eslint-disable-next-line max-len
124+
text:
125+
'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}}',
126+
},
127+
},
128+
{
129+
matcher: {
130+
tagName: 'script',
131+
attribs: {
132+
src: /^https:\/\/.+\/v0\.js$/,
133+
},
134+
},
135+
node: {
136+
tagName: 'script',
137+
attribs: {
138+
async: '',
139+
src: (params.ampUrlPrefix || DEFAULT_AMP_CACHE_HOST) + '/v0.js',
140+
},
141+
},
142+
},
143+
{
144+
matcher: {
145+
tagName: 'link',
146+
attribs: {
147+
rel: 'canonical',
148+
},
149+
},
150+
node: {
151+
tagName: 'link',
152+
attribs: {
153+
rel: 'canonical',
154+
href: (params, log) => {
155+
if (!params.canonical) {
156+
log.warn('No canonical param is given. Setting canonical href to `.`');
157+
params.canonical = '.';
158+
}
159+
return params.canonical;
160+
},
161+
},
162+
},
163+
},
164+
],
165+
};
166+
167167
// Only supports AMP for websites
168-
const boilerplateSpec = BOILERPLATES[this.format];
168+
const boilerplateSpec = boilerplates[this.format];
169169
if (!boilerplateSpec) {
170170
this.log_.info('Unsupported AMP format', this.format);
171171
return;

packages/optimizer/lib/transformers/AmpStoryCssTransformer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {
1313
firstChildByTag,
1414
appendChild,
1515
} = require('../NodeUtils');
16-
const {AMP_CACHE_HOST} = require('../AmpConstants.js');
16+
const {DEFAULT_AMP_CACHE_HOST} = require('../AmpConstants.js');
1717

1818
// This string should not be modified, even slightly. This string is strictly
1919
// checked by the validator.
@@ -139,7 +139,7 @@ function appendAmpStoryCssLink(head) {
139139
'amp-extension': 'amp-story',
140140
// We rely on the `RewriteAmpUrls` transformer to modify this to
141141
// the correct LTS or correct rtv path.
142-
'href': `${AMP_CACHE_HOST}/v0/amp-story-1.0.css`,
142+
'href': `${DEFAULT_AMP_CACHE_HOST}/v0/amp-story-1.0.css`,
143143
});
144144
appendChild(head, ampStoryCssLink);
145145
}

packages/optimizer/lib/transformers/AutoExtensionImporter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const {
2323
hasAttribute,
2424
} = require('../NodeUtils');
2525
const {findMetaViewport, findRuntimeScript} = require('../HtmlDomHelper');
26-
const {AMP_FORMATS, AMP_CACHE_HOST} = require('../AmpConstants');
26+
const {AMP_FORMATS, DEFAULT_AMP_CACHE_HOST} = require('../AmpConstants');
2727

2828
const BIND_SHORT_FORM_PREFIX = 'bind';
2929
const AMP_BIND_DATA_ATTRIBUTE_PREFIX = 'data-amp-bind-';
@@ -188,7 +188,7 @@ class AutoExtensionImporter {
188188
}
189189

190190
// Use cdn.ampproject.org as default, RewriteUrlTransformer will change this in case of self-hosting
191-
const host = AMP_CACHE_HOST;
191+
const host = DEFAULT_AMP_CACHE_HOST;
192192
for (const extensionName of extensionsToImport) {
193193
if (existingImports.has(extensionName)) {
194194
continue;

packages/optimizer/lib/transformers/ReorderHeadTransformer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class HeadNodes {
191191
rel === 'prefetch' ||
192192
rel === 'dns-prefetch' ||
193193
rel === 'preconnect' ||
194-
rel == 'modulepreload'
194+
rel === 'modulepreload'
195195
) {
196196
this._resourceHintLinks.push(node);
197197
return;

packages/optimizer/lib/transformers/RewriteAmpUrls.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const {
2323
insertBefore,
2424
remove,
2525
} = require('../NodeUtils');
26-
const {AMP_CACHE_HOST} = require('../AmpConstants.js');
26+
const {AMP_CACHE_HOSTS} = require('../AmpConstants.js');
2727
const {findMetaViewport} = require('../HtmlDomHelper');
2828
const {calculateHost} = require('../RuntimeHostHelper');
2929

@@ -141,13 +141,15 @@ class RewriteAmpUrls {
141141

142142
_usesAmpCacheUrl(url) {
143143
if (!url) {
144-
return;
144+
return false;
145145
}
146-
return url.startsWith(AMP_CACHE_HOST);
146+
// check if url starts with one of string array
147+
return AMP_CACHE_HOSTS.some((host) => url.startsWith(host));
147148
}
148149

149150
_replaceUrl(url, host) {
150-
return host + url.substring(AMP_CACHE_HOST.length);
151+
const existingHost = AMP_CACHE_HOSTS.find((ampCacheHost) => url.startsWith(ampCacheHost));
152+
return host + url.substring(existingHost.length);
151153
}
152154

153155
_addEsm(scriptNode, preloadEnabled) {

packages/optimizer/spec/helpers/validatorInstance.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ let instance = null;
66
module.exports = {
77
get: () => {
88
if (!instance) {
9+
console.error('Validator instance created: ' + path.join(__dirname, 'validator.js'));
910
instance = validator.getInstance(path.join(__dirname, 'validator.js'));
1011
}
1112
return instance;

0 commit comments

Comments
 (0)