-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] (options): Add a
hrefFn
option to build the URL to prefetch. (…
…#201) * feat(options): Add a `hrefFn` option to build the URL to prefetch. * fix conflicts on tests * adding hrefFn usage simple SPA example * added hrefFn demo within README * added hrefFn demo within README * improved hrefFn example code style Co-authored-by: Ruben Jimenez <[email protected]>
- Loading branch information
1 parent
a8872b8
commit ee072d4
Showing
9 changed files
with
153 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Prefetch experiments</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" media="screen" href="main.css"> | ||
</head> | ||
|
||
<body> | ||
<h1>Page 2</h1> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta est sint assumenda corrupti minima aut, magnam | ||
totam beatae ullam ea iste voluptatum iusto expedita animi rem vitae rerum atque nemo!</p> | ||
</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,4 @@ | ||
{ | ||
"title": "API Target to Prefetch Example", | ||
"description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta est sint assumenda corrupti minima aut, magnam totam beatae ullam ea iste voluptatum iusto expedita animi rem vitae rerum atque nemo!" | ||
} |
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,57 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Basic demo</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" media="screen" href="../../test/main.css" /> | ||
<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script> | ||
</head> | ||
<body> | ||
<div class="screen"> | ||
<h1>Basic demo</h1> | ||
<p> | ||
<a href="2.html">Link 1</a> | ||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | ||
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | ||
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | ||
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | ||
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | ||
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | ||
</p> | ||
</div> | ||
<script src="../../dist/quicklink.umd.js"></script> | ||
<script> | ||
quicklink.listen({ | ||
el: document.querySelector('div.screen'), | ||
// hrefFn will prevent quicklink to prefetch the "href" attribute URL | ||
// and the returned value will be prefetched as a result. | ||
hrefFn: function(element) { | ||
return element.href.replace('html','json'); | ||
} | ||
}); | ||
</script> | ||
|
||
<script> | ||
// Simple SPA-like page transition for demo purposes | ||
var container = document.querySelector('div.screen'), | ||
link = container.querySelector('a'), | ||
title = container.querySelector('h1'), | ||
paragraph = container.querySelector('p'); | ||
|
||
link.addEventListener('click', function (e) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
var href = e.target.href.replace('html','json'); | ||
fetch(href) | ||
.then(response => response.json()) | ||
.then(data => { | ||
title.innerHTML = document.title = data.title; | ||
paragraph.innerHTML = data.description; | ||
}); | ||
}); | ||
</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
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
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,26 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Prefetch: Custom function to build the URL.</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> | ||
<a href="4.html">Link 4</a> | ||
<script src="../dist/quicklink.umd.js"></script> | ||
<script> | ||
quicklink.listen({ | ||
hrefFn: (entry) => ( `https://example.com/?url=${entry.href}` ), | ||
}); | ||
</script> | ||
</body> | ||
|
||
</html> |