Quicklink
Faster subsequent page-loads by prefetching in-viewport links during idle t...
README
quicklink
Faster subsequent page-loads by prefetching or prerendering in-viewport links during idle time
How it works
Why
Multi page apps
Installation
- ```sh
- npm install --save quicklink
- ```
Usage
- ``` html
- <script src="dist/quicklink.umd.js"></script>
- <script>
- quicklink.listen();
- </script>
- ```
- ``` html
- <script>
- window.addEventListener('load', () =>{
- quicklink.listen();
- });
- </script>
- ```
- ``` js
- import { listen, prefetch } from "quicklink";
- ```
Single page apps (React)
Installation
- ```sh
- npm install quicklink webpack-route-manifest --save-dev
- ```
Usage
- ```sh
- import { withQuicklink } from 'quicklink/dist/react/hoc.js';
- const options = {
- origins: []
- };
- <Suspense fallback={<div>Loading...</div>}>
- <Route path="/" exact component={withQuicklink(Home, options)} />
- <Route path="/blog" exact component={withQuicklink(Blog, options)} />
- <Route path="/blog/:title" component={withQuicklink(Article, options)} />
- <Route path="/about" exact component={withQuicklink(About, options)} />
- </Suspense>
- ```
API
quicklink.listen(options)
options.prerender
Note: The prerendering mode (when this option is set to true) will fallback to the prefetching mode if the browser does not support prerender.
options.delay
options.el
options.limit
options.threshold
options.throttle
options.timeout
Note: The browser must be idle for the configured duration before prefetching.
options.timeoutFn
options.priority
options.origins
options.ignores
Note: An Array may contain String, RegExp, or Function values.
Important: This logic is executed _after_ origin matching!
options.onError
options.hrefFn
quicklink.prefetch(urls, isPriority)
Important: You much catch you own request error(s).
urls
Note: Each url value is resolved from the current location.
isPriority
Note: This behaves identically to listen()'s priority option.
quicklink.prerender(urls)
Important: You much catch you own request error(s).
urls
Note: As prerendering using Speculative Rules API only supports same-origin at this point, only same-origin urls are accepted. Any non same-origin urls will return a rejected Promise.
Polyfills
- ``` html
- <script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>
- ```
Recipes
Set a custom timeout for prefetching resources
- ``` js
- quicklink.listen({
- timeout: 4000
- });
- ```
Set the DOM element to observe for in-viewport links
- ``` js
- quicklink.listen({
- el: document.getElementById('carousel')
- });
- ```
Programmatically prefetch() URLs
- ``` js
- // Single URL
- quicklink.prefetch('2.html');
- // Multiple URLs
- quicklink.prefetch(['2.html', '3.html', '4.js']);
- // Multiple URLs, with high priority
- // Note: Can also be use with single URL!
- quicklink.prefetch(['2.html', '3.html', '4.js'], true);
- ```
Programmatically prerender() URLs
- ``` js
- // Single URL
- quicklink.prerender('2.html');
- // Multiple URLs
- quicklink.prerender(['2.html', '3.html', '4.js']);
- ```
Set the request priority for prefetches while scrolling
Note: This runs prefetch(..., true) with URLs found within the options.el container.
- ``` js
- quicklink.listen({ priority: true });
- ```
Specify a custom list of allowed origins
Important: You must also include your own hostname!
- ``` js
- quicklink.listen({
- origins: [
- // add mine
- 'my-website.com',
- 'api.my-website.com',
- // add third-parties
- 'other-website.com',
- 'example.com',
- // ...
- ]
- });
- ```
Allow all origins
- ``` js
- quicklink.listen({
- origins: true,
- // or
- origins: []
- });
- ```
Custom Ignore Patterns
- ``` js
- // Same-origin restraint is enabled by default.
- //
- // This example will ignore all requests to:
- // - all "/api/*" pathnames
- // - all ".zip" extensions
- // - all tags with "noprefetch" attribute
- //
- quicklink.listen({
- ignores: [
- /\/api\/?/,
- uri => uri.includes('.zip'),
- (uri, elem) => elem.hasAttribute('noprefetch')
- ]
- });
- ```
- ``` js
- quicklink.listen({
- ignores: [
- uri => uri.includes('#')
- // or RegExp: /#(.+)/
- // or element matching: (uri, elem) => !!elem.hash
- ]
- });
- ```
Custom URL to prefetch via hrefFn callback
- ``` js
- quicklink.listen({
- hrefFn: function(element) {
- return element.href.replace('html','json');
- }
- });
- ```
Browser Support
Using the prefetcher directly
- ``` html
- <script type="module">
- import { prefetch } from 'quicklink';
- prefetch(['1.html', '2.html']).catch(err => {
- // Handle own errors
- });
- </script>
- ```