-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for same-site prerendering with Speculation Rules API (#258)
* initial commit to add support for same-origin prerendering with Speculation Rules API * reset author * refactor, best practices, and minor logic update * refactor, best practices, and minor logic update * avoid prefetching the link that has been prerendered in listen * create prerenderLimit as a constant to cater for the current Spec Rules API limitations and ease of change once the limitations are updated * refactor prerender specific checks and compliance with eslint-config-google formatting * create new tests for prerender with speculation rules * bug fix: addSpeculationRules does not resolve * adding prerendering doc in README * Update README.md Co-authored-by: Domenic Denicola <d@domenic.me> * removed reference to outdated OT removed extra argument in the promise constructor fixed the inconsistent application of spacing throughout repo * fixed promise rejection inside catch handler issue * Update src/index.mjs Co-authored-by: Domenic Denicola <d@domenic.me> * Update src/index.mjs Co-authored-by: Domenic Denicola <d@domenic.me> * Update src/prerender.mjs Co-authored-by: Domenic Denicola <d@domenic.me> * update return value documentation * remove conn param from prefetch and prerender functions * updated addSpeculationRules return value * restored conn param Co-authored-by: Addy Osmani <addyosmani@gmail.com> Co-authored-by: Domenic Denicola <d@domenic.me>
- Loading branch information
1 parent
6ac410c
commit 3d26f40
Showing
7 changed files
with
310 additions
and
16 deletions.
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
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) { | ||
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.