|
| 1 | +/** |
| 2 | + * Portions copyright 2018 Google Inc. |
| 3 | + * Inspired by Gatsby's prefetching logic, with those portions |
| 4 | + * remaining MIT. Additions include support for Fetch API, |
| 5 | + * XHR switching, SaveData and Effective Connection Type checking. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + **/ |
| 19 | +import { inBrowser } from './utils' |
| 20 | + |
| 21 | +const preFetched = {} |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks if a feature on `link` is natively supported. |
| 25 | + * Examples of features include `prefetch` and `preload`. |
| 26 | + * @param {string} feature - name of the feature to test |
| 27 | + * @return {Boolean} whether the feature is supported |
| 28 | + */ |
| 29 | +function support(feature) { |
| 30 | + if (!inBrowser) { |
| 31 | + return |
| 32 | + } |
| 33 | + const link = document.createElement('link') |
| 34 | + return link.relList && link.relList.supports && link.relList.supports(feature) |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Fetches a given URL using `<link rel=prefetch>` |
| 39 | + * @param {string} url - the URL to fetch |
| 40 | + * @return {Object} a Promise |
| 41 | + */ |
| 42 | +function linkPrefetchStrategy(url) { |
| 43 | + return new Promise((resolve, reject) => { |
| 44 | + const link = document.createElement(`link`) |
| 45 | + link.rel = `prefetch` |
| 46 | + link.href = url |
| 47 | + |
| 48 | + link.addEventListener('load', resolve) |
| 49 | + link.addEventListener('error', reject) |
| 50 | + |
| 51 | + document.head.appendChild(link) |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Fetches a given URL using XMLHttpRequest |
| 57 | + * @param {string} url - the URL to fetch |
| 58 | + * @return {Object} a Promise |
| 59 | + */ |
| 60 | +function xhrPrefetchStrategy(url) { |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + const req = new XMLHttpRequest() |
| 63 | + |
| 64 | + req.open(`GET`, url, (req.withCredentials = true)) |
| 65 | + |
| 66 | + req.addEventListener('load', () => { |
| 67 | + req.status === 200 ? resolve() : reject() |
| 68 | + }) |
| 69 | + |
| 70 | + req.send() |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Fetches a given URL using the Fetch API. Falls back |
| 76 | + * to XMLHttpRequest if the API is not supported. |
| 77 | + * @param {string} url - the URL to fetch |
| 78 | + * @return {Object} a Promise |
| 79 | + */ |
| 80 | +function highPriFetchStrategy(url) { |
| 81 | + // TODO: Investigate using preload for high-priority |
| 82 | + // fetches. May have to sniff file-extension to provide |
| 83 | + // valid 'as' values. In the future, we may be able to |
| 84 | + // use Priority Hints here. |
| 85 | + // |
| 86 | + // As of 2018, fetch() is high-priority in Chrome |
| 87 | + // and medium-priority in Safari. |
| 88 | + return self.fetch |
| 89 | + ? fetch(url, { credentials: `include` }) |
| 90 | + : xhrPrefetchStrategy(url) |
| 91 | +} |
| 92 | + |
| 93 | +const supportedPrefetchStrategy = support('prefetch') |
| 94 | + ? linkPrefetchStrategy |
| 95 | + : xhrPrefetchStrategy |
| 96 | + |
| 97 | +/** |
| 98 | + * Prefetch a given URL with an optional preferred fetch priority |
| 99 | + * @param {String} url - the URL to fetch |
| 100 | + * @param {Boolean} isPriority - if is "high" priority |
| 101 | + * @param {Object} conn - navigator.connection (internal) |
| 102 | + * @return {Object} a Promise |
| 103 | + */ |
| 104 | +function prefetcher(url, isPriority) { |
| 105 | + if (preFetched[url]) { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + // Wanna do something on catch()? |
| 110 | + return (isPriority ? highPriFetchStrategy : supportedPrefetchStrategy)( |
| 111 | + url |
| 112 | + ).then(() => { |
| 113 | + preFetched[url] = true |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +export default prefetcher |
0 commit comments