forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamp.js
157 lines (146 loc) · 5.14 KB
/
amp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* The entry point for AMP Runtime (v0.js) when AMP Runtime = AMP Doc.
*/
// src/polyfills.js must be the first import.
import './polyfills';
import {TickLabel_Enum} from '#core/constants/enums';
import {whenDocumentComplete} from '#core/document/ready';
import * as mode from '#core/mode';
import {Services} from '#service';
import {installDocService} from '#service/ampdoc-impl';
import {
installAmpdocServices,
installBuiltinElements,
installRuntimeServices,
} from '#service/core-services';
import {stubElementsForDoc} from '#service/custom-element-registry';
import {installPerformanceService} from '#service/performance-impl';
import {installPlatformService} from '#service/platform-impl';
import {installAutoLightboxExtension} from './auto-lightbox';
import {startupChunk} from './chunk';
import {markUnresolvedElements} from './custom-element';
import {installErrorReporting} from './error-reporting';
import {fontStylesheetTimeout} from './font-stylesheet-timeout';
import {maybeTrackImpression} from './impression';
import {getMode} from './mode';
import {preconnectToOrigin} from './preconnect';
import {installPullToRefreshBlocker} from './pull-to-refresh';
import {adoptWithMultidocDeps} from './runtime';
import {installStandaloneExtension} from './standalone';
import {
installStylesForDoc,
makeBodyVisible,
makeBodyVisibleRecovery,
} from './style-installer';
import {maybeValidate} from './validator-integration';
import {cssText as ampDocCss} from '../build/ampdoc.css';
import {cssText as ampSharedCss} from '../build/ampshared.css';
/**
* Execute the bootstrap
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {!./service/performance-impl.Performance} perf
*/
function bootstrap(ampdoc, perf) {
startupChunk(self.document, function services() {
// Core services.
installRuntimeServices(self);
installAmpdocServices(ampdoc);
// We need the core services (viewer/resources) to start instrumenting
perf.coreServicesAvailable();
maybeTrackImpression(self);
});
startupChunk(self.document, function adoptWindow() {
adoptWithMultidocDeps(self);
});
startupChunk(self.document, function builtins() {
// Builtins.
installBuiltinElements(self);
});
startupChunk(self.document, function stub() {
// Pre-stub already known elements.
stubElementsForDoc(ampdoc);
whenDocumentComplete(self.document).then(() => markUnresolvedElements());
});
startupChunk(
self.document,
function final() {
if (!IS_SXG) {
installPullToRefreshBlocker(self);
}
installAutoLightboxExtension(ampdoc);
installStandaloneExtension(ampdoc);
maybeValidate(self);
makeBodyVisible(self.document);
preconnectToOrigin(self.document);
},
/* makes the body visible */ true
);
startupChunk(self.document, function finalTick() {
perf.tick(TickLabel_Enum.END_INSTALL_STYLES);
Services.resourcesForDoc(ampdoc).ampInitComplete();
// TODO(erwinm): move invocation of the `flush` method when we have the
// new ticks in place to batch the ticks properly.
perf.flush();
});
}
// Store the originalHash as early as possible. Trying to debug:
// https://github.com/ampproject/amphtml/issues/6070
if (self.location) {
self.location['originalHash'] = self.location.hash;
}
/** @type {!./service/ampdoc-impl.AmpDocService} */
let ampdocService;
// We must under all circumstances call makeBodyVisible.
// It is much better to have AMP tags not rendered than having
// a completely blank page.
try {
// Should happen first.
installErrorReporting(self); // Also calls makeBodyVisibleRecovery on errors.
// Declare that this runtime will support a single root doc. Should happen
// as early as possible.
installDocService(self, /* isSingleDoc */ true);
ampdocService = Services.ampdocServiceFor(self);
} catch (e) {
// In case of an error call this.
makeBodyVisibleRecovery(self.document);
throw e;
}
startupChunk(self.document, function initial() {
/** @const {!./service/ampdoc-impl.AmpDoc} */
const ampdoc = ampdocService.getAmpDoc(self.document);
installPlatformService(self);
installPerformanceService(self);
/** @const {!./service/performance-impl.Performance} */
const perf = Services.performanceFor(self);
if (mode.isEsm()) {
perf.addEnabledExperiment('esm');
}
fontStylesheetTimeout(self);
perf.tick(TickLabel_Enum.INSTALL_STYLES);
if (mode.isEsm()) {
bootstrap(ampdoc, perf);
return;
}
installStylesForDoc(
ampdoc,
ampDocCss + ampSharedCss,
() => bootstrap(ampdoc, perf),
/* opt_isRuntimeCss */ true,
/* opt_ext */ 'amp-runtime'
);
});
// Output a message to the console and add an attribute to the <html>
// tag to give some information that can be used in error reports.
// (At least by sophisticated users).
if (self.console) {
(console.info || console.log).call(
console,
`Powered by AMP ⚡ HTML – Version ${mode.version()}`,
self.location.href
);
}
// This code is eleminated in prod build through a babel transformer.
if (getMode().localDev) {
self.document.documentElement.setAttribute('esm', mode.isEsm() ? 1 : 0);
}
self.document.documentElement.setAttribute('amp-version', mode.version());