Skip to content

Commit cada9d4

Browse files
committed
docs(prefetch): add missing jsdoc comments
1 parent bfa8917 commit cada9d4

1 file changed

Lines changed: 53 additions & 9 deletions

File tree

src/prefetch.mjs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1-
// Inspired by the prefetching logic in Gatsby.js
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+
const preFetched = {};
220

3-
const support = function (feature) {
21+
/**
22+
* Checks if a feature on `link` is natively supported.
23+
* Examples of features include `prefetch` and `preload`.
24+
* @param {string} feature - name of the feature to test
25+
* @return {Boolean} whether the feature is supported
26+
*/
27+
function support(feature) {
428
if (typeof document === `undefined`) {
529
return false;
630
}
@@ -14,7 +38,12 @@ const support = function (feature) {
1438
}
1539
};
1640

17-
const linkPrefetchStrategy = function (url) {
41+
/**
42+
* Fetches a given URL using `<link rel=prefetch>`
43+
* @param {string} url - the URL to fetch
44+
* @return {Object} a Promise
45+
*/
46+
function linkPrefetchStrategy(url) {
1847
return new Promise((resolve, reject) => {
1948
if (typeof document === `undefined`) {
2049
reject();
@@ -35,7 +64,12 @@ const linkPrefetchStrategy = function (url) {
3564
});
3665
};
3766

38-
const xhrPrefetchStrategy = function (url) {
67+
/**
68+
* Fetches a given URL using XMLHttpRequest
69+
* @param {string} url - the URL to fetch
70+
* @return {Object} a Promise
71+
*/
72+
function xhrPrefetchStrategy(url) {
3973
return new Promise((resolve, reject) => {
4074
const req = new XMLHttpRequest();
4175
req.open(`GET`, url, true);
@@ -53,7 +87,13 @@ const xhrPrefetchStrategy = function (url) {
5387
});
5488
};
5589

56-
const highPriFetchStrategy = function (url) {
90+
/**
91+
* Fetches a given URL using the Fetch API. Falls back
92+
* to XMLHttpRequest if the API is not supported.
93+
* @param {string} url - the URL to fetch
94+
* @return {Object} a Promise
95+
*/
96+
function highPriFetchStrategy(url) {
5797
return new Promise((resolve, reject) => {
5898
// TODO: Investigate using preload for high-priority
5999
// fetches. May have to sniff file-extension to provide
@@ -79,9 +119,13 @@ const supportedPrefetchStrategy = support(`prefetch`)
79119
? linkPrefetchStrategy
80120
: xhrPrefetchStrategy;
81121

82-
const preFetched = {};
83-
84-
const prefetch = function (url, priority) {
122+
/**
123+
* Prefetch a given URL with an optional preferred fetch priority
124+
* @param {string} url - the URL to fetch
125+
* @param {string} priority - preferred fetch priority (`low` or `high`)
126+
* @return {Object} a Promise
127+
*/
128+
function prefetcher(url, priority) {
85129
return new Promise(resolve => {
86130
if ('connection' in navigator) {
87131
// Don't prefetch if the user is on 2G..
@@ -117,4 +161,4 @@ const prefetch = function (url, priority) {
117161
});
118162
};
119163

120-
export default prefetch;
164+
export default prefetcher;

0 commit comments

Comments
 (0)