* Preconnects to a URL. Always also does a dns-prefetch because * browser support for that is better. * @param {!./service/ampdoc-impl.AmpDoc} ampdoc * @param {string} url * @param {boolean=} opt_alsoConnecting Set this flag if you also just * did or are about to connect to this ho
(ampdoc, url, opt_alsoConnecting)
| 136 | * @private |
| 137 | */ |
| 138 | url_(ampdoc, url, opt_alsoConnecting) { |
| 139 | if (!this.isInterestingUrl_(url)) { |
| 140 | return; |
| 141 | } |
| 142 | const {origin} = parseUrlDeprecated(url); |
| 143 | const now = Date.now(); |
| 144 | const lastPreconnectTimeout = this.origins_[origin]; |
| 145 | if (lastPreconnectTimeout && now < lastPreconnectTimeout) { |
| 146 | if (opt_alsoConnecting) { |
| 147 | this.origins_[origin] = now + ACTIVE_CONNECTION_TIMEOUT_MS; |
| 148 | } |
| 149 | return; |
| 150 | } |
| 151 | // If we are about to use the connection, don't re-preconnect for |
| 152 | // 180 seconds. |
| 153 | const timeout = opt_alsoConnecting |
| 154 | ? ACTIVE_CONNECTION_TIMEOUT_MS |
| 155 | : PRECONNECT_TIMEOUT_MS; |
| 156 | this.origins_[origin] = now + timeout; |
| 157 | // If we know that preconnect is supported, there is no need to do |
| 158 | // dedicated dns-prefetch. |
| 159 | let dns; |
| 160 | if (!this.features_.preconnect) { |
| 161 | dns = this.document_.createElement('link'); |
| 162 | dns.setAttribute('rel', 'dns-prefetch'); |
| 163 | dns.setAttribute('href', origin); |
| 164 | this.head_.appendChild(dns); |
| 165 | } |
| 166 | const preconnect = this.document_.createElement('link'); |
| 167 | preconnect.setAttribute('rel', 'preconnect'); |
| 168 | preconnect.setAttribute('href', origin); |
| 169 | preconnect.setAttribute('referrerpolicy', 'origin'); |
| 170 | this.head_.appendChild(preconnect); |
| 171 | |
| 172 | // Remove the tags eventually to free up memory. |
| 173 | this.timer_.delay(() => { |
| 174 | if (dns && dns.parentNode) { |
| 175 | dns.parentNode.removeChild(dns); |
| 176 | } |
| 177 | if (preconnect.parentNode) { |
| 178 | preconnect.parentNode.removeChild(preconnect); |
| 179 | } |
| 180 | }, 10000); |
| 181 | |
| 182 | this.preconnectPolyfill_(ampdoc, origin); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Asks the browser to preload a URL. Always also does a preconnect |
no test coverage detected