-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for same-site prerendering with Speculation Rules API #258
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9551dd1
initial commit to add support for same-origin prerendering with Specu…
hadyan 5b1186f
reset author
hadyan 96d1de8
refactor, best practices, and minor logic update
hadyan 6314b81
refactor, best practices, and minor logic update
hadyan 82e2a86
avoid prefetching the link that has been prerendered in listen
hadyan 94508f2
create prerenderLimit as a constant to cater for the current Spec Rul…
hadyan 0134735
refactor prerender specific checks and compliance with eslint-config-…
hadyan d91867c
create new tests for prerender with speculation rules
hadyan 50fc351
bug fix: addSpeculationRules does not resolve
hadyan 5bca8ef
adding prerendering doc in README
hadyan 1c183cd
Update README.md
addyosmani baeb743
removed reference to outdated OT
hadyan 8d54fe0
Merge branch 'speculationrules' of /~https://github.com/hadyan/quicklin…
hadyan 54eea9b
fixed promise rejection inside catch handler issue
hadyan a777f4e
Update src/index.mjs
addyosmani 0b69e2e
Update src/index.mjs
addyosmani 334d3f5
Update src/prerender.mjs
addyosmani 42cae5a
update return value documentation
hadyan 8757882
remove conn param from prefetch and prerender functions
hadyan 0443c72
updated addSpeculationRules return value
hadyan bb371fc
restored conn param
hadyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,17 @@ | |
import throttle from 'throttles'; | ||
import {priority, supported} from './prefetch.mjs'; | ||
import requestIdleCallback from './request-idle-callback.mjs'; | ||
import {isSameOrigin, addSpeculationRules, hasSpecRulesSupport, isSpecRulesExists} from './prerender.mjs'; | ||
|
||
// Cache of URLs we've prefetched | ||
// Its `size` is compared against `opts.limit` value. | ||
const toPrefetch = new Set(); | ||
|
||
// Cache of URLs we've prerendered | ||
const toPrerender = new Set(); | ||
// global var to keep prerenderAndPrefer option | ||
let shouldPrerenderAndPrefetch = false; | ||
|
||
/** | ||
* Determine if the anchor tag should be prefetched. | ||
* A filter can be a RegExp, Function, or Array of both. | ||
|
@@ -36,6 +42,25 @@ function isIgnored(node, filter) { | |
: (filter.test || filter).call(filter, node.href, node); | ||
} | ||
|
||
/** | ||
* Checks network conditions | ||
* @param {NetworkInformation} conn The connection information to be checked | ||
* @return {Boolean|Object} Error Object if the constrainsts are met or boolean otherwise | ||
*/ | ||
function checkConnection (conn) { | ||
if (conn) { | ||
// Don't pre* if using 2G or if Save-Data is enabled. | ||
if (conn.saveData) { | ||
return new Error('Save-Data is enabled'); | ||
} | ||
if (/2g/.test(conn.effectiveType)) { | ||
return new Error('network conditions are poor'); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Prefetch an array of URLs if the user's effective | ||
* connection type and data-saver preferences suggests | ||
|
@@ -56,6 +81,8 @@ function isIgnored(node, filter) { | |
* @param {Function} [options.onError] - Error handler for failed `prefetch` requests | ||
* @param {Function} [options.hrefFn] - Function to use to build the URL to prefetch. | ||
* If it's not a valid function, then it will use the entry href. | ||
* @param {Boolean} [options.prerender] - Option to switch from prefetching and use prerendering only | ||
* @param {Boolean} [options.prerenderAndPrefetch] - Option to use both prerendering and prefetching | ||
* @return {Function} | ||
*/ | ||
export function listen(options) { | ||
|
@@ -73,7 +100,12 @@ export function listen(options) { | |
|
||
const timeoutFn = options.timeoutFn || requestIdleCallback; | ||
const hrefFn = typeof options.hrefFn === 'function' && options.hrefFn; | ||
|
||
|
||
const shouldOnlyPrerender = options.prerender || false; | ||
shouldPrerenderAndPrefetch = options.prerenderAndPrefetch || false; | ||
|
||
const prerenderLimit = 1; | ||
|
||
const setTimeoutIfDelay = (callback, delay) => { | ||
if (!delay) { | ||
callback(); | ||
|
@@ -96,14 +128,32 @@ export function listen(options) { | |
if (hrefsInViewport.indexOf(entry.href) === -1) return; | ||
|
||
observer.unobserve(entry); | ||
// Do not prefetch if will match/exceed limit | ||
if (toPrefetch.size < limit) { | ||
|
||
// prerender, if.. | ||
// either it's the prerender + prefetch mode or it's prerender *only* mode | ||
// && no link has been prerendered before (no spec rules defined) | ||
if (shouldPrerenderAndPrefetch || shouldOnlyPrerender) { | ||
if (toPrerender.size < prerenderLimit) { | ||
prerender(hrefFn ? hrefFn(entry) : entry.href).catch(err => { | ||
if (options.onError) { | ||
options.onError(err); | ||
}else { | ||
throw err; | ||
} | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
// Do not prefetch if will match/exceed limit and user has not switched to shouldOnlyPrerender mode | ||
if (toPrefetch.size < limit && !shouldOnlyPrerender) { | ||
toAdd(() => { | ||
prefetch(hrefFn ? hrefFn(entry) : entry.href, options.priority).then(isDone).catch(err => { | ||
isDone(); if (options.onError) options.onError(err); | ||
}); | ||
}); | ||
} | ||
|
||
}, delay); | ||
} | ||
// On exit | ||
|
@@ -141,7 +191,6 @@ export function listen(options) { | |
}; | ||
} | ||
|
||
|
||
/** | ||
* Prefetch a given URL with an optional preferred fetch priority | ||
* @param {String} url - the URL to fetch | ||
|
@@ -150,14 +199,13 @@ export function listen(options) { | |
* @return {Object} a Promise | ||
*/ | ||
export function prefetch(url, isPriority, conn) { | ||
if (conn = navigator.connection) { | ||
// Don't prefetch if using 2G or if Save-Data is enabled. | ||
if (conn.saveData) { | ||
return Promise.reject(new Error('Cannot prefetch, Save-Data is enabled')); | ||
} | ||
if (/2g/.test(conn.effectiveType)) { | ||
return Promise.reject(new Error('Cannot prefetch, network conditions are poor')); | ||
} | ||
let chkConn = checkConnection(conn = navigator.connection); | ||
if (chkConn instanceof Error) { | ||
return Promise.reject(new Error('Cannot prefetch, '+chkConn.message)); | ||
} | ||
|
||
if(toPrerender.size > 0 && !shouldPrerenderAndPrefetch) { | ||
console.warn('[Warning] You are using both prefetching and prerendering on the same document'); | ||
} | ||
|
||
// Dev must supply own catch() | ||
|
@@ -175,3 +223,45 @@ export function prefetch(url, isPriority, conn) { | |
}) | ||
); | ||
} | ||
|
||
/** | ||
* Prerender a given URL | ||
* @param {String} url - the URL to fetch | ||
* @param {Object} [conn] - navigator.connection (internal) | ||
* @return {Object} a Promise | ||
*/ | ||
export function prerender(urls, conn) { | ||
let chkConn = checkConnection(conn = navigator.connection); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same problem with assigning to a parameter here. |
||
if (chkConn instanceof Error) { | ||
return Promise.reject(new Error('Cannot prerender, '+chkConn.message)); | ||
} | ||
|
||
// prerendering preconditions: | ||
// 1) whether UA supports spec rules.. If not, fallback to prefetch | ||
if (!hasSpecRulesSupport()) { | ||
prefetch (urls); | ||
return Promise.reject(new Error('This browser does not support the speculation rules API. Falling back to prefetch.')); | ||
} | ||
|
||
// 2) whether spec rules is already defined (and with this we also covered when we have created spec rules before) | ||
if (isSpecRulesExists()) { | ||
return Promise.reject(new Error('Speculation Rules is already defined and cannot be altered.')); | ||
} | ||
|
||
// 3) whether it's a same origin url, | ||
for (const url of [].concat(urls)) { | ||
if (!isSameOrigin(url)) { | ||
return Promise.reject(new Error('Only same origin URLs are allowed: ' + url)); | ||
} | ||
|
||
toPrerender.add(url); | ||
} | ||
|
||
// check if both prerender and prefetch exists.. throw a warning but still proceed | ||
if (toPrefetch.size > 0 && !shouldPrerenderAndPrefetch) { | ||
console.warn('[Warning] You are using both prefetching and prerendering on the same document'); | ||
} | ||
|
||
let addSpecRules = addSpeculationRules(toPrerender); | ||
return (addSpecRules === true) ? Promise.resolve() : Promise.reject(addSpecRules); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Portions copyright 2018 Google Inc. | ||
* Inspired by Gatsby's prefetching logic, with those portions | ||
* remaining MIT. Additions include support for Fetch API, | ||
* XHR switching, SaveData and Effective Connection Type checking. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
**/ | ||
/** | ||
* Checks if the given string is a same origin url | ||
* @param {string} str - the URL to check | ||
* @return {Boolean} true for same origin url | ||
*/ | ||
export function isSameOrigin(str) { | ||
return window.location.origin === (new URL(str, window.location.href)).origin; | ||
} | ||
|
||
/** | ||
* Add a given set of urls to the speculation rules | ||
* @param {Set} toPrerender - the URLs to add to speculation rules | ||
* @return {Boolean|Object} boolean or Error Object | ||
*/ | ||
export function addSpeculationRules(urlsToPrerender) { | ||
hadyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let specScript = document.createElement('script'); | ||
specScript.type = 'speculationrules'; | ||
specScript.text = '{"prerender":[{"source": "list","urls": ["'+Array.from(urlsToPrerender).join('","')+'"]}]}'; | ||
try { | ||
document.head.appendChild(specScript); | ||
}catch(e) { | ||
return e; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Check whether UA supports Speculation Rules API | ||
* @return {Boolean} whether UA has support for Speculation Rules API | ||
*/ | ||
export function hasSpecRulesSupport() { | ||
return HTMLScriptElement.supports('speculationrules'); | ||
} | ||
|
||
/** | ||
* Check whether Spec Rules is already defined in the document | ||
* @return {Boolean} whether Spec Rules exists/already defined | ||
*/ | ||
export function isSpecRulesExists() { | ||
return document.querySelector('script[type="speculationrules"]'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Prefetch: Basic Usage</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" media="screen" href="main.css"> | ||
<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script> | ||
</head> | ||
|
||
<body> | ||
<a href="1.html">Link 1</a> | ||
<a href="2.html">Link 2</a> | ||
<a href="3.html">Link 3</a> | ||
<section id="stuff"> | ||
<a href="main.css">CSS</a> | ||
</section> | ||
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a> | ||
<script src="../dist/quicklink.umd.js"></script> | ||
<script> | ||
quicklink.listen({prerenderAndPrefetch: true}); | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Prefetch: Basic Usage</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" media="screen" href="main.css"> | ||
<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script> | ||
</head> | ||
|
||
<body> | ||
<a href="1.html">Link 1</a> | ||
<a href="2.html">Link 2</a> | ||
<a href="3.html">Link 3</a> | ||
<section id="stuff"> | ||
<a href="main.css">CSS</a> | ||
</section> | ||
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a> | ||
<script src="../dist/quicklink.umd.js"></script> | ||
<script> | ||
quicklink.listen({prerender: true}); | ||
</script> | ||
</body> | ||
|
||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear why this is assigning to the
conn
parameter. Did you mean to set a default on line 201? Or just remove theconn
parameter altogether? Or doconn || navigator.connection
?I guess this was already buggy...